Exemplo n.º 1
0
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)
			}
		}
	}
}
Exemplo n.º 2
0
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")
	}
}
Exemplo n.º 3
0
// 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)
}
Exemplo n.º 4
0
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
}
Exemplo n.º 5
0
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
}
Exemplo n.º 6
0
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)
}
Exemplo n.º 7
0
func main() {
	if err := bson.Unmarshal(in, &out); err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", out)
}
Exemplo n.º 8
0
func BenchmarkDecodingBsonTweetStruct(b *testing.B) {
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		tw := Tweet{}
		bson.Unmarshal(bsonTweet, &tw)
	}
}
Exemplo n.º 9
0
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
}
Exemplo n.º 10
0
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
}
Exemplo n.º 11
0
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))
}
Exemplo n.º 12
0
func BenchmarkDecodingBsonTweet(b *testing.B) {
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		var tw map[string]interface{}
		bson.Unmarshal(bsonTweet, &tw)
	}
}
Exemplo n.º 13
0
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()
}
Exemplo n.º 14
0
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
}
Exemplo n.º 15
0
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)
}
Exemplo n.º 16
0
func (s *URLSuite) TestBSON(c *C) {
	type doc struct {
		URL *charm.URL
	}
	url := charm.MustParseURL("cs:series/name")
	data, err := bson.Marshal(doc{url})
	c.Assert(err, IsNil)
	var v doc
	err = bson.Unmarshal(data, &v)
	c.Assert(v.URL, DeepEquals, url)

	data, err = bson.Marshal(doc{})
	c.Assert(err, IsNil)
	err = bson.Unmarshal(data, &v)
	c.Assert(v.URL, IsNil)
}
Exemplo n.º 17
0
func (c *ServiceClient) send(retry, giveup time.Duration, ri *skynet.RequestInfo, fn string, in interface{}, out interface{}) (err error) {
	attempts := make(chan sendAttempt)

	var ticker <-chan time.Time
	if retry > 0 {
		ticker = time.NewTicker(retry).C
	}

	var timeout <-chan time.Time
	if giveup > 0 {
		timeout = time.NewTimer(giveup).C
	}

	doneSignal := make(chan bool)
	attemptCount := 1

	defer func() {
		go func() {
			for i := 0; i < attemptCount; i++ {
				doneSignal <- true
			}
		}()
	}()

	go c.attemptSend(doneSignal, attempts, ri, fn, in)

	for {
		select {
		case <-ticker:
			attemptCount++
			go c.attemptSend(doneSignal, attempts, ri, fn, in)
		case <-timeout:
			if err == nil {
				err = ErrRequestTimeout
			}
			// otherwise use the last error reported from an attempt
			return
		case attempt := <-attempts:
			err = attempt.err
			if err != nil {
				if _, ok := err.(serviceError); !ok {
					// error during transmition, abort this attempt
					if giveup == 0 {
						return
					}
					continue
				}
			}

			unmarshallerr := bson.Unmarshal(attempt.result, out)
			if unmarshallerr != nil {
				err = unmarshallerr
			}
			return
		}
	}

	return
}
Exemplo n.º 18
0
// specs: inbound params use json
func UnmarshalIn(d []byte) (v bson.M, err error) {
	err = bson.Unmarshal(d, &v)
	if err != nil {
		log.Error("mongo.unmarshalIn: %s -> %s", d, err)
	}

	return
}
Exemplo n.º 19
0
// 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
}
Exemplo n.º 20
0
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, DeepEquals, &struct{ Ptr *byte }{nil})
}
Exemplo n.º 21
0
// Partially unmarshal incoming data to unpack event name.
func (_ *BSONProtocol) Unpack(data []byte) (string, interface{}, error) {
	rawMsg := &RawIncomingBSONMessage{}
	err := bson.Unmarshal(data, rawMsg)
	if err != nil {
		return "", nil, err
	}
	return rawMsg.Event, &rawMsg.Data, nil
}
Exemplo n.º 22
0
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)
}
Exemplo n.º 23
0
// Json,Interface{} to Bson by BsonBytes
func Json2Bson(i interface{}) (m *bson.M, err error) {
	b, err := bson.Marshal(i)
	if isError(err) {
		return
	}
	err = bson.Unmarshal(b, &m)
	return
}
Exemplo n.º 24
0
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})
}
Exemplo n.º 25
0
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, DeepEquals, item.obj, Commentf("Failed on item %d: %#v", i, item))
	}
}
Exemplo n.º 26
0
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, DeepEquals, item.obj, Commentf("Failed on item %d", i))
	}
}
Exemplo n.º 27
0
func (p *WirePkt) Decode() (obj interface{}, err error) {
	switch p.Type {
	case TypeNop:
		if p.Length != 0 {
			/* throw error later... */
			return nil, ErrMalformedMessage
		}
		return nil, nil
	case TypeIdentifyClient:
		ic := new(IdentifyClient)
		err := bson.Unmarshal(p.Payload[0:p.Length], ic)
		if err != nil {
			return nil, err
		}
		return ic, nil
	case TypeReadyForTask:
		if p.Length != 0 {
			/* throw error later... */
			return nil, ErrMalformedMessage
		}
		return nil, nil
	case TypeTaskRequest:
		tr := new(TaskRequest)
		err := bson.Unmarshal(p.Payload[0:p.Length], tr)
		if err != nil {
			return nil, err
		}
		return tr, nil
	case TypeTaskResponse:
		tr := new(TaskResponse)
		err := bson.Unmarshal(p.Payload[0:p.Length], tr)
		if err != nil {
			return nil, err
		}
		return tr, nil
	case TypeAcknowledgement:
		tr := new(Acknowledgement)
		err := bson.Unmarshal(p.Payload[0:p.Length], tr)
		if err != nil {
			return nil, err
		}
		return tr, nil
	}
	return nil, ErrUnknownMessage
}
Exemplo n.º 28
0
func (s *S) TestMarshalWithGetterNil(c *C) {
	obj := docWithGetterField{}
	data, err := bson.Marshal(obj)
	c.Assert(err, IsNil)
	m := bson.M{}
	err = bson.Unmarshal(data, m)
	c.Assert(err, IsNil)
	c.Assert(m, DeepEquals, bson.M{"_": "<value is nil>"})
}
Exemplo n.º 29
0
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)
}
Exemplo n.º 30
0
func (s *marshalSuite) TestUnmarshalNilRoundtrip(c *gc.C) {
	// We have a custom unmarshaller that should keep
	// the field unset when it finds a nil value.
	var v struct{ Tools *tools.Tools }
	data, err := bson.Marshal(&v)
	c.Assert(err, gc.IsNil)
	err = bson.Unmarshal(data, &v)
	c.Assert(err, gc.IsNil)
	c.Assert(v.Tools, gc.IsNil)
}