Example #1
0
// 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")
	}
}
Example #2
0
// 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)
}
Example #3
0
// 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
}
Example #4
0
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
}
Example #5
0
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"))
	}
}
Example #6
0
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")
	}
}
Example #7
0
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")
	}
}
Example #8
0
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)
	}
}
Example #9
0
func (b *runContainer16) readFromMsgpack(stream io.Reader) (int, error) {
	err := msgp.Decode(stream, b)
	return 0, err
}
Example #10
0
//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)
	}
}