Esempio n. 1
0
func BenchmarkEncodeInterface(b *testing.B) {
	values := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	for i := 0; i < b.N; i++ {
		buf := bytes2.NewChunkedWriter(2048)
		EncodeInterface(buf, "Val", values)
		buf.Reset()
	}
}
Esempio n. 2
0
// MarshalToStream marshals val into writer.
func MarshalToStream(writer io.Writer, val interface{}) (err error) {
	buf := bytes2.NewChunkedWriter(DefaultBufferSize)
	if err = MarshalToBuffer(buf, val); err != nil {
		return err
	}
	_, err = buf.WriteTo(writer)
	return err
}
Esempio n. 3
0
func TestEncodeFieldNil(t *testing.T) {
	buf := bytes2.NewChunkedWriter(DefaultBufferSize)
	EncodeField(buf, "Val", nil)
	got := string(buf.Bytes())
	want := "\nVal\x00"
	if got != want {
		t.Errorf("nil encode: got %q, want %q", got, want)
	}
}
Esempio n. 4
0
func TestInterfaceMarshal(t *testing.T) {
	for _, tcase := range interfaceMarshalCases {
		buf := bytes2.NewChunkedWriter(DefaultBufferSize)
		EncodeInterface(buf, "Val", tcase.in)
		got := string(buf.Bytes())
		if got != tcase.out {
			t.Errorf("%s: got \n%q, want \n%q", tcase.desc, got, tcase.out)
		}
	}
}
Esempio n. 5
0
func (cc *ClientCodec) WriteRequest(r *rpc.Request, body interface{}) error {
	buf := bytes2.NewChunkedWriter(DefaultBufferSize)
	if err := bson.MarshalToBuffer(buf, &RequestBson{r}); err != nil {
		return err
	}
	if err := bson.MarshalToBuffer(buf, body); err != nil {
		return err
	}
	_, err := buf.WriteTo(cc.rwc)
	return err
}
Esempio n. 6
0
// String prints a readable version of Query, and also truncates
// data if it's too long
func (query *Query) String() string {
	buf := bytes2.NewChunkedWriter(1024)
	fmt.Fprintf(buf, "Sql: %#v, BindVars: {", query.Sql)
	for k, v := range query.BindVariables {
		switch val := v.(type) {
		case []byte:
			fmt.Fprintf(buf, "%s: %#v, ", k, slimit(string(val)))
		case string:
			fmt.Fprintf(buf, "%s: %#v, ", k, slimit(val))
		default:
			fmt.Fprintf(buf, "%s: %v, ", k, v)
		}
	}
	fmt.Fprintf(buf, "}")
	return string(buf.Bytes())
}
Esempio n. 7
0
func TestInterfaceMarshalFailure(t *testing.T) {
	want := "don't know how to marshal chan int"
	func() {
		defer func() {
			if x := recover(); x != nil {
				got := x.(BsonError).Error()
				if got != want {
					t.Errorf("got %s, want %s", got, want)
				}
				return
			}
		}()
		buf := bytes2.NewChunkedWriter(DefaultBufferSize)
		EncodeInterface(buf, "Val", make(chan int))
		t.Errorf("got no error, want %s", want)
	}()
}
Esempio n. 8
0
func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec {
	return &ServerCodec{conn, bytes2.NewChunkedWriter(DefaultBufferSize)}
}
Esempio n. 9
0
// Marshal marshals val into encoded.
func Marshal(val interface{}) (encoded []byte, err error) {
	buf := bytes2.NewChunkedWriter(DefaultBufferSize)
	err = MarshalToBuffer(buf, val)
	return buf.Bytes(), err
}