// 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 }
func main() { data, err := bson.Marshal(&Person{Name: "Bob"}) if err != nil { panic(err) } p := new(Person) bson.Unmarshal(data, p) fmt.Printf("%v\n", p.Name) }
func main() { data, err := bson.Marshal(&Person{Name: "bob", Phone: "fdfdfdfd"}) if err != nil { panic(err) } bb := &OtherPerson{} err = bson.Unmarshal(data, bb) fmt.Printf("%v\n", bb) }
func main() { ba := &bsonArr{Length: "123", Rowdata: []byte{3, 4, 5}} aa, err := bson.Marshal(ba) fmt.Println(err) fmt.Println(aa) cc := new(bsonRaw) _ = bson.Unmarshal(aa, cc) fmt.Printf("%#v\n", cc) }
func LoadNodeFromDisk(id string) (node *Node, err error) { path := getPath(id) nbson, err := ioutil.ReadFile(fmt.Sprintf("%s/%s.bson", path, id)) if err != nil { return } node = new(Node) err = bson.Unmarshal(nbson, &node) if err != nil { node = nil } return }
func main() { person := &Person{AAA: "AAA"} bb, _ := bson.Marshal(struct{ VAL *Person }{person}) fmt.Println(bb) person2 := &Person{} r := &struct{ VAL *Person }{person2} err := bson.Unmarshal(bb, r) if err != nil { fmt.Println(err) } fmt.Println(person2) }
func (socket *mongoSocket) resetNonce() { debugf("Socket %p to %s: requesting a new nonce", socket, socket.addr) op := &queryOp{} op.query = &getNonceCmd{GetNonce: 1} op.collection = "admin.$cmd" op.limit = -1 op.replyFunc = func(err error, reply *replyOp, docNum int, docData []byte) { if err != nil { socket.kill(errors.New("getNonce: " + err.Error())) return } result := &getNonceResult{} err = bson.Unmarshal(docData, &result) if err != nil { socket.kill(errors.New("Failed to unmarshal nonce: " + err.Error())) return } debugf("Socket %p to %s: nonce unmarshalled: %#v", socket, socket.addr, result) if result.Code == 13390 { // mongos doesn't yet support auth (see http://j.mp/mongos-auth) result.Nonce = "mongos" } else if result.Nonce == "" { var msg string if result.Err != "" { msg = fmt.Sprintf("Got an empty nonce: %s (%d)", result.Err, result.Code) } else { msg = "Got an empty nonce" } socket.kill(errors.New(msg)) return } socket.Lock() if socket.cachedNonce != "" { socket.Unlock() panic("resetNonce: nonce already cached") } socket.cachedNonce = result.Nonce socket.gotNonce.Signal() socket.Unlock() } err := socket.Query(op) if err != nil { socket.kill(errors.New("resetNonce: " + err.Error())) } }
func main() { arg := &Arg{a: 1, b: 3} bb, _ := bson.Marshal(arg) argType := reflect.TypeOf(arg) req := new(Request) var out Arg _ = bson.Unmarshal(bb, &out) req.params = out var argv reflect.Value argv = reflect.New(argType.Elem()) argv.Elem().Set(reflect.ValueOf(req.params)) fmt.Printf("%#v\n", argv) }
func (m *MongoDB) Fetch(collection string, id Id, obj DBObjectWriter) error { c := m.Collection(collection) mdoc := MongoDBObject{Id: id} err := c.Find(bson.M{"_id": id}).One(&mdoc) if err != nil { return err } // The value will now be in bson.M form in the Object field. Reserialize it // to the receiver. It would be more clever to have a wrapper in mdoc around // the interface that does the serialization. bytes, err := bson.Marshal(mdoc.Object) if err != nil { return err } err = bson.Unmarshal(bytes, obj) if err != nil { return err } return err }
func fnBsonDecodeFn(buf *bytes.Buffer, ts *TestStruc) error { return bson.Unmarshal(buf.Bytes(), ts) }
func (socket *mongoSocket) Login(db string, user string, pass string) error { socket.Lock() for _, a := range socket.auth { if a.db == db && a.user == user && a.pass == pass { debugf("Socket %p to %s: login: db=%q user=%q (already logged in)", socket, socket.addr, db, user) socket.Unlock() return nil } } if auth, found := socket.dropLogout(db, user, pass); found { debugf("Socket %p to %s: login: db=%q user=%q (cached)", socket, socket.addr, db, user) socket.auth = append(socket.auth, auth) socket.Unlock() return nil } socket.Unlock() debugf("Socket %p to %s: login: db=%q user=%q", socket, socket.addr, db, user) // Note that this only works properly because this function is // synchronous, which means the nonce won't get reset while we're // using it and any other login requests will block waiting for a // new nonce provided in the defer call below. nonce, err := socket.getNonce() if err != nil { return err } defer socket.resetNonce() psum := md5.New() psum.Write([]byte(user + ":mongo:" + pass)) ksum := md5.New() ksum.Write([]byte(nonce + user)) ksum.Write([]byte(hex.EncodeToString(psum.Sum(nil)))) key := hex.EncodeToString(ksum.Sum(nil)) cmd := authCmd{Authenticate: 1, User: user, Nonce: nonce, Key: key} var mutex sync.Mutex var replyErr error mutex.Lock() op := queryOp{} op.query = &cmd 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 } // Must handle this within the read loop for the socket, so // that concurrent login requests are properly ordered. result := &authResult{} err = bson.Unmarshal(docData, result) if err != nil { replyErr = err return } if !result.Ok { replyErr = errors.New(result.ErrMsg) } socket.Lock() socket.dropAuth(db) socket.auth = append(socket.auth, authInfo{db, user, pass}) socket.Unlock() } err = socket.Query(&op) if err != nil { return err } mutex.Lock() // Wait. if replyErr != nil { debugf("Socket %p to %s: login error: %s", socket, socket.addr, replyErr) } else { debugf("Socket %p to %s: login successful", socket, socket.addr) } return replyErr }