当前位置: 首页>>代码示例>>Golang>>正文


Golang bson.Unmarshal函数代码示例

本文整理汇总了Golang中launchpad/net/gobson/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, Bug("Pointer not initialized (%#v)", expected))
				c.Assert(field.received, Equals, expected)
			}
		}
	}
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:30,代码来源:gobson_test.go

示例2: TestUnmarshalMapDocumentTooShort

func (s *S) TestUnmarshalMapDocumentTooShort(c *C) {
	for _, data := range corruptedData {
		err := bson.Unmarshal([]byte(data), bson.M{})
		c.Assert(err, Matches, "Document is corrupted")

		err = bson.Unmarshal([]byte(data), &struct{}{})
		c.Assert(err, Matches, "Document is corrupted")
	}
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:9,代码来源:gobson_test.go

示例3: 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:iron-io,项目名称:gobson_temp,代码行数:8,代码来源:gobson_test.go

示例4: GetInfo

// GetInfo unmarshals the optional metadata associated with the file
// into the result parameter.  For example:
//
//     result := struct{ INode int }{}
//     err = file.GetInfo(&result)
//     if err != nil {
//         panic(err.String())
//     }
//     fmt.Printf("inode: %d\n", result.INode)
//
func (file *GridFile) GetInfo(result interface{}) (err os.Error) {
	file.m.Lock()
	if file.doc.Metadata != nil {
		err = bson.Unmarshal(file.doc.Metadata.Data, result)
	}
	file.m.Unlock()
	return
}
开发者ID:adamsxu,项目名称:mgo,代码行数:18,代码来源:gridfs.go

示例5: 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, Equals, &struct{ Ptr *byte }{nil})
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:8,代码来源:gobson_test.go

示例6: 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, Equals, item.obj, Bug("Failed on item %d: %#v", i, item))
	}
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:8,代码来源:gobson_test.go

示例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, Equals, item.obj,
			Bug("Failed on item %d", i))
	}
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:9,代码来源:gobson_test.go

示例8: testCrossPair

func testCrossPair(c *C, dump interface{}, load interface{}, bug interface{}) {
	//c.Logf("")
	zero := makeZeroDoc(load)
	data, err := bson.Marshal(dump)
	c.Assert(err, IsNil, bug)
	c.Logf("Data: %#v", string(data))
	err = bson.Unmarshal(data, zero)
	c.Assert(err, IsNil, bug)
	c.Assert(zero, Equals, load, bug)
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:10,代码来源:gobson_test.go

示例9: TestUnmarshalErrorItems

func (s *S) TestUnmarshalErrorItems(c *C) {
	for _, item := range unmarshalErrorItems {
		data := []byte(wrapInDoc(item.data))
		var value interface{}
		switch reflect.ValueOf(item.obj).Kind() {
		case reflect.Map, reflect.Ptr:
			value = makeZeroDoc(item.obj)
		case reflect.Invalid:
			value = bson.M{}
		default:
			value = item.obj
		}
		err := bson.Unmarshal(data, value)
		c.Assert(err, Matches, item.error)
	}
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:16,代码来源:gobson_test.go

示例10: resetNonce

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 os.Error, reply *replyOp, docNum int, docData []byte) {
		if err != nil {
			socket.kill(os.NewError("getNonce: " + err.String()))
			return
		}
		result := &getNonceResult{}
		err = bson.Unmarshal(docData, &result)
		if err != nil {
			socket.kill(os.NewError("Failed to unmarshal nonce: " + err.String()))
			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(os.NewError(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(os.NewError("resetNonce: " + err.String()))
	}
}
开发者ID:adamsxu,项目名称:mgo,代码行数:45,代码来源:auth.go

示例11: TestUnmarshalSetterErrors

func (s *S) TestUnmarshalSetterErrors(c *C) {
	boom := os.NewError("BOOM")
	setterResult["2"] = boom
	defer func() {
		setterResult["2"] = nil, false
	}()

	m := map[string]*setterType{}
	data := wrapInDoc("\x02abc\x00\x02\x00\x00\x001\x00" +
		"\x02def\x00\x02\x00\x00\x002\x00" +
		"\x02ghi\x00\x02\x00\x00\x003\x00")
	err := bson.Unmarshal([]byte(data), m)
	c.Assert(err, Equals, boom)
	c.Assert(m["abc"], NotNil)
	c.Assert(m["def"], IsNil)
	c.Assert(m["ghi"], IsNil)

	c.Assert(m["abc"].received, Equals, "1")
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:19,代码来源:gobson_test.go

示例12: main

func main() {
	var inter [4]interface{}
	in1 := 1
	in2 := [...]int{1, 2, 3}
	in3 := []byte("womengde")
	in4 := "dfdfdfd"

	inter[0] = in1
	inter[1] = in2
	inter[2] = in3
	inter[3] = in4

	data, _ := bson.Marshal(inter)

	fmt.Printf("%#v\n", data)
	bson.Unmarshal(data, inter)

	fmt.Printf("%#v\n", inter)

}
开发者ID:notedit,项目名称:code-snippets,代码行数:20,代码来源:test_interface.go

示例13: TestUnmarshalSetterOmits

func (s *S) TestUnmarshalSetterOmits(c *C) {
	setterResult["2"] = &bson.TypeError{}
	setterResult["4"] = &bson.TypeError{}
	defer func() {
		setterResult["2"] = nil, false
		setterResult["4"] = nil, false
	}()

	m := map[string]*setterType{}
	data := wrapInDoc("\x02abc\x00\x02\x00\x00\x001\x00" +
		"\x02def\x00\x02\x00\x00\x002\x00" +
		"\x02ghi\x00\x02\x00\x00\x003\x00" +
		"\x02jkl\x00\x02\x00\x00\x004\x00")
	err := bson.Unmarshal([]byte(data), m)
	c.Assert(err, IsNil)
	c.Assert(m["abc"], NotNil)
	c.Assert(m["def"], IsNil)
	c.Assert(m["ghi"], NotNil)
	c.Assert(m["jkl"], IsNil)

	c.Assert(m["abc"].received, Equals, "1")
	c.Assert(m["ghi"].received, Equals, "3")
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:23,代码来源:gobson_test.go

示例14: readLoop

// Estimated minimum cost per socket: 1 goroutine + memory for the largest
// document ever seen.
func (socket *mongoSocket) readLoop() {
	p := [36]byte{}[:] // 16 from header + 20 from OP_REPLY fixed fields
	s := [4]byte{}[:]
	conn := socket.conn // No locking, conn never changes.
	for {
		// XXX Handle timeouts, , etc
		err := fill(conn, p)
		if err != nil {
			socket.kill(err)
			return
		}

		totalLen := getInt32(p, 0)
		responseTo := getInt32(p, 8)
		opCode := getInt32(p, 12)

		// Don't use socket.server.Addr here.  socket is not locked.
		debugf("Socket %p to %s: got reply (%d bytes)", socket, socket.addr, totalLen)

		_ = totalLen

		if opCode != 1 {
			socket.kill(os.NewError("opcode != 1, corrupted data?"))
			return
		}

		reply := replyOp{
			flags:     uint32(getInt32(p, 16)),
			cursorId:  getInt64(p, 20),
			firstDoc:  getInt32(p, 28),
			replyDocs: getInt32(p, 32),
		}

		stats.receivedOps(+1)
		stats.receivedDocs(int(reply.replyDocs))

		socket.Lock()
		replyFunc, replyFuncFound := socket.replyFuncs[uint32(responseTo)]
		socket.Unlock()

		if replyFunc != nil && reply.replyDocs == 0 {
			replyFunc(nil, &reply, -1, nil)
		} else {
			for i := 0; i != int(reply.replyDocs); i++ {
				err := fill(conn, s)
				if err != nil {
					socket.kill(err)
					return
				}

				b := make([]byte, int(getInt32(s, 0)))

				// copy(b, s) in an efficient way.
				b[0] = s[0]
				b[1] = s[1]
				b[2] = s[2]
				b[3] = s[3]

				err = fill(conn, b[4:])
				if err != nil {
					socket.kill(err)
					return
				}

				if globalDebug && globalLogger != nil {
					m := bson.M{}
					if err := bson.Unmarshal(b, m); err == nil {
						debugf("Socket %p to %s: received document: %#v", socket, socket.addr, m)
					}
				}

				if replyFunc != nil {
					replyFunc(nil, &reply, i, b)
				}

				// XXX Do bound checking against totalLen.
			}
		}

		// Only remove replyFunc after iteration, so that kill() will see it.
		socket.Lock()
		if replyFuncFound {
			socket.replyFuncs[uint32(responseTo)] = replyFunc, false
		}
		socket.Unlock()

		// XXX Do bound checking against totalLen.
	}
}
开发者ID:adamsxu,项目名称:mgo,代码行数:91,代码来源:socket.go

示例15: TestUnmarshalWholeDocumentWithSetter

func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
	obj := &setterType{}
	err := bson.Unmarshal([]byte(sampleItems[0].data), obj)
	c.Assert(err, IsNil)
	c.Assert(obj.received, Equals, bson.M{"hello": "world"})
}
开发者ID:iron-io,项目名称:gobson_temp,代码行数:6,代码来源:gobson_test.go


注:本文中的launchpad/net/gobson/bson.Unmarshal函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。