func (s *S) TestUnmarshalAllItemsWithPtrSetter(c *C) { for _, item := range allItems { for i := 0; i != 2; i++ { var field *setterType if i == 0 { obj := &ptrSetterDoc{} err := bson.Unmarshal([]byte(wrapInDoc(item.data)), obj) c.Assert(err, IsNil) field = obj.Field } else { obj := &valSetterDoc{} err := bson.Unmarshal([]byte(wrapInDoc(item.data)), obj) c.Assert(err, IsNil) field = &obj.Field } if item.data == "" { // Nothing to unmarshal. Should be untouched. if i == 0 { c.Assert(field, IsNil) } else { c.Assert(field.received, IsNil) } } else { expected := item.obj.(bson.M)["_"] c.Assert(field, NotNil, Commentf("Pointer not initialized (%#v)", expected)) c.Assert(field.received, DeepEquals, expected) } } } }
func (s *S) TestUnmarshalMapDocumentTooShort(c *C) { for _, data := range corruptedData { err := bson.Unmarshal([]byte(data), bson.M{}) c.Assert(err, ErrorMatches, "Document is corrupted") err = bson.Unmarshal([]byte(data), &struct{}{}) c.Assert(err, ErrorMatches, "Document is corrupted") } }
// read the request header func (c *ServerCodec) ReadRequestHeader(req *serverRequest) (err error) { msgheader := make([]byte, 4) n, err := io.ReadFull(c.rw.Reader, msgheader) if err != nil { return } if n != 4 { return io.ErrUnexpectedEOF } length := binary.LittleEndian.Uint32(msgheader) b := make([]byte, length) binary.LittleEndian.PutUint32(b, length) n, err = io.ReadFull(c.rw.Reader, b[4:]) if err != nil { if err == io.EOF { return io.ErrUnexpectedEOF } return err } if n != int(length-4) { return io.ErrUnexpectedEOF } if err = bson.Unmarshal(b, req); err != nil { return } return }
func (cn *conn) ReadResponseHeader(res *clientResponse) (err error) { msgheader := make([]byte, 4) n, err := cn.rw.Read(msgheader) if n != 4 { return io.ErrUnexpectedEOF } if err != nil { res = nil if err == io.EOF { return errors.New("rpc: client cannot read requestHeader" + err.Error()) } err = errors.New("rpc: client cannot read requestHeader " + err.Error()) return } length := binary.LittleEndian.Uint32(msgheader) b := make([]byte, length) binary.LittleEndian.PutUint32(b, length) n, err = io.ReadFull(cn.rw.Reader, b[4:]) if err != nil { if err == io.EOF { return io.ErrUnexpectedEOF } return } if n != int(length-4) { return io.ErrUnexpectedEOF } err = bson.Unmarshal(b, res) return }
func (cn *conn) ReadResponseBody(reply interface{}) (err error) { msgbody := make([]byte, 4) n, err := cn.rw.Read(msgbody) if n != 4 { return io.ErrUnexpectedEOF } if err != nil { if err == io.EOF { return io.ErrUnexpectedEOF } return } length := binary.LittleEndian.Uint32(msgbody) b := make([]byte, length) binary.LittleEndian.PutUint32(b, length) n, err = io.ReadFull(cn.rw.Reader, b[4:]) if err != nil { if err == io.EOF { return io.ErrUnexpectedEOF } return } if n != int(length-4) { return io.ErrUnexpectedEOF } if err = bson.Unmarshal(b, reply); err != nil { return } return }
func (s *S) TestUnmarshalSampleItems(c *C) { for i, item := range sampleItems { value := bson.M{} err := bson.Unmarshal([]byte(item.data), value) c.Assert(err, IsNil) c.Assert(value, DeepEquals, item.obj, Commentf("Failed on item %d", i)) } }
func (s *S) TestUnmarshalAllItems(c *C) { for i, item := range allItems { value := bson.M{} err := bson.Unmarshal([]byte(wrapInDoc(item.data)), value) c.Assert(err, IsNil) c.Assert(value, DeepEquals, item.obj, Commentf("Failed on item %d: %#v", i, item)) } }
func (s *S) TestUnmarshalZeroesMap(c *C) { data, err := bson.Marshal(bson.M{"b": 2}) c.Assert(err, IsNil) m := bson.M{"a": 1} err = bson.Unmarshal(data, &m) c.Assert(err, IsNil) c.Assert(m, DeepEquals, bson.M{"b": 2}) }
func (s *S) TestMarshalShortWithGetter(c *C) { obj := typeWithIntGetter{42} data, err := bson.Marshal(obj) c.Assert(err, IsNil) m := bson.M{} err = bson.Unmarshal(data, m) c.Assert(m["v"], Equals, 42) }
func (s *S) TestUnmarshalNilInStruct(c *C) { // Nil is the default value, so we need to ensure it's indeed being set. b := byte(1) v := &struct{ Ptr *byte }{&b} err := bson.Unmarshal([]byte(wrapInDoc("\x0Aptr\x00")), v) c.Assert(err, IsNil) c.Assert(v, DeepEquals, &struct{ Ptr *byte }{nil}) }
func (s *S) TestUnmarshalZeroesStruct(c *C) { data, err := bson.Marshal(bson.M{"b": 2}) c.Assert(err, IsNil) type T struct{ A, B int } v := T{A: 1} err = bson.Unmarshal(data, &v) c.Assert(err, IsNil) c.Assert(v.A, Equals, 0) c.Assert(v.B, Equals, 2) }
func testCrossPair(c *C, dump interface{}, load interface{}) { c.Logf("Dump: %#v", dump) c.Logf("Load: %#v", load) zero := makeZeroDoc(load) data, err := bson.Marshal(dump) c.Assert(err, IsNil) c.Logf("Dumped: %#v", string(data)) err = bson.Unmarshal(data, zero) c.Assert(err, IsNil) c.Logf("Loaded: %#v", zero) c.Assert(zero, DeepEquals, load) }
func (s *S) TestSimpleUnmarshal(c *C) { for _, item := range simpleUnmarshalItems { err := bson.Unmarshal([]byte(item.data), item.obj) c.Assert(err, IsNil) } c.Assert(intt, Equals, 10) c.Assert(stringt, Equals, "string") c.Assert(floatt, Equals, 3.14) c.Assert(truet, Equals, true) c.Assert(falset, Equals, false) c.Assert(int64t, Equals, int64(18)) }
func BenchmarkUnmarhsalStruct(b *testing.B) { v := BenchT{A: "A", D: "D", E: "E"} data, err := bson.Marshal(&v) if err != nil { panic(err) } b.ResetTimer() for i := 0; i < b.N; i++ { err = bson.Unmarshal(data, &v) } if err != nil { panic(err) } }
func BenchmarkUnmarhsalMap(b *testing.B) { m := bson.M{"a": "a", "d": "d", "e": "e"} data, err := bson.Marshal(&m) if err != nil { panic(err) } b.ResetTimer() for i := 0; i < b.N; i++ { err = bson.Unmarshal(data, &m) } if err != nil { panic(err) } }
func (s *S) TestUnmarshalErrorItems(c *C) { for _, item := range unmarshalErrorItems { data := []byte(wrapInDoc(item.data)) var value interface{} switch reflect.ValueOf(item.obj).Kind() { case reflect.Map, reflect.Ptr: value = makeZeroDoc(item.obj) case reflect.Invalid: value = bson.M{} default: value = item.obj } err := bson.Unmarshal(data, value) c.Assert(err, ErrorMatches, item.error) } }
func (s *S) TestUnmarshalSetterErrors(c *C) { boom := errors.New("BOOM") setterResult["2"] = boom defer func() { delete(setterResult, "2") }() m := map[string]*setterType{} data := wrapInDoc("\x02abc\x00\x02\x00\x00\x001\x00" + "\x02def\x00\x02\x00\x00\x002\x00" + "\x02ghi\x00\x02\x00\x00\x003\x00") err := bson.Unmarshal([]byte(data), m) c.Assert(err, Equals, boom) c.Assert(m["abc"], NotNil) c.Assert(m["def"], IsNil) c.Assert(m["ghi"], IsNil) c.Assert(m["abc"].received, Equals, "1") }
func (s *S) TestUnmarshalSetterOmits(c *C) { setterResult["2"] = &bson.TypeError{} setterResult["4"] = &bson.TypeError{} defer func() { delete(setterResult, "2") delete(setterResult, "4") }() m := map[string]*setterType{} data := wrapInDoc("\x02abc\x00\x02\x00\x00\x001\x00" + "\x02def\x00\x02\x00\x00\x002\x00" + "\x02ghi\x00\x02\x00\x00\x003\x00" + "\x02jkl\x00\x02\x00\x00\x004\x00") err := bson.Unmarshal([]byte(data), m) c.Assert(err, IsNil) c.Assert(m["abc"], NotNil) c.Assert(m["def"], IsNil) c.Assert(m["ghi"], NotNil) c.Assert(m["jkl"], IsNil) c.Assert(m["abc"].received, Equals, "1") c.Assert(m["ghi"].received, Equals, "3") }
// read the request body func (c *ServerCodec) ReadRequestBody(body interface{}) (err error) { msgbody := make([]byte, 4) n, err := io.ReadFull(c.rw.Reader, msgbody) if err != nil { return } if n != 4 { return io.ErrUnexpectedEOF } length := binary.LittleEndian.Uint32(msgbody) b := make([]byte, length) binary.LittleEndian.PutUint32(b, length) n, err = io.ReadFull(c.rw.Reader, b[4:]) if err != nil { if err == io.EOF { return io.ErrUnexpectedEOF } return } if err = bson.Unmarshal(b, body); err != nil { return } return }
func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) { obj := &setterType{} err := bson.Unmarshal([]byte(sampleItems[0].data), obj) c.Assert(err, IsNil) c.Assert(obj.received, DeepEquals, bson.M{"hello": "world"}) }
func testUnmarshal(c *C, data string, obj interface{}) { zero := makeZeroDoc(obj) err := bson.Unmarshal([]byte(data), zero) c.Assert(err, IsNil) c.Assert(zero, DeepEquals, obj) }