Пример #1
0
func fnBsonEncodeFn(buf *bytes.Buffer, ts *TestStruc) error {
	bs, err := bson.Marshal(ts)
	if err == nil {
		buf.Write(bs)
	}
	return err
}
func main() {

	nils := NilStruct{Abb: "1111111", Nilone: nil, Niltwo: nil, nilthree: nil}
	bb, err := bson.Marshal(&nils)
	fmt.Printf("%v\n", bb)
	fmt.Printf("%v\n", err)
}
Пример #3
0
func (node *Node) Save() (err error) {
	//jsonPath := fmt.Sprintf("%s/%s.json", node.Path(), node.Id)
	//os.Remove(jsonPath)
	//n, err := node.ToJson(); if err != nil { return }
	//err = ioutil.WriteFile(jsonPath, []byte(n), 0644); if err != nil { return }

	db, err := DBConnect()
	if err != nil {
		return
	}
	defer db.Close()

	bsonPath := fmt.Sprintf("%s/%s.bson", node.Path(), node.Id)
	os.Remove(bsonPath)
	nbson, err := bson.Marshal(node)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(bsonPath, nbson, 0644)
	if err != nil {
		return
	}
	err = db.Upsert(node)
	if err != nil {
		return
	}
	return
}
Пример #4
0
Файл: bsson.go Проект: rif/gocmd
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)
}
Пример #5
0
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)
}
Пример #6
0
// SetMeta changes the optional "metadata" field associated with the
// file. The meaning of keys under that field is user-defined.
// For example:
//
//     file.SetMeta(bson.M{"inode": inode})
//
// It is a runtime error to call this function when the file is not open
// for writing.
func (file *GridFile) SetMeta(metadata interface{}) {
	file.assertMode(gfsWriting)
	data, err := bson.Marshal(metadata)
	file.m.Lock()
	if err != nil && file.err == nil {
		file.err = err
	} else {
		file.doc.Metadata = &bson.Raw{Data: data}
	}
	file.m.Unlock()
}
Пример #7
0
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)
}
Пример #8
0
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 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)

}
Пример #10
0
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
}
Пример #11
0
func (file *GridFile) insertChunk(data []byte) {
	n := file.chunk
	file.chunk++
	debugf("GridFile %p: adding to checksum: %q", file, string(data))
	file.wsum.Write(data)

	for file.doc.ChunkSize*file.wpending >= 1024*1024 {
		// Hold on.. we got a MB pending.
		file.c.Wait()
		if file.err != nil {
			return
		}
	}

	file.wpending++

	debugf("GridFile %p: inserting chunk %d with %d bytes", file, n, len(data))

	// We may not own the memory of data, so rather than
	// simply copying it, we'll marshal the document ahead of time.
	data, err := bson.Marshal(gfsChunk{bson.NewObjectId(), file.doc.Id, n, data})
	if err != nil {
		file.err = err
		return
	}

	go func() {
		err := file.gfs.Chunks.Insert(bson.Raw{Data: data})
		file.m.Lock()
		file.wpending--
		if err != nil && file.err == nil {
			file.err = err
		}
		file.c.Broadcast()
		file.m.Unlock()
	}()
}
Пример #12
0
func (node *Node) Save() (err error) {
	db, err := DBConnect()
	if err != nil {
		return
	}
	defer db.Close()

	node.UpdateVersion()
	bsonPath := fmt.Sprintf("%s/%s.bson", node.Path(), node.Id)
	os.Remove(bsonPath)
	nbson, err := bson.Marshal(node)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(bsonPath, nbson, 0644)
	if err != nil {
		return
	}
	err = db.Upsert(node)
	if err != nil {
		return
	}
	return
}
Пример #13
0
func main() {
	bson, _ := bson.Marshal(bson.M{"hello": "world"})

	fmt.Printf("%q\n", bson)
}