|
@@ -0,0 +1,124 @@
|
|
|
+package msgpack_test
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/gob"
|
|
|
+ "fmt"
|
|
|
+ "reflect"
|
|
|
+ "testing"
|
|
|
+
|
|
|
+ "github.com/vmihailenco/msgpack/v5"
|
|
|
+)
|
|
|
+
|
|
|
+func Encode(v interface{}) ([]byte, error) {
|
|
|
+ enc := msgpack.GetEncoder()
|
|
|
+
|
|
|
+ var buf bytes.Buffer
|
|
|
+ enc.Reset(&buf)
|
|
|
+
|
|
|
+ err := enc.Encode(v)
|
|
|
+ b := buf.Bytes()
|
|
|
+
|
|
|
+ msgpack.PutEncoder(enc)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return b, err
|
|
|
+}
|
|
|
+
|
|
|
+func Decode(data []byte, v interface{}) error {
|
|
|
+ dec := msgpack.GetDecoder()
|
|
|
+
|
|
|
+ dec.Reset(bytes.NewReader(data))
|
|
|
+ err := dec.Decode(v)
|
|
|
+
|
|
|
+ msgpack.PutDecoder(dec)
|
|
|
+
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func TestA(t *testing.T) {
|
|
|
+ v := int(0)
|
|
|
+ in := map[string]interface{}{"v": v}
|
|
|
+
|
|
|
+ bs, e := Encode(in)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Println(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ out := map[string]interface{}{}
|
|
|
+
|
|
|
+ e = Decode(bs, &out)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Println(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println(fmt.Sprintf("input: %v %v", reflect.TypeOf(in["v"]), in["v"]))
|
|
|
+ fmt.Println(fmt.Sprintf("output: %v %v", reflect.TypeOf(out["v"]), out["v"]))
|
|
|
+
|
|
|
+ fmt.Println()
|
|
|
+}
|
|
|
+
|
|
|
+func TestB(t *testing.T) {
|
|
|
+ in := map[string]interface{}{
|
|
|
+ "int": int(0),
|
|
|
+ "int8": int8(0),
|
|
|
+ "int16": int16(0),
|
|
|
+ "int32": int32(0),
|
|
|
+ "int64": int64(0),
|
|
|
+ // "struct": struct{ A string }{A: "a"}, // gob: type not registered for interface: struct { A string }
|
|
|
+ }
|
|
|
+
|
|
|
+ var buf bytes.Buffer
|
|
|
+ e := gob.NewEncoder(&buf).Encode(in)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Println(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ out := map[string]interface{}{}
|
|
|
+
|
|
|
+ e = gob.NewDecoder(&buf).Decode(&out)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Println(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, v := range in {
|
|
|
+ fmt.Println(fmt.Sprintf("input: %s = %v(%v)", k, reflect.TypeOf(v), v))
|
|
|
+ }
|
|
|
+ for k, v := range out {
|
|
|
+ fmt.Println(fmt.Sprintf("output: %s = %v(%v)", k, reflect.TypeOf(v), v))
|
|
|
+ }
|
|
|
+ fmt.Println()
|
|
|
+}
|
|
|
+func TestC(t *testing.T) {
|
|
|
+ in := map[string]interface{}{
|
|
|
+ "int": int(0),
|
|
|
+ "int8": int8(0),
|
|
|
+ "int16": int16(0),
|
|
|
+ "int32": int32(0),
|
|
|
+ "int64": int64(0),
|
|
|
+ "struct": struct{ A string }{A: "a"},
|
|
|
+ }
|
|
|
+
|
|
|
+ var buf bytes.Buffer
|
|
|
+ e := msgpack.NewEncoder(&buf).Encode(in)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Println(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ out := map[string]interface{}{}
|
|
|
+
|
|
|
+ e = msgpack.NewDecoder(&buf).Decode(&out)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Println(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, v := range in {
|
|
|
+ fmt.Println(fmt.Sprintf("input: %s = %v(%v)", k, reflect.TypeOf(v), v))
|
|
|
+ }
|
|
|
+ for k, v := range out {
|
|
|
+ fmt.Println(fmt.Sprintf("output: %s = %v(%v)", k, reflect.TypeOf(v), v))
|
|
|
+ }
|
|
|
+ fmt.Println()
|
|
|
+}
|