• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang msgp.Encode函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang msgp.NewEndlessReader函数代码示例发布时间:2022-05-28
下一篇:
Golang msgp.Decode函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap