本文整理匯總了Golang中camlistore/org/third_party/labix/org/v2/mgo/bson.Unmarshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Unmarshal函數的具體用法?Golang Unmarshal怎麽用?Golang Unmarshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Unmarshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestUnmarshalAllItemsWithPtrSetter
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)
}
}
}
}
示例2: TestUnmarshalMapDocumentTooShort
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")
}
}
示例3: TestUnmarshalAllItems
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))
}
}
示例4: TestUnmarshalSampleItems
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))
}
}
示例5: TestUnmarshalZeroesMap
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})
}
示例6: TestUnmarshalNilInStruct
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})
}
示例7: GetMeta
// GetMeta unmarshals the optional "metadata" field associated with the
// file into the result parameter. The meaning of keys under that field
// is user-defined. For example:
//
// result := struct{ INode int }{}
// err = file.GetMeta(&result)
// if err != nil {
// panic(err.String())
// }
// fmt.Printf("inode: %d\n", result.INode)
//
func (file *GridFile) GetMeta(result interface{}) (err error) {
file.m.Lock()
if file.doc.Metadata != nil {
err = bson.Unmarshal(file.doc.Metadata.Data, result)
}
file.m.Unlock()
return
}
示例8: TestMarshalShortWithGetter
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)
}
示例9: TestUnmarshalZeroesStruct
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)
}
示例10: TestUnmarshalNonNilInterface
func (s *S) TestUnmarshalNonNilInterface(c *C) {
data, err := bson.Marshal(bson.M{"b": 2})
c.Assert(err, IsNil)
m := bson.M{"a": 1}
var i interface{}
i = m
err = bson.Unmarshal(data, &i)
c.Assert(err, IsNil)
c.Assert(i, DeepEquals, bson.M{"b": 2})
c.Assert(m, DeepEquals, bson.M{"a": 1})
}
示例11: testCrossPair
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)
}
示例12: Test64bitInt
func (s *S) Test64bitInt(c *C) {
var i int64 = (1 << 31)
if int(i) > 0 {
data, err := bson.Marshal(bson.M{"i": int(i)})
c.Assert(err, IsNil)
c.Assert(string(data), Equals, wrapInDoc("\x12i\x00\x00\x00\x00\x80\x00\x00\x00\x00"))
var result struct{ I int }
err = bson.Unmarshal(data, &result)
c.Assert(err, IsNil)
c.Assert(int64(result.I), Equals, i)
}
}
示例13: BenchmarkUnmarhsalStruct
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)
}
}
示例14: BenchmarkUnmarhsalMap
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)
}
}
示例15: TestUnmarshalSetterSetZero
func (s *S) TestUnmarshalSetterSetZero(c *C) {
setterResult["foo"] = bson.SetZero
defer delete(setterResult, "field")
data, err := bson.Marshal(bson.M{"field": "foo"})
c.Assert(err, IsNil)
m := map[string]*setterType{}
err = bson.Unmarshal([]byte(data), m)
c.Assert(err, IsNil)
value, ok := m["field"]
c.Assert(ok, Equals, true)
c.Assert(value, IsNil)
}