本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/gopkg/in/mgo/v2/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: loginRun
func (socket *mongoSocket) loginRun(db string, query, result interface{}, f func() error) error {
var mutex sync.Mutex
var replyErr error
mutex.Lock()
op := queryOp{}
op.query = query
op.collection = db + ".$cmd"
op.limit = -1
op.replyFunc = func(err error, reply *replyOp, docNum int, docData []byte) {
defer mutex.Unlock()
if err != nil {
replyErr = err
return
}
err = bson.Unmarshal(docData, result)
if err != nil {
replyErr = err
} else {
// Must handle this within the read loop for the socket, so
// that concurrent login requests are properly ordered.
replyErr = f()
}
}
err := socket.Query(&op)
if err != nil {
return err
}
mutex.Lock() // Wait.
return replyErr
}
示例4: 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))
}
}
示例5: 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
}
示例6: 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})
}
示例7: 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})
}
示例8: 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))
}
}
示例9: TestMarshalWithGetterNil
func (s *S) TestMarshalWithGetterNil(c *C) {
obj := docWithGetterField{}
data, err := bson.Marshal(obj)
c.Assert(err, IsNil)
m := bson.M{}
err = bson.Unmarshal(data, m)
c.Assert(err, IsNil)
c.Assert(m, DeepEquals, bson.M{"_": "<value is nil>"})
}
示例10: 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(err, IsNil)
c.Assert(m["v"], Equals, 42)
}
示例11: 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)
}
示例12: objToDoc
func objToDoc(obj interface{}) (d bson.D, err error) {
data, err := bson.Marshal(obj)
if err != nil {
return nil, err
}
err = bson.Unmarshal(data, &d)
if err != nil {
return nil, err
}
return d, err
}
示例13: 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})
}
示例14: 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)
}
示例15: 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)
}
}