本文整理汇总了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
}
示例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")
}
}
示例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
}
示例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()
}
示例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()
}
示例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)
}
}
示例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)
}
}
}
示例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)
}
}
示例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)
}
}
示例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()
}
示例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)
}
}
示例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()
}
示例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")
}
}
示例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")
}
}