// 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") } }
// WriteMessage marshals the birpc.Message into messagepack and publishes it // to the redis pub-sub channel func (c *codec) WriteMessage(msg *birpc.Message) error { c.wmu.Lock() defer c.wmu.Unlock() m := &mpc.Message{} m.ID = msg.ID m.Func = msg.Func if t, ok := msg.Args.(msgp.Marshaler); ok { b, err := t.MarshalMsg(nil) if err != nil { return err } m.Args = msgp.Raw(b) } if t, ok := msg.Result.(msgp.Marshaler); ok { b, err := t.MarshalMsg(nil) if err != nil { return err } m.Result = msgp.Raw(b) } if msg.Error != nil { m.Error = &mpc.Error{Msg: msg.Error.Msg} } conn, err := c.db.Connection() if err != nil { return err } b, err := m.MarshalMsg(c.buf) if err != nil { return err } c.buf = b[:0] _, err = conn.Do("PUBLISH", c.ch, b) if err != nil { return err } err = conn.Return() return err }
// WriteMessage marshals the birpc.Message into messagepack and writes it out // to the websocket connection func (c *codec) WriteMessage(msg *birpc.Message) error { c.wmu.Lock() defer c.wmu.Unlock() m := &mpc.Message{} m.ID = msg.ID m.Func = msg.Func if t, ok := msg.Args.(msgp.Marshaler); ok { b, err := t.MarshalMsg(nil) if err != nil { return err } m.Args = msgp.Raw(b) } if t, ok := msg.Result.(msgp.Marshaler); ok { b, err := t.MarshalMsg(nil) if err != nil { return err } m.Result = msgp.Raw(b) } if msg.Error != nil { m.Error = &mpc.Error{Msg: msg.Error.Msg} } w, err := c.ws.NextWriter(websocket.BinaryMessage) if err != nil { return err } // replace the writer, encode the message, flush the buffer to the writer // buffer, close the writer thus flushing its buffer to the wire finally defer w.Close() c.w.Reset(w) if err = m.EncodeMsg(c.w); err != nil { return err } if err = c.w.Flush(); err != nil { return err } return nil }
// WriteMessage marshals the birpc.Message into MessagePack and writes it out func (c *codec) WriteMessage(msg *birpc.Message) error { c.wmu.Lock() defer c.wmu.Unlock() m := &mpc.Message{} m.ID = msg.ID m.Func = msg.Func if t, ok := msg.Args.(msgp.Marshaler); ok { b, err := t.MarshalMsg(nil) if err != nil { return err } m.Args = msgp.Raw(b) } if t, ok := msg.Result.(msgp.Marshaler); ok { b, err := t.MarshalMsg(nil) if err != nil { return err } m.Result = msgp.Raw(b) } if msg.Error != nil { m.Error = &mpc.Error{Msg: msg.Error.Msg} } if err := m.EncodeMsg(c.w); err != nil { return err } if err := c.w.Flush(); err != nil { return err } return nil }