...
1
2
3
4
5
6
7 package bsoncodec
8
9 import (
10 "fmt"
11 "reflect"
12
13 "go.mongodb.org/mongo-driver/bson/bsonoptions"
14 "go.mongodb.org/mongo-driver/bson/bsonrw"
15 "go.mongodb.org/mongo-driver/bson/bsontype"
16 )
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 type ByteSliceCodec struct {
37
38
39
40
41
42 EncodeNilAsEmpty bool
43 }
44
45 var (
46 defaultByteSliceCodec = NewByteSliceCodec()
47
48
49
50
51 _ typeDecoder = defaultByteSliceCodec
52 )
53
54
55
56
57
58 func NewByteSliceCodec(opts ...*bsonoptions.ByteSliceCodecOptions) *ByteSliceCodec {
59 byteSliceOpt := bsonoptions.MergeByteSliceCodecOptions(opts...)
60 codec := ByteSliceCodec{}
61 if byteSliceOpt.EncodeNilAsEmpty != nil {
62 codec.EncodeNilAsEmpty = *byteSliceOpt.EncodeNilAsEmpty
63 }
64 return &codec
65 }
66
67
68 func (bsc *ByteSliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
69 if !val.IsValid() || val.Type() != tByteSlice {
70 return ValueEncoderError{Name: "ByteSliceEncodeValue", Types: []reflect.Type{tByteSlice}, Received: val}
71 }
72 if val.IsNil() && !bsc.EncodeNilAsEmpty && !ec.nilByteSliceAsEmpty {
73 return vw.WriteNull()
74 }
75 return vw.WriteBinary(val.Interface().([]byte))
76 }
77
78 func (bsc *ByteSliceCodec) decodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
79 if t != tByteSlice {
80 return emptyValue, ValueDecoderError{
81 Name: "ByteSliceDecodeValue",
82 Types: []reflect.Type{tByteSlice},
83 Received: reflect.Zero(t),
84 }
85 }
86
87 var data []byte
88 var err error
89 switch vrType := vr.Type(); vrType {
90 case bsontype.String:
91 str, err := vr.ReadString()
92 if err != nil {
93 return emptyValue, err
94 }
95 data = []byte(str)
96 case bsontype.Symbol:
97 sym, err := vr.ReadSymbol()
98 if err != nil {
99 return emptyValue, err
100 }
101 data = []byte(sym)
102 case bsontype.Binary:
103 var subtype byte
104 data, subtype, err = vr.ReadBinary()
105 if err != nil {
106 return emptyValue, err
107 }
108 if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld {
109 return emptyValue, decodeBinaryError{subtype: subtype, typeName: "[]byte"}
110 }
111 case bsontype.Null:
112 err = vr.ReadNull()
113 case bsontype.Undefined:
114 err = vr.ReadUndefined()
115 default:
116 return emptyValue, fmt.Errorf("cannot decode %v into a []byte", vrType)
117 }
118 if err != nil {
119 return emptyValue, err
120 }
121
122 return reflect.ValueOf(data), nil
123 }
124
125
126 func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
127 if !val.CanSet() || val.Type() != tByteSlice {
128 return ValueDecoderError{Name: "ByteSliceDecodeValue", Types: []reflect.Type{tByteSlice}, Received: val}
129 }
130
131 elem, err := bsc.decodeType(dc, vr, tByteSlice)
132 if err != nil {
133 return err
134 }
135
136 val.Set(elem)
137 return nil
138 }
139
View as plain text