Skip to content

Commit

Permalink
bson: EncodeField bug fix
Browse files Browse the repository at this point in the history
Also added more tests for Skip function.
  • Loading branch information
sougou committed Mar 4, 2014
1 parent b4bbd06 commit 3828b8b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
100 changes: 100 additions & 0 deletions go/bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,96 @@ var customUnmarshalCases = []struct {
Null,
func(buf *bytes.Buffer, kind byte) interface{} { return DecodeArray(buf, kind) },
[]interface{}(nil),
}, {
"Number->Skip",
"\x00\x00\x00\x00\x00\x00\xf0?",
Number,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"String->Skip",
"\x05\x00\x00\x00test\x00",
String,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Object->Skip",
"\x14\x00\x00\x00\x05Val2\x00\x04\x00\x00\x00\x00test\x00",
Object,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Object->Skip with null element",
"\v\x00\x00\x00\nVal2\x00\x00",
Object,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Array->Skip",
"\x11\x00\x00\x00\x050\x00\x04\x00\x00\x00\x00test\x00",
Array,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Array->Skip with null element",
"\x13\x00\x00\x00\n0\x00\x121\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
Array,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Binary->Skip",
"\x04\x00\x00\x00\x00test",
Binary,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Boolean->Skip",
"\x01",
Boolean,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Datetime->Skip",
"\x88\xf2\\\x8d\b\x01\x00\x00",
Datetime,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Int->Skip",
"\x01\x00\x00\x00",
Int,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Long->Skip",
"\x01\x00\x00\x00\x00\x00\x00\x00",
Long,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Ulong->Skip",
"\x01\x00\x00\x00\x00\x00\x00\x00",
Ulong,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Null->Skip",
"",
Null,
func(buf *bytes.Buffer, kind byte) interface{} { Skip(buf, kind); return nil },
nil,
}, {
"Null->map[string]interface{}",
"",
Null,
func(buf *bytes.Buffer, kind byte) interface{} { return DecodeMap(buf, kind) },
map[string]interface{}(nil),
}, {
"Null->[]interface{}",
"",
Null,
func(buf *bytes.Buffer, kind byte) interface{} { return DecodeArray(buf, kind) },
[]interface{}(nil),
}, {
"Array->[]string",
"\x1f\x00\x00\x00\x050\x00\x05\x00\x00\x00\x00test1\x051\x00\x05\x00\x00\x00\x00test2\x00",
Expand Down Expand Up @@ -1087,6 +1177,16 @@ func TestCustomUnmarshalFailures(t *testing.T) {
}
}

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)
}
}

type alltypes struct {
Bytes []byte
Float64 float64
Expand Down
5 changes: 5 additions & 0 deletions go/bson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func EncodeField(buf *bytes2.ChunkedWriter, key string, val interface{}) {
}

func encodeField(buf *bytes2.ChunkedWriter, key string, val reflect.Value) {
// nil interfaces show up as invalid
if !val.IsValid() {
EncodePrefix(buf, Null, key)
return
}
if marshaler := canMarshal(val); marshaler != nil {
marshaler.MarshalBson(buf, key)
return
Expand Down
4 changes: 2 additions & 2 deletions go/bson/unmarshal_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func SkipIndex(buf *bytes.Buffer) {
func ReadCString(buf *bytes.Buffer) string {
index := bytes.IndexByte(buf.Bytes(), 0)
if index < 0 {
panic(NewBsonError("Unexpected EOF"))
panic(NewBsonError("unexpected EOF"))
}
// Read including null termination, but
// return the string without the null.
Expand All @@ -341,7 +341,7 @@ func ReadCString(buf *bytes.Buffer) string {
func Next(buf *bytes.Buffer, n int) []byte {
b := buf.Next(n)
if len(b) != n {
panic(NewBsonError("Unexpected EOF"))
panic(NewBsonError("unexpected EOF"))
}
return b[:n:n]
}
Expand Down

0 comments on commit 3828b8b

Please sign in to comment.