本文整理汇总了Golang中github.com/skynetservices/mgo/bson.Marshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Marshal函数的具体用法?Golang Marshal怎么用?Golang Marshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Marshal函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestGetterErrors
func (s *S) TestGetterErrors(c *C) {
e := errors.New("oops")
obj1 := &docWithGetterField{}
obj1.Field = &typeWithGetter{sampleItems[0].obj, e}
data, err := bson.Marshal(obj1)
c.Assert(err, ErrorMatches, "oops")
c.Assert(data, IsNil)
obj2 := &typeWithGetter{sampleItems[0].obj, e}
data, err = bson.Marshal(obj2)
c.Assert(err, ErrorMatches, "oops")
c.Assert(data, IsNil)
}
开发者ID:gs412,项目名称:mgo,代码行数:14,代码来源:bson_test.go
示例2: TestMarshalAllItems
func (s *S) TestMarshalAllItems(c *C) {
for i, item := range allItems {
data, err := bson.Marshal(item.obj)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, wrapInDoc(item.data), Commentf("Failed on item %d: %#v", i, item))
}
}
开发者ID:gs412,项目名称:mgo,代码行数:7,代码来源:bson_test.go
示例3: TestMarshalOneWayItems
func (s *S) TestMarshalOneWayItems(c *C) {
for _, item := range marshalItems {
data, err := bson.Marshal(item.obj)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, wrapInDoc(item.data))
}
}
开发者ID:gs412,项目名称:mgo,代码行数:7,代码来源:bson_test.go
示例4: TestMarshalErrorItems
func (s *S) TestMarshalErrorItems(c *C) {
for _, item := range marshalErrorItems {
data, err := bson.Marshal(item.obj)
c.Assert(err, ErrorMatches, item.data)
c.Assert(data, IsNil)
}
}
开发者ID:gs412,项目名称:mgo,代码行数:7,代码来源:bson_test.go
示例5: TestMarshalSampleItems
func (s *S) TestMarshalSampleItems(c *C) {
for i, item := range sampleItems {
data, err := bson.Marshal(item.obj)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, item.data, Commentf("Failed on item %d", i))
}
}
开发者ID:gs412,项目名称:mgo,代码行数:7,代码来源:bson_test.go
示例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})
}
开发者ID:gs412,项目名称:mgo,代码行数:8,代码来源:bson_test.go
示例7: 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)
}
开发者ID:gs412,项目名称:mgo,代码行数:8,代码来源:bson_test.go
示例8: Encode
func (e *Encoder) Encode(v interface{}) (err error) {
buf, err := bson.Marshal(v)
if err != nil {
return
}
_, err = e.w.Write(buf)
return
}
开发者ID:smarinskaya,项目名称:skynet,代码行数:9,代码来源:bsoncoders.go
示例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)
}
开发者ID:gs412,项目名称:mgo,代码行数:10,代码来源:bson_test.go
示例10: addBSON
func addBSON(b []byte, doc interface{}) ([]byte, error) {
if doc == nil {
return append(b, 5, 0, 0, 0, 0), nil
}
data, err := bson.Marshal(doc)
if err != nil {
return b, err
}
return append(b, data...), nil
}
开发者ID:hfeeki,项目名称:mgo,代码行数:10,代码来源:socket.go
示例11: SetMeta
// 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()
}
开发者ID:hfeeki,项目名称:mgo,代码行数:19,代码来源:gridfs.go
示例12: 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})
}
开发者ID:gs412,项目名称:mgo,代码行数:11,代码来源:bson_test.go
示例13: 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
}
开发者ID:gs412,项目名称:mgo,代码行数:11,代码来源:flusher.go
示例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)
}
开发者ID:gs412,项目名称:mgo,代码行数:12,代码来源:bson_test.go
示例15: TestMarshalAllItemsWithGetter
func (s *S) TestMarshalAllItemsWithGetter(c *C) {
for i, item := range allItems {
if item.data == "" {
continue
}
obj := &docWithGetterField{}
obj.Field = &typeWithGetter{result: item.obj.(bson.M)["_"]}
data, err := bson.Marshal(obj)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, wrapInDoc(item.data),
Commentf("Failed on item #%d", i))
}
}
开发者ID:gs412,项目名称:mgo,代码行数:13,代码来源:bson_test.go
示例16: 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)
}
}
开发者ID:gs412,项目名称:mgo,代码行数:14,代码来源:bson_test.go
示例17: 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)
}
}
开发者ID:gs412,项目名称:mgo,代码行数:14,代码来源:bson_test.go
示例18: 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)
}
开发者ID:gs412,项目名称:mgo,代码行数:15,代码来源:bson_test.go
示例19: TestServiceRPCBasic
func TestServiceRPCBasic(t *testing.T) {
var addr net.Addr
config := &skynet.ServiceConfig{}
service := CreateService(EchoRPC{}, config)
service.ClientInfo = make(map[string]ClientInfo, 1)
addr = &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 123,
}
service.ClientInfo["123"] = ClientInfo{
Address: addr,
}
srpc := NewServiceRPC(service)
in := M{"Hi": "there"}
out := &M{}
sin := skynet.ServiceRPCIn{
RequestInfo: &skynet.RequestInfo{
RequestID: "id",
OriginAddress: addr.String(),
ConnectionAddress: addr.String(),
},
Method: "Foo",
ClientID: "123",
}
sin.In, _ = bson.Marshal(in)
sout := skynet.ServiceRPCOut{}
err := srpc.Forward(sin, &sout)
if err != nil {
t.Error(err)
}
bson.Unmarshal(sout.Out, out)
if v, ok := (*out)["Hi"].(string); !ok || v != "there" {
t.Error(fmt.Sprintf("Expected %v, got %v", in, *out))
}
}
开发者ID:smarinskaya,项目名称:skynet,代码行数:46,代码来源:servicerpc_test.go
示例20: sendToInstance
// ServiceClient.sendToInstance() tries to make an RPC request on a particular connection to an instance
func (c *ServiceClient) sendToInstance(sr ServiceResource,
requestInfo *skynet.RequestInfo, funcName string, in interface{}) (
result []byte, serviceErr, err error) {
ts("sendToInstance", requestInfo)
defer te("sendToInstance", requestInfo)
sr.service.FetchStats(c.client.doozer())
dbgf("stats: %+v\n", sr.service.Stats)
sin := skynet.ServiceRPCIn{
RequestInfo: requestInfo,
Method: funcName,
ClientID: sr.clientID,
}
sin.In, err = bson.Marshal(in)
if err != nil {
err = fmt.Errorf("Error calling bson.Marshal: %v", err)
return
}
sout := skynet.ServiceRPCOut{}
err = sr.rpcClient.Call(sr.service.Config.Name+".Forward", sin, &sout)
if err != nil {
sr.Close()
dbg("(sr.rpcClient.Call)", err)
// Log failure
c.Log.Error("Error calling sr.rpcClient.Call: " + err.Error())
}
if sout.ErrString != "" {
serviceErr = serviceError{sout.ErrString}
}
result = sout.Out
return
}
开发者ID:smarinskaya,项目名称:skynet,代码行数:41,代码来源:serviceclient.go
注:本文中的github.com/skynetservices/mgo/bson.Marshal函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论