예제 #1
0
파일: marshal.go 프로젝트: joao-parana/csfw
// 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
}
예제 #2
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)
		}
	}
}
예제 #3
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)
	}()
}
예제 #4
0
파일: marshal.go 프로젝트: joao-parana/csfw
// 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
}