Skip to content

Commit ca59ef9

Browse files
author
Divjot Arora
authored
GODRIVER-1681 Add typeDecoder implementations for non-recursive types (#448)
1 parent a9223ce commit ca59ef9

File tree

6 files changed

+753
-345
lines changed

6 files changed

+753
-345
lines changed

bson/bsoncodec/byte_slice_codec.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ import (
1515
"go.mongodb.org/mongo-driver/bson/bsontype"
1616
)
1717

18-
var defaultByteSliceCodec = NewByteSliceCodec()
19-
2018
// ByteSliceCodec is the Codec used for []byte values.
2119
type ByteSliceCodec struct {
2220
EncodeNilAsEmpty bool
2321
}
2422

25-
var _ ValueCodec = &ByteSliceCodec{}
23+
var (
24+
defaultByteSliceCodec = NewByteSliceCodec()
25+
26+
_ ValueCodec = defaultByteSliceCodec
27+
_ typeDecoder = defaultByteSliceCodec
28+
)
2629

2730
// NewByteSliceCodec returns a StringCodec with options opts.
2831
func NewByteSliceCodec(opts ...*bsonoptions.ByteSliceCodecOptions) *ByteSliceCodec {
@@ -45,10 +48,13 @@ func (bsc *ByteSliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter,
4548
return vw.WriteBinary(val.Interface().([]byte))
4649
}
4750

48-
// DecodeValue is the ValueDecoder for []byte.
49-
func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
50-
if !val.CanSet() || val.Type() != tByteSlice {
51-
return ValueDecoderError{Name: "ByteSliceDecodeValue", Types: []reflect.Type{tByteSlice}, Received: val}
51+
func (bsc *ByteSliceCodec) decodeType(dc DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
52+
if t != tByteSlice {
53+
return emptyValue, ValueDecoderError{
54+
Name: "ByteSliceDecodeValue",
55+
Types: []reflect.Type{tByteSlice},
56+
Received: reflect.Zero(t),
57+
}
5258
}
5359

5460
var data []byte
@@ -57,34 +63,49 @@ func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader,
5763
case bsontype.String:
5864
str, err := vr.ReadString()
5965
if err != nil {
60-
return err
66+
return emptyValue, err
6167
}
6268
data = []byte(str)
6369
case bsontype.Symbol:
6470
sym, err := vr.ReadSymbol()
6571
if err != nil {
66-
return err
72+
return emptyValue, err
6773
}
6874
data = []byte(sym)
6975
case bsontype.Binary:
7076
var subtype byte
7177
data, subtype, err = vr.ReadBinary()
7278
if err != nil {
73-
return err
79+
return emptyValue, err
7480
}
7581
if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld {
76-
return fmt.Errorf("ByteSliceDecodeValue can only be used to decode subtype 0x00 or 0x02 for %s, got %v", bsontype.Binary, subtype)
82+
return emptyValue, decodeBinaryError{subtype: subtype, typeName: "[]byte"}
7783
}
7884
case bsontype.Null:
79-
val.Set(reflect.Zero(val.Type()))
80-
return vr.ReadNull()
85+
err = vr.ReadNull()
8186
case bsontype.Undefined:
82-
val.Set(reflect.Zero(val.Type()))
83-
return vr.ReadUndefined()
87+
err = vr.ReadUndefined()
8488
default:
85-
return fmt.Errorf("cannot decode %v into a []byte", vrType)
89+
return emptyValue, fmt.Errorf("cannot decode %v into a []byte", vrType)
90+
}
91+
if err != nil {
92+
return emptyValue, err
93+
}
94+
95+
return reflect.ValueOf(data), nil
96+
}
97+
98+
// DecodeValue is the ValueDecoder for []byte.
99+
func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
100+
if !val.CanSet() || val.Type() != tByteSlice {
101+
return ValueDecoderError{Name: "ByteSliceDecodeValue", Types: []reflect.Type{tByteSlice}, Received: val}
102+
}
103+
104+
elem, err := bsc.decodeType(dc, vr, tByteSlice)
105+
if err != nil {
106+
return err
86107
}
87108

88-
val.Set(reflect.ValueOf(data))
109+
val.Set(elem)
89110
return nil
90111
}

0 commit comments

Comments
 (0)