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


Golang bson.Unmarshal函数代码示例

本文整理汇总了Golang中labix/org/v2/mgo/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)
			}
		}
	}
}
开发者ID:RyanDeng,项目名称:qbot,代码行数:30,代码来源:bson_test.go

示例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")
	}
}
开发者ID:RyanDeng,项目名称:qbot,代码行数:9,代码来源:bson_test.go

示例3: _Bson2Json

// Bson to Json,InterfaceP{} by BsonBytes
// failed
func _Bson2Json(m *bson.M, i interface{}) error {
	b, err := bson.Marshal(m)
	if isError(err) {
		return err
	}
	return bson.Unmarshal(b, &i)
}
开发者ID:everfore,项目名称:bconv,代码行数:9,代码来源:data.go

示例4: Decode

func (d *Decoder) Decode(pv interface{}) (err error) {
	var lbuf [4]byte
	n, err := d.r.Read(lbuf[:])
	if n != 4 {
		err = errors.New(fmt.Sprintf("Corrupted BSON stream: could only read %d", n))
		return
	}
	if err != nil {
		return
	}

	length := (int(lbuf[0]) << 0) |
		(int(lbuf[1]) << 8) |
		(int(lbuf[2]) << 16) |
		(int(lbuf[3]) << 24)

	buf := make([]byte, length)
	copy(buf[0:4], lbuf[:])
	_, err = d.r.Read(buf[4:])
	if err != nil {
		return
	}

	fmt.Printf("decoding: %v\n", buf)

	err = bson.Unmarshal(buf, pv)

	fmt.Printf("decoded: %+v\n", pv)

	return
}
开发者ID:skelterjohn,项目名称:bsonrpc,代码行数:31,代码来源:bsoncoders.go

示例5: GetContest

func GetContest() (*Contest, error) {
	id := int64(0)
	b := []byte{}
	salt := []byte{}
	ready := false
	err := db.QueryRow("SELECT id, struct, salt, ready FROM contests LIMIT 1").
		Scan(&id, &b, &salt, &ready)
	if err == sql.ErrNoRows {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	c := &Contest{}
	if len(b) > 0 {
		err = bson.Unmarshal(b, c)
		if err != nil {
			return nil, err
		}
	}
	c.Id = id
	c.Salt = salt
	c.Ready = ready
	return c, nil
}
开发者ID:rakeen,项目名称:cactus,代码行数:25,代码来源:contest.go

示例6: TestHttpCrashReportPersisterSendsValidBSON

func TestHttpCrashReportPersisterSendsValidBSON(t *testing.T) {
	go func() {
		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			defer r.Body.Close()

			bytes, _ := ioutil.ReadAll(r.Body)

			report := make(map[string]interface{})
			if err := bson.Unmarshal(bytes, report); err != nil {
				http.Error(w, err.Error(), http.StatusBadRequest)
			}
		}))

		server.Close()
	}()

	mi := &machine.MockIdentifier{}
	mi.On("Identify").Return([]byte{42, 42, 42}, nil)

	u, _ := url.Parse("http://localhost:9090")

	persister := HttpReportPersister{*u, mi, &http.Client{}}

	f, _ := os.Open("test_data/test.crash")

	report, err := ParseReport(NewLineReader{f})
	assert.Nil(t, err)

	err = persister.Persist(report)
	assert.Nil(t, err)
}
开发者ID:vosst,项目名称:csi,代码行数:31,代码来源:http_report_persister_test.go

示例7: main

func main() {
	if err := bson.Unmarshal(in, &out); err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", out)
}
开发者ID:justinblah,项目名称:fae,代码行数:7,代码来源:bson.go

示例8: BenchmarkDecodingBsonTweetStruct

func BenchmarkDecodingBsonTweetStruct(b *testing.B) {
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		tw := Tweet{}
		bson.Unmarshal(bsonTweet, &tw)
	}
}
开发者ID:araddon,项目名称:goperf,代码行数:7,代码来源:encoding_test.go

示例9: 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
}
开发者ID:davidsoloman,项目名称:beats,代码行数:34,代码来源:auth.go

示例10: Decode

func (d *Decoder) Decode(pv interface{}) (err error) {
	var lbuf [4]byte
	n, err := d.r.Read(lbuf[:])
	if n == 0 {
		return io.EOF
	}
	if n != 4 {
		return fmt.Errorf("Corrupted BSON stream: could only read %d", n)
	}
	if err != nil {
		return
	}

	length := (int(lbuf[0]) << 0) |
		(int(lbuf[1]) << 8) |
		(int(lbuf[2]) << 16) |
		(int(lbuf[3]) << 24)

	buf := make([]byte, length)
	copy(buf[0:4], lbuf[:])
	n, err = io.ReadFull(d.r, buf[4:])
	if err != nil {
		return
	}
	if n+4 != length {
		return fmt.Errorf("Expected %d bytes, read %d", length, n)
	}

	err = bson.Unmarshal(buf, pv)

	return
}
开发者ID:jbaikge,项目名称:skynet2,代码行数:32,代码来源:bsoncoders.go

示例11: BenchmarkBsonUnmarshal

func BenchmarkBsonUnmarshal(b *testing.B) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	total := 0

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StopTimer()

		p := NewPopulatedPreAccept(popr)

		data, err := bson.Marshal(p)
		if err != nil {
			panic(err)
		}

		total += len(data)

		msg := &PreAccept{}

		b.StartTimer()
		if err := bson.Unmarshal(data, msg); err != nil {
			panic(err)
		}

	}
	b.SetBytes(int64(total / b.N))
}
开发者ID:hongchaodeng,项目名称:serialization-bench,代码行数:27,代码来源:bson_test.go

示例12: BenchmarkDecodingBsonTweet

func BenchmarkDecodingBsonTweet(b *testing.B) {
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		var tw map[string]interface{}
		bson.Unmarshal(bsonTweet, &tw)
	}
}
开发者ID:araddon,项目名称:goperf,代码行数:7,代码来源:encoding_test.go

示例13: TestSend

func TestSend(t *testing.T) {
	client, server := net.Pipe()
	go doServiceHandshake(server, true, t)

	cn, err := NewConnectionFromNetConn("TestRPCService", client)
	c := cn.(*Conn)

	s := rpc.NewServer()
	var ts TestRPCService
	s.Register(&ts)
	go s.ServeCodec(bsonrpc.NewServerCodec(server))

	var tp TestParam
	tp.Val1 = "Hello World"
	tp.Val2 = 10

	ri := &skynet.RequestInfo{}

	ts.TestMethod = func(in skynet.ServiceRPCIn, out *skynet.ServiceRPCOut) (err error) {
		out.Out, err = bson.Marshal(&tp)

		var t TestParam

		if err != nil {
			return
		}

		if in.ClientID != c.clientID {
			return errors.New("Failed to set ClientID on request")
		}

		if in.Method != "Foo" {
			return errors.New("Failed to set Method on request")
		}

		if *in.RequestInfo != *ri {
			return errors.New("Failed to set RequestInfo on request")
		}

		err = bson.Unmarshal(in.In, &t)
		if err != nil {
			return
		}

		if t.Val1 != tp.Val1 || tp.Val2 != tp.Val2 {
			return errors.New("Request failed to send proper data")
		}

		return
	}

	err = c.Send(ri, "Foo", tp, &tp)
	if err != nil {
		t.Error(err)
		return
	}

	c.Close()
	server.Close()
}
开发者ID:bubble66,项目名称:skynet,代码行数:60,代码来源:connection_test.go

示例14: List

func (storage *manageStorage) List(chat string, amountToSkip int) (*[]quotes.Quote, error) {
	col, err := storage.ejdb.GetColl(chat)
	if err != nil {
		return nil, err
	}
	query, err := storage.ejdb.CreateQuery("{}")
	defer query.Del()
	if err != nil {
		return nil, err
	}
	hint := fmt.Sprintf(`{"$orderby":{"when":1}, "$skip": %d, "$max": 10}`, amountToSkip)
	query.SetHints(hint)

	results, err := query.Execute(col)
	if err != nil {
		return nil, err
	}

	res := []quotes.Quote{}
	for _, quoteBytes := range results {
		var quote quotes.Quote
		bson.Unmarshal(quoteBytes, &quote)
		res = append(res, quote)
	}

	return &res, nil
}
开发者ID:graffic,项目名称:wanon,代码行数:27,代码来源:storage.go

示例15: decode

func (c *codec) decode(pv interface{}) (err error) {
	var lbuf [4]byte
	n, err := c.r.Read(lbuf[:])
	if n == 0 {
		err = io.EOF
		return
	}
	if n != 4 {
		err = errors.New(fmt.Sprintf("Corrupted BSON stream: could only read %d", n))
		return
	}
	if err != nil {
		return
	}

	length := (int(lbuf[0]) << 0) |
		(int(lbuf[1]) << 8) |
		(int(lbuf[2]) << 16) |
		(int(lbuf[3]) << 24)

	buf := make([]byte, length)
	copy(buf[0:4], lbuf[:])
	_, err = io.ReadFull(c.r, buf[4:])
	if err != nil {
		return
	}

	return bson.Unmarshal(buf, pv)
}
开发者ID:jmptrader,项目名称:go-bidirpc,代码行数:29,代码来源:bsonrpc.go


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