@@ -15,14 +15,17 @@ import (
15
15
"go.mongodb.org/mongo-driver/bson/bsontype"
16
16
)
17
17
18
- var defaultByteSliceCodec = NewByteSliceCodec ()
19
-
20
18
// ByteSliceCodec is the Codec used for []byte values.
21
19
type ByteSliceCodec struct {
22
20
EncodeNilAsEmpty bool
23
21
}
24
22
25
- var _ ValueCodec = & ByteSliceCodec {}
23
+ var (
24
+ defaultByteSliceCodec = NewByteSliceCodec ()
25
+
26
+ _ ValueCodec = defaultByteSliceCodec
27
+ _ typeDecoder = defaultByteSliceCodec
28
+ )
26
29
27
30
// NewByteSliceCodec returns a StringCodec with options opts.
28
31
func NewByteSliceCodec (opts ... * bsonoptions.ByteSliceCodecOptions ) * ByteSliceCodec {
@@ -45,10 +48,13 @@ func (bsc *ByteSliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter,
45
48
return vw .WriteBinary (val .Interface ().([]byte ))
46
49
}
47
50
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
+ }
52
58
}
53
59
54
60
var data []byte
@@ -57,34 +63,49 @@ func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader,
57
63
case bsontype .String :
58
64
str , err := vr .ReadString ()
59
65
if err != nil {
60
- return err
66
+ return emptyValue , err
61
67
}
62
68
data = []byte (str )
63
69
case bsontype .Symbol :
64
70
sym , err := vr .ReadSymbol ()
65
71
if err != nil {
66
- return err
72
+ return emptyValue , err
67
73
}
68
74
data = []byte (sym )
69
75
case bsontype .Binary :
70
76
var subtype byte
71
77
data , subtype , err = vr .ReadBinary ()
72
78
if err != nil {
73
- return err
79
+ return emptyValue , err
74
80
}
75
81
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" }
77
83
}
78
84
case bsontype .Null :
79
- val .Set (reflect .Zero (val .Type ()))
80
- return vr .ReadNull ()
85
+ err = vr .ReadNull ()
81
86
case bsontype .Undefined :
82
- val .Set (reflect .Zero (val .Type ()))
83
- return vr .ReadUndefined ()
87
+ err = vr .ReadUndefined ()
84
88
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
86
107
}
87
108
88
- val .Set (reflect . ValueOf ( data ) )
109
+ val .Set (elem )
89
110
return nil
90
111
}
0 commit comments