Skip to content

Commit

Permalink
Merge pull request #5 from vipally/develop
Browse files Browse the repository at this point in the history
update version to 1.1.0
rename Pack/Unpack to Encode/Decode
  • Loading branch information
vipally committed Nov 1, 2017
2 parents b52aa27 + c5ede74 commit f273ff1
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 169 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# binary [![Build Status](https://travis-ci.org/vipally/binary.svg?branch=master)](https://travis-ci.org/vipally/binary) [![Coverage Status](https://coveralls.io/repos/github/vipally/binary/badge.svg?branch=master)](https://coveralls.io/github/vipally/binary?branch=master) [![GoDoc](https://godoc.org/github.com/vipally/binary?status.svg)](https://godoc.org/github.com/vipally/binary) ![Version](https://img.shields.io/badge/version-1.0.0.final-green.svg)
Package binary is uesed to Pack/Unpack between go data and byte slice.
# binary

[![Build Status](https://travis-ci.org/vipally/binary.svg?branch=master)](https://travis-ci.org/vipally/binary) [![Coverage Status](https://coveralls.io/repos/github/vipally/binary/badge.svg?branch=master)](https://coveralls.io/github/vipally/binary?branch=master) [![GoDoc](https://godoc.org/github.com/vipally/binary?status.svg)](https://godoc.org/github.com/vipally/binary) ![Version](https://img.shields.io/badge/version-1.1.0-green.svg)

***

Package binary is uesed to Encode/Decode between go data and byte slice.

The main purpose of this package is to replace package "std.binary".

Expand Down Expand Up @@ -30,7 +35,7 @@ Site : [https://github.com/vipally](https://github.com/vipally)
And their direct pointers.
eg: *string, *struct, *map, *slice, *int32.

# 2. [recommended usage] Use Pack/UnPack to read/write memory buffer directly.
# 2. [recommended usage] Use Encode/Decode to read/write memory buffer directly.
## Use RegStruct to improve struct encoding/decoding efficiency.
type someRegedStruct struct {
A int `binary:"ignore"`
Expand All @@ -39,31 +44,31 @@ Site : [https://github.com/vipally](https://github.com/vipally)
}
binary.RegStruct((*someRegedStruct)(nil))

If data implements interface Packer, it will use data.Pack/data.Unpack
If data implements interface BinaryEncoder, it will use data.Encode/data.Decode
to encode/decode data.
NOTE that data.Unpack must implement on pointer receiever to enable modifying
receiever.Even though Size/Pack of data can implement on non-pointer receiever,
binary.Pack(&data, nil) is required if data has implement interface Packer.
binary.Pack(data, nil) will probably NEVER use Packer methods to Pack/Unpack
NOTE that data.Decode must implement on pointer receiever to enable modifying
receiever.Even though Size/Encode of data can implement on non-pointer receiever,
binary.Encode(&data, nil) is required if data has implement interface BinaryEncoder.
binary.Encode(data, nil) will probably NEVER use BinaryEncoder methods to Encode/Decode
data.
eg:

import "github.com/vipally/binary"

//1.Pack with default buffer
if bytes, err := binary.Pack(&data, nil); err==nil{
//1.Encode with default buffer
if bytes, err := binary.Encode(&data, nil); err==nil{
//...
}

//2.Pack with existing buffer
//2.Encode with existing buffer
size := binary.Sizeof(data)
buffer := make([]byte, size)
if bytes, err := binary.Pack(&data, buffer); err==nil{
if bytes, err := binary.Encode(&data, buffer); err==nil{
//...
}

//3.Unpack from buffer
if err := binary.Unpack(bytes, &data); err==nil{
//3.Decode from buffer
if err := binary.Decode(bytes, &data); err==nil{
//...
}

Expand Down
38 changes: 19 additions & 19 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func BenchmarkWriteRegedStruct(b *testing.B) {
data := regedStruct(_struct)
testBenchWrite(b, data, "BenchmarkWriteRegedStruct")
}
func BenchmarkPackStruct(b *testing.B) {
func BenchmarkEncodeStruct(b *testing.B) {
data := _struct
testBenchPack(b, data, "BenchmarkPackStruct")
testBenchEncode(b, data, "BenchmarkEncodeStruct")
}
func BenchmarkPackRegedStruct(b *testing.B) {
func BenchmarkEncodeRegedStruct(b *testing.B) {
data := regedStruct(_struct)
testBenchPack(b, data, "BenchmarkPackRegedStruct")
testBenchEncode(b, data, "BenchmarkEncodeRegedStruct")
}
func BenchmarkGobDecodeStruct(b *testing.B) {
data := _struct
Expand All @@ -90,14 +90,14 @@ func BenchmarkReadRegedStruct(b *testing.B) {
dataC := regedStruct(wStruct)
testBenchRead(b, &data, &dataC, "BenchmarkReadRegedStruct")
}
func BenchmarkUnackStruct(b *testing.B) {
func BenchmarkDecodeStruct(b *testing.B) {
data := _struct
testBenchUnpack(b, &data, &wStruct, "BenchmarkUnackStruct")
testBenchDecode(b, &data, &wStruct, "BenchmarkDecodeStruct")
}
func BenchmarkUnpackRegedStruct(b *testing.B) {
func BenchmarkDecodeRegedStruct(b *testing.B) {
data := regedStruct(_struct)
dataC := regedStruct(wStruct)
testBenchUnpack(b, &data, &dataC, "BenchmarkUnpackRegedStruct")
testBenchDecode(b, &data, &dataC, "BenchmarkDecodeRegedStruct")
}

//////////////////////////////////////////////////////////////////Int1000
Expand All @@ -113,9 +113,9 @@ func BenchmarkWriteInt1000(b *testing.B) {
data := u32Array1000
testBenchWrite(b, data, "BenchmarkWriteInt1000")
}
func BenchmarkPackInt1000(b *testing.B) {
func BenchmarkEncodeInt1000(b *testing.B) {
data := u32Array1000
testBenchPack(b, data, "BenchmarkPackInt1000")
testBenchEncode(b, data, "BenchmarkEncodeInt1000")
} //BUG: this case will fail
//func BenchmarkGobDecodeInt1000(b *testing.B) {
// data := u32Array1000
Expand All @@ -131,7 +131,7 @@ func BenchmarkReadInt1000(b *testing.B) {
}
func BenchmarkUnackInt1000(b *testing.B) {
data := u32Array1000
testBenchUnpack(b, &data, &u32Array1000W, "BenchmarkUnackInt1000")
testBenchDecode(b, &data, &u32Array1000W, "BenchmarkUnackInt1000")
}

//////////////////////////////////////////////////////////////////String
Expand All @@ -147,9 +147,9 @@ func BenchmarkWriteString(b *testing.B) {
data := str
testBenchWrite(b, data, "BenchmarkWriteString")
}
func BenchmarkPackString(b *testing.B) {
func BenchmarkEncodeString(b *testing.B) {
data := str
testBenchPack(b, data, "BenchmarkPackString")
testBenchEncode(b, data, "BenchmarkEncodeString")
}
func BenchmarkGobDecodeString(b *testing.B) {
data := str
Expand All @@ -165,7 +165,7 @@ func BenchmarkReadString(b *testing.B) {
}
func BenchmarkUnackString(b *testing.B) {
data := str
testBenchUnpack(b, &data, &strW, "BenchmarkUnackString")
testBenchDecode(b, &data, &strW, "BenchmarkUnackString")
}

func newSame(v reflect.Value) (value reflect.Value) {
Expand Down Expand Up @@ -226,11 +226,11 @@ func testBenchWrite(b *testing.B, data interface{}, caseName string) {
}
b.StopTimer()
}
func testBenchPack(b *testing.B, data interface{}, caseName string) {
func testBenchEncode(b *testing.B, data interface{}, caseName string) {
b.SetBytes(int64(Sizeof(data)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Pack(data, buff)
Encode(data, buff)
}
b.StopTimer()
}
Expand Down Expand Up @@ -309,16 +309,16 @@ func testBenchRead(b *testing.B, data, w interface{}, caseName string) {
b.Fatalf("%s doesn't match:\ngot %#v;\nwant %#v", caseName, w, data)
}
}
func testBenchUnpack(b *testing.B, data, w interface{}, caseName string) {
buf, err := Pack(data, buff)
func testBenchDecode(b *testing.B, data, w interface{}, caseName string) {
buf, err := Encode(data, buff)
if err != nil {
b.Error(caseName, err)
}
b.SetBytes(int64(len(buf)))

b.ResetTimer()
for i := 0; i < b.N; i++ {
Unpack(buf, w)
Decode(buf, w)
}
b.StopTimer()
if b.N > 0 && !reflect.DeepEqual(data, w) {
Expand Down
Loading

0 comments on commit f273ff1

Please sign in to comment.