當前位置: 首頁>>代碼示例>>Golang>>正文


Golang msgp.Encode函數代碼示例

本文整理匯總了Golang中github.com/tinylib/msgp/msgp.Encode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Encode函數的具體用法?Golang Encode怎麽用?Golang Encode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Encode函數的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Write

// Write writes a message to the wire which contains the header followed by the body.
// The body is assumed to satisfy the msgp.Encodable interface.
func (c *msgpackCodec) Write(m *codec.Message, b interface{}) error {
	switch m.Type {
	case codec.Request:
		h := Request{
			ID:     uint32(m.Id),
			Method: m.Method,
			Body:   b,
		}

		return msgp.Encode(c.rwc, &h)

	case codec.Response:
		h := Response{
			ID:   uint32(m.Id),
			Body: b,
		}

		h.Error = m.Error

		return msgp.Encode(c.rwc, &h)

	case codec.Publication:
		h := Notification{
			Method: m.Method,
			Body:   b,
		}

		return msgp.Encode(c.rwc, &h)

	default:
		return fmt.Errorf("Unrecognized message type: %v", m.Type)
	}

	return nil
}
開發者ID:micro,項目名稱:go-plugins,代碼行數:37,代碼來源:codec.go

示例2: 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")
	}
}
開發者ID:Congenital,項目名稱:msgp,代碼行數:67,代碼來源:gen_test.go

示例3: writeToMsgpack

func (ra *roaringArray) writeToMsgpack(stream io.Writer) error {

	ra.conserz = make([]containerSerz, len(ra.containers))
	for i, v := range ra.containers {
		switch cn := v.(type) {
		case *bitmapContainer:
			bts, err := cn.MarshalMsg(nil)
			if err != nil {
				return err
			}
			ra.conserz[i].t = bitmapContype
			ra.conserz[i].r = bts
		case *arrayContainer:
			bts, err := cn.MarshalMsg(nil)
			if err != nil {
				return err
			}
			ra.conserz[i].t = arrayContype
			ra.conserz[i].r = bts
		case *runContainer16:
			bts, err := cn.MarshalMsg(nil)
			if err != nil {
				return err
			}
			ra.conserz[i].t = run16Contype
			ra.conserz[i].r = bts
		default:
			panic(fmt.Errorf("Unrecognized container implementation: %T", cn))
		}
	}
	w := snappy.NewWriter(stream)
	err := msgp.Encode(w, ra)
	ra.conserz = nil
	return err
}
開發者ID:RoaringBitmap,項目名稱:roaring,代碼行數:35,代碼來源:roaringarray.go

示例4: BenchmarkIncidentEncode

func BenchmarkIncidentEncode(b *testing.B) {
	v := Incident{}
	var buf bytes.Buffer
	msgp.Encode(&buf, &v)
	b.SetBytes(int64(buf.Len()))
	en := msgp.NewWriter(msgp.Nowhere)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		v.EncodeMsg(en)
	}
	en.Flush()
}
開發者ID:postfix,項目名稱:bangarang,代碼行數:13,代碼來源:incident_gen_test.go

示例5: BenchmarkEncodebitmapContainerShortIterator

func BenchmarkEncodebitmapContainerShortIterator(b *testing.B) {
	v := bitmapContainerShortIterator{}
	var buf bytes.Buffer
	msgp.Encode(&buf, &v)
	b.SetBytes(int64(buf.Len()))
	en := msgp.NewWriter(msgp.Nowhere)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		v.EncodeMsg(en)
	}
	en.Flush()
}
開發者ID:RoaringBitmap,項目名稱:roaring,代碼行數:13,代碼來源:bitmapcontainer_gen_test.go

示例6: 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)
	}
}
開發者ID:postfix,項目名稱:bangarang,代碼行數:23,代碼來源:incident_gen_test.go

示例7: BenchmarkIncidentDecode

func BenchmarkIncidentDecode(b *testing.B) {
	v := Incident{}
	var buf bytes.Buffer
	msgp.Encode(&buf, &v)
	b.SetBytes(int64(buf.Len()))
	rd := msgp.NewEndlessReader(buf.Bytes(), b)
	dc := msgp.NewReader(rd)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := v.DecodeMsg(dc)
		if err != nil {
			b.Fatal(err)
		}
	}
}
開發者ID:postfix,項目名稱:bangarang,代碼行數:16,代碼來源:incident_gen_test.go

示例8: BenchmarkNotificationDecode

func BenchmarkNotificationDecode(b *testing.B) {
	r := Notification{
		Method: "Call",
		Body:   nil,
	}

	var buf bytes.Buffer
	msgp.Encode(&buf, &r)
	byts := buf.Bytes()

	mr := msgp.NewReader(&buf)

	for i := 0; i < b.N; i++ {
		buf.Reset()
		buf.Write(byts)
		r.DecodeMsg(mr)
	}
}
開發者ID:micro,項目名稱:go-plugins,代碼行數:18,代碼來源:rpc_test.go

示例9: BenchmarkFastDecode

// benchmark decoding a small, "fast" type.
// the point here is to see how much garbage
// is generated intrinsically by the encoding/
// decoding process as opposed to the nature
// of the struct.
func BenchmarkFastDecode(b *testing.B) {
	v := &TestFast{
		Lat:  40.12398,
		Long: -41.9082,
		Alt:  201.08290,
		Data: []byte("whaaaaargharbl"),
	}

	var buf bytes.Buffer
	msgp.Encode(&buf, v)
	dc := msgp.NewReader(msgp.NewEndlessReader(buf.Bytes(), b))
	b.SetBytes(int64(buf.Len()))
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		v.DecodeMsg(dc)
	}
}
開發者ID:Congenital,項目名稱:msgp,代碼行數:23,代碼來源:gen_test.go

示例10: BenchmarkFastEncode

// benchmark encoding a small, "fast" type.
// the point here is to see how much garbage
// is generated intrinsically by the encoding/
// decoding process as opposed to the nature
// of the struct.
func BenchmarkFastEncode(b *testing.B) {
	v := &TestFast{
		Lat:  40.12398,
		Long: -41.9082,
		Alt:  201.08290,
		Data: []byte("whaaaaargharbl"),
	}
	var buf bytes.Buffer
	msgp.Encode(&buf, v)
	en := msgp.NewWriter(msgp.Nowhere)
	b.SetBytes(int64(buf.Len()))
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		v.EncodeMsg(en)
	}
	en.Flush()
}
開發者ID:Congenital,項目名稱:msgp,代碼行數:23,代碼來源:gen_test.go

示例11: BenchmarkResponseDecode

func BenchmarkResponseDecode(b *testing.B) {
	r := Response{
		ID:    100,
		Error: "error",
		Body:  nil,
	}

	var buf bytes.Buffer
	msgp.Encode(&buf, &r)
	byts := buf.Bytes()

	mr := msgp.NewReader(&buf)

	for i := 0; i < b.N; i++ {
		buf.Reset()
		buf.Write(byts)
		r.DecodeMsg(mr)
	}
}
開發者ID:micro,項目名稱:go-plugins,代碼行數:19,代碼來源:rpc_test.go

示例12: EncodeMsg

func (r *Response) EncodeMsg(w *msgp.Writer) error {
	var bm msgp.Encodable

	if r.Body != nil {
		var ok bool
		bm, ok = r.Body.(msgp.Encodable)
		if !ok {
			return ErrNotEncodable
		}
	}

	var err error

	if err = w.WriteArrayHeader(ResponsePackSize); err != nil {
		return err
	}

	if err = w.WriteInt(ResponseType); err != nil {
		return err
	}

	if err = w.WriteUint32(r.ID); err != nil {
		return err
	}

	// No error.
	if r.Error == "" {
		if err = w.WriteNil(); err != nil {
			return err
		}

		if bm != nil {
			return msgp.Encode(w, bm)
		}
	} else {
		if err = w.WriteString(r.Error); err != nil {
			return err
		}
	}

	// Write nil body.
	return w.WriteNil()
}
開發者ID:micro,項目名稱:go-plugins,代碼行數:43,代碼來源:rpc.go

示例13: 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")
	}
}
開發者ID:micro,項目名稱:go-plugins,代碼行數:22,代碼來源:rpc_test.go

示例14: 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")
	}
}
開發者ID:micro,項目名稱:go-plugins,代碼行數:22,代碼來源:rpc_test.go


注:本文中的github.com/tinylib/msgp/msgp.Encode函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。