mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-18 20:38:00 +01:00
Merge branch 'master' of https://github.com/dgryski/msgpack into go
Conflicts: go/pack.go
This commit is contained in:
commit
5456814199
11
go/Makefile
11
go/Makefile
@ -1,11 +0,0 @@
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=msgpack
|
||||
|
||||
GOFILES=pack.go unpack.go
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
||||
|
||||
%: install %.go
|
||||
$(GC) $*.go
|
||||
$(LD) -o $@ $*.$O
|
3
go/README
Normal file
3
go/README
Normal file
@ -0,0 +1,3 @@
|
||||
This builds with the new 'go' tool.
|
||||
|
||||
It is installable with "go get github.com/msgpack/msgpack/go".
|
@ -1,7 +1,6 @@
|
||||
package msgpack_test
|
||||
package msgpack
|
||||
|
||||
import (
|
||||
. "msgpack"
|
||||
"testing"
|
||||
"bytes"
|
||||
"reflect"
|
||||
@ -74,6 +73,35 @@ func TestPackUint8(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackBytes(t *testing.T) {
|
||||
for _, i := range []struct {
|
||||
s string
|
||||
b []byte
|
||||
}{
|
||||
{"a", []byte("\xa1a")},
|
||||
{"hello", []byte("\xa5hello")},
|
||||
{"world world world", []byte("\xb1world world world")},
|
||||
{"world world world world world world", []byte("\xda\x00#world world world world world world")},
|
||||
} {
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
_, err := PackBytes(b, []byte(i.s))
|
||||
if err != nil {
|
||||
t.Error("err != nil")
|
||||
}
|
||||
|
||||
v, _, e := Unpack(b)
|
||||
if e != nil {
|
||||
t.Error("err != nil")
|
||||
}
|
||||
|
||||
if !equal(v, reflect.ValueOf([]byte(i.s))) {
|
||||
t.Error("unpack(pack(%s)) != %s", i.s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackUint16(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []uint16{0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535} {
|
||||
@ -247,7 +275,14 @@ func TestPackMap(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("err != nil")
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte{0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03}) != 0 {
|
||||
|
||||
// map ordering is no longer deterministic -- check all possible orderings :(
|
||||
if bytes.Compare(b.Bytes(), []byte{0x83, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05}) != 0 &&
|
||||
bytes.Compare(b.Bytes(), []byte{0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03}) != 0 &&
|
||||
bytes.Compare(b.Bytes(), []byte{0x83, 0x02, 0x03, 0x00, 0x01, 0x04, 0x05}) != 0 &&
|
||||
bytes.Compare(b.Bytes(), []byte{0x83, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01}) != 0 &&
|
||||
bytes.Compare(b.Bytes(), []byte{0x83, 0x04, 0x05, 0x00, 0x01, 0x02, 0x03}) != 0 &&
|
||||
bytes.Compare(b.Bytes(), []byte{0x83, 0x04, 0x05, 0x02, 0x03, 0x00, 0x01}) != 0 {
|
||||
t.Error("wrong output", b.Bytes())
|
||||
}
|
||||
}
|
||||
|
10
go/pack.go
10
go/pack.go
@ -2,9 +2,9 @@ package msgpack
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
@ -59,7 +59,7 @@ func PackUint(writer io.Writer, value uint) (n int, err error) {
|
||||
case 8:
|
||||
return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, syscall.ENOENT // never get here
|
||||
return 0, os.ErrNotExist // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
@ -116,7 +116,7 @@ func PackInt(writer io.Writer, value int) (n int, err error) {
|
||||
case 8:
|
||||
return PackInt64(writer, *(*int64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, syscall.ENOENT // never get here
|
||||
return 0, os.ErrNotExist // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
@ -304,7 +304,7 @@ func PackUintArray(writer io.Writer, value []uint) (n int, err error) {
|
||||
case 8:
|
||||
return PackUint64Array(writer, *(*[]uint64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, syscall.ENOENT // never get here
|
||||
return 0, os.ErrNotExist // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
@ -483,7 +483,7 @@ func PackIntArray(writer io.Writer, value []int) (n int, err error) {
|
||||
case 8:
|
||||
return PackInt64Array(writer, *(*[]int64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, syscall.ENOENT // never get here
|
||||
return 0, os.ErrNotExist // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
|
@ -174,7 +174,7 @@ func unpack(reader io.Reader, reflected bool) (v reflect.Value, n int, err error
|
||||
}
|
||||
nbytesread += n
|
||||
} else if c >= 0xa0 && c <= 0xbf {
|
||||
data := make([]byte, c&0xf)
|
||||
data := make([]byte, c&0x1f)
|
||||
n, e := reader.Read(data)
|
||||
nbytesread += n
|
||||
if e != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user