本文整理汇总了Golang中gopkg/in/mgo/v2-unstable/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: Login
func (socket *mongoSocket) Login(cred Credential) error {
nonce, err := socket.getNonce()
if err != nil {
return err
}
psum := md5.New()
psum.Write([]byte(cred.Username + ":mongo:" + cred.Password))
ksum := md5.New()
ksum.Write([]byte(nonce + cred.Username))
ksum.Write([]byte(hex.EncodeToString(psum.Sum(nil))))
key := hex.EncodeToString(ksum.Sum(nil))
cmd := authCmd{Authenticate: 1, User: cred.Username, Nonce: nonce, Key: key}
fmt.Printf("Trying to login with nonce:%s \n", nonce)
res := authResult{}
op := queryOp{}
op.query = &cmd
op.collection = cred.Source + ".$cmd"
op.limit = -1
op.replyFunc = func(err error, reply *replyOp, docNum int, docData []byte) {
err = bson.Unmarshal(docData, &res)
if err != nil {
return
}
}
err = socket.Query(&op)
if err != nil {
return err
}
return err
}
示例5: 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))
}
}
示例6: 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
}
示例7: 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))
}
}
示例8: 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})
}
示例9: 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})
}
示例10: TestSpecTests
func (s *S) TestSpecTests(c *C) {
for _, data := range specTests {
var test specTest
err := yaml.Unmarshal([]byte(data), &test)
c.Assert(err, IsNil)
c.Logf("Running spec test set %q", test.Description)
for _, doc := range test.Documents {
if doc.Error != nil {
continue
}
c.Logf("Ensuring %q decodes as %v", doc.Encoded, doc.Decoded)
var decoded map[string]interface{}
encoded, err := hex.DecodeString(doc.Encoded)
c.Assert(err, IsNil)
err = bson.Unmarshal(encoded, &decoded)
c.Assert(err, IsNil)
c.Assert(decoded, DeepEquals, doc.Decoded)
}
for _, doc := range test.Documents {
if doc.DecodeOnly || doc.Error != nil {
continue
}
c.Logf("Ensuring %v encodes as %q", doc.Decoded, doc.Encoded)
encoded, err := bson.Marshal(doc.Decoded)
c.Assert(err, IsNil)
c.Assert(strings.ToUpper(hex.EncodeToString(encoded)), Equals, doc.Encoded)
}
for _, doc := range test.Documents {
if doc.Error == nil {
continue
}
c.Logf("Ensuring %q errors when decoded: %s", doc.Encoded, doc.Error)
var decoded map[string]interface{}
encoded, err := hex.DecodeString(doc.Encoded)
c.Assert(err, IsNil)
err = bson.Unmarshal(encoded, &decoded)
c.Assert(err, NotNil)
c.Logf("Failed with: %v", err)
}
}
}
示例11: 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>"})
}
示例12: 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)
}
示例13: 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)
}
示例14: 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
}
示例15: 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})
}