Skip to content

Commit f43e267

Browse files
committed
Allow decoding top level into interface{}
GODRIVER-710 Change-Id: Id67ea6d4e8a6ee2968dc8b6acf282618715d1e03
1 parent 986b204 commit f43e267

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

bson/bsoncodec/default_value_decoders.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder) {
9999
RegisterTypeMapEntry(bsontype.Timestamp, tTimestamp).
100100
RegisterTypeMapEntry(bsontype.Decimal128, tDecimal).
101101
RegisterTypeMapEntry(bsontype.MinKey, tMinKey).
102-
RegisterTypeMapEntry(bsontype.MaxKey, tMaxKey)
102+
RegisterTypeMapEntry(bsontype.MaxKey, tMaxKey).
103+
RegisterTypeMapEntry(bsontype.Type(0), tD)
103104
}
104105

105106
// BooleanDecodeValue is the ValueDecoderFunc for bool types.

bson/bsoncodec/default_value_decoders_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,18 @@ func TestDefaultValueDecoders(t *testing.T) {
27692769
t.Errorf("Errors are not equal. got %v; want %v", got, want)
27702770
}
27712771
})
2772+
t.Run("top level document", func(t *testing.T) {
2773+
data := bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))
2774+
vr := bsonrw.NewBSONDocumentReader(data)
2775+
want := primitive.D{{"pi", 3.14159}}
2776+
var got interface{}
2777+
val := reflect.ValueOf(&got).Elem()
2778+
err := dvd.EmptyInterfaceDecodeValue(DecodeContext{Registry: buildDefaultRegistry()}, vr, val)
2779+
noerr(t, err)
2780+
if !cmp.Equal(got, want) {
2781+
t.Errorf("Did not get correct result. got %v; want %v", got, want)
2782+
}
2783+
})
27722784
})
27732785
}
27742786

0 commit comments

Comments
 (0)