本文整理汇总了Golang中github.com/tinylib/msgp/msgp.Decode函数的典型用法代码示例。如果您正苦于以下问题:Golang Decode函数的具体用法?Golang Decode怎么用?Golang Decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Decode函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Test1EncodeDecode
// This covers the following cases:
// - Recursive types
// - Non-builtin identifiers (and recursive types)
// - time.Time
// - map[string]string
// - anonymous structs
//
func Test1EncodeDecode(t *testing.T) {
f := 32.00
tt := &TestType{
F: &f,
Els: map[string]string{
"thing_one": "one",
"thing_two": "two",
},
Obj: struct {
ValueA string `msg:"value_a"`
ValueB []byte `msg:"value_b"`
}{
ValueA: "here's the first inner value",
ValueB: []byte("here's the second inner value"),
},
Child: nil,
Time: time.Now(),
Appended: msgp.Raw([]byte{0xc0}), // 'nil'
}
var buf bytes.Buffer
err := msgp.Encode(&buf, tt)
if err != nil {
t.Fatal(err)
}
tnew := new(TestType)
err = msgp.Decode(&buf, tnew)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(tt, tnew) {
t.Logf("in: %v", tt)
t.Logf("out: %v", tnew)
t.Fatal("objects not equal")
}
tanother := new(TestType)
buf.Reset()
msgp.Encode(&buf, tt)
var left []byte
left, err = tanother.UnmarshalMsg(buf.Bytes())
if err != nil {
t.Error(err)
}
if len(left) > 0 {
t.Errorf("%d bytes left", len(left))
}
if !reflect.DeepEqual(tt, tanother) {
t.Logf("in: %v", tt)
t.Logf("out: %v", tanother)
t.Fatal("objects not equal")
}
}
示例2: decodeBody
// decodeBody decodes the body of the message.
func decodeBody(r *msgp.Reader, v interface{}) error {
b, ok := v.(msgp.Decodable)
if !ok {
return ErrNotDecodable
}
return msgp.Decode(r, b)
}
示例3: ReadHeader
// ReadHeader reads the header from the wire.
func (c *msgpackCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
c.mt = mt
switch mt {
case codec.Request:
var h Request
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = uint64(h.ID)
m.Method = h.Method
case codec.Response:
var h Response
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = uint64(h.ID)
m.Error = h.Error
case codec.Publication:
var h Notification
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Method = h.Method
default:
return errors.New("Unrecognized message type")
}
return nil
}
示例4: readFromMsgpack
func (ra *roaringArray) readFromMsgpack(stream io.Reader) error {
r := snappy.NewReader(stream)
err := msgp.Decode(r, ra)
if err != nil {
return err
}
if len(ra.containers) != len(ra.keys) {
ra.containers = make([]container, len(ra.keys))
}
for i, v := range ra.conserz {
switch v.t {
case bitmapContype:
c := &bitmapContainer{}
_, err = c.UnmarshalMsg(v.r)
if err != nil {
return err
}
ra.containers[i] = c
case arrayContype:
c := &arrayContainer{}
_, err = c.UnmarshalMsg(v.r)
if err != nil {
return err
}
ra.containers[i] = c
case run16Contype:
c := &runContainer16{}
_, err = c.UnmarshalMsg(v.r)
if err != nil {
return err
}
ra.containers[i] = c
default:
return fmt.Errorf("unrecognized contype serialization code: '%v'", v.t)
}
}
ra.conserz = nil
return nil
}
示例5: DecodeRequest
func DecodeRequest(r *http.Request, v interface{}) {
defer r.Body.Close()
split := strings.SplitN(r.Header.Get(contentType), ";", 1)
if len(split) < 1 {
panic(errors.New("unsupport content-type"))
}
switch split[0] {
case "application/octet-stream":
x, ok := v.(msgp.Decodable)
if !ok {
panic(errors.New("unable to decode msgpack"))
} else if err := msgp.Decode(r.Body, x); err != nil {
panic(err)
}
case "application/json":
json.NewDecoder(r.Body).Decode(v)
default:
panic(errors.New("unsupport content-type"))
}
}
示例6: TestNotification
func TestNotification(t *testing.T) {
r1 := Notification{
Method: "Call",
Body: nil,
}
var buf bytes.Buffer
if err := msgp.Encode(&buf, &r1); err != nil {
t.Fatal(err)
}
var r2 Notification
if err := msgp.Decode(&buf, &r2); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(r1, r2) {
t.Error("values are not equal")
}
}
示例7: TestResponse
func TestResponse(t *testing.T) {
r1 := Response{
ID: 100,
Error: "error",
}
var buf bytes.Buffer
if err := msgp.Encode(&buf, &r1); err != nil {
t.Fatal(err)
}
var r2 Response
if err := msgp.Decode(&buf, &r2); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(r1, r2) {
t.Error("values are not equal")
}
}
示例8: TestIncidentEncodeDecode
func TestIncidentEncodeDecode(t *testing.T) {
v := Incident{}
var buf bytes.Buffer
msgp.Encode(&buf, &v)
m := v.Msgsize()
if buf.Len() > m {
t.Logf("WARNING: Msgsize() for %v is inaccurate", v)
}
vn := Incident{}
err := msgp.Decode(&buf, &vn)
if err != nil {
t.Error(err)
}
buf.Reset()
msgp.Encode(&buf, &v)
err = msgp.NewReader(&buf).Skip()
if err != nil {
t.Error(err)
}
}
示例9: readFromMsgpack
func (b *runContainer16) readFromMsgpack(stream io.Reader) (int, error) {
err := msgp.Decode(stream, b)
return 0, err
}
示例10: DecodeMSGPack
//ParseMSGPack decode msgpack to interface{}
func DecodeMSGPack(r *http.Request, v msgp.Decodable) {
defer r.Body.Close()
if err := msgp.Decode(r.Body, v); err != nil {
panic(err)
}
}