1
2
3
4
5
6
7 package bson
8
9 import (
10 "bytes"
11 "errors"
12 "fmt"
13 "reflect"
14 "time"
15
16 "go.mongodb.org/mongo-driver/bson/bsoncodec"
17 "go.mongodb.org/mongo-driver/bson/bsonrw"
18 "go.mongodb.org/mongo-driver/bson/bsontype"
19 "go.mongodb.org/mongo-driver/bson/primitive"
20 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
21 )
22
23
24 var ErrNilContext = errors.New("DecodeContext cannot be nil")
25
26
27 var ErrNilRegistry = errors.New("Registry cannot be nil")
28
29
30
31
32
33 type RawValue struct {
34 Type bsontype.Type
35 Value []byte
36
37 r *bsoncodec.Registry
38 }
39
40
41
42 func (rv RawValue) IsZero() bool {
43 return rv.Type == 0x00 && len(rv.Value) == 0
44 }
45
46
47
48
49
50 func (rv RawValue) Unmarshal(val interface{}) error {
51 reg := rv.r
52 if reg == nil {
53 reg = DefaultRegistry
54 }
55 return rv.UnmarshalWithRegistry(reg, val)
56 }
57
58
59 func (rv RawValue) Equal(rv2 RawValue) bool {
60 if rv.Type != rv2.Type {
61 return false
62 }
63
64 if !bytes.Equal(rv.Value, rv2.Value) {
65 return false
66 }
67
68 return true
69 }
70
71
72
73 func (rv RawValue) UnmarshalWithRegistry(r *bsoncodec.Registry, val interface{}) error {
74 if r == nil {
75 return ErrNilRegistry
76 }
77
78 vr := bsonrw.NewBSONValueReader(rv.Type, rv.Value)
79 rval := reflect.ValueOf(val)
80 if rval.Kind() != reflect.Ptr {
81 return fmt.Errorf("argument to Unmarshal* must be a pointer to a type, but got %v", rval)
82 }
83 rval = rval.Elem()
84 dec, err := r.LookupDecoder(rval.Type())
85 if err != nil {
86 return err
87 }
88 return dec.DecodeValue(bsoncodec.DecodeContext{Registry: r}, vr, rval)
89 }
90
91
92
93 func (rv RawValue) UnmarshalWithContext(dc *bsoncodec.DecodeContext, val interface{}) error {
94 if dc == nil {
95 return ErrNilContext
96 }
97
98 vr := bsonrw.NewBSONValueReader(rv.Type, rv.Value)
99 rval := reflect.ValueOf(val)
100 if rval.Kind() != reflect.Ptr {
101 return fmt.Errorf("argument to Unmarshal* must be a pointer to a type, but got %v", rval)
102 }
103 rval = rval.Elem()
104 dec, err := dc.LookupDecoder(rval.Type())
105 if err != nil {
106 return err
107 }
108 return dec.DecodeValue(*dc, vr, rval)
109 }
110
111 func convertFromCoreValue(v bsoncore.Value) RawValue { return RawValue{Type: v.Type, Value: v.Data} }
112 func convertToCoreValue(v RawValue) bsoncore.Value {
113 return bsoncore.Value{Type: v.Type, Data: v.Value}
114 }
115
116
117 func (rv RawValue) Validate() error { return convertToCoreValue(rv).Validate() }
118
119
120 func (rv RawValue) IsNumber() bool { return convertToCoreValue(rv).IsNumber() }
121
122
123
124 func (rv RawValue) String() string { return convertToCoreValue(rv).String() }
125
126
127
128 func (rv RawValue) DebugString() string { return convertToCoreValue(rv).DebugString() }
129
130
131
132 func (rv RawValue) Double() float64 { return convertToCoreValue(rv).Double() }
133
134
135 func (rv RawValue) DoubleOK() (float64, bool) { return convertToCoreValue(rv).DoubleOK() }
136
137
138
139
140
141
142 func (rv RawValue) StringValue() string { return convertToCoreValue(rv).StringValue() }
143
144
145
146 func (rv RawValue) StringValueOK() (string, bool) { return convertToCoreValue(rv).StringValueOK() }
147
148
149
150 func (rv RawValue) Document() Raw { return Raw(convertToCoreValue(rv).Document()) }
151
152
153
154 func (rv RawValue) DocumentOK() (Raw, bool) {
155 doc, ok := convertToCoreValue(rv).DocumentOK()
156 return Raw(doc), ok
157 }
158
159
160
161 func (rv RawValue) Array() Raw { return Raw(convertToCoreValue(rv).Array()) }
162
163
164
165 func (rv RawValue) ArrayOK() (Raw, bool) {
166 doc, ok := convertToCoreValue(rv).ArrayOK()
167 return Raw(doc), ok
168 }
169
170
171
172 func (rv RawValue) Binary() (subtype byte, data []byte) { return convertToCoreValue(rv).Binary() }
173
174
175
176 func (rv RawValue) BinaryOK() (subtype byte, data []byte, ok bool) {
177 return convertToCoreValue(rv).BinaryOK()
178 }
179
180
181
182 func (rv RawValue) ObjectID() primitive.ObjectID { return convertToCoreValue(rv).ObjectID() }
183
184
185
186 func (rv RawValue) ObjectIDOK() (primitive.ObjectID, bool) {
187 return convertToCoreValue(rv).ObjectIDOK()
188 }
189
190
191
192 func (rv RawValue) Boolean() bool { return convertToCoreValue(rv).Boolean() }
193
194
195
196 func (rv RawValue) BooleanOK() (bool, bool) { return convertToCoreValue(rv).BooleanOK() }
197
198
199
200 func (rv RawValue) DateTime() int64 { return convertToCoreValue(rv).DateTime() }
201
202
203
204 func (rv RawValue) DateTimeOK() (int64, bool) { return convertToCoreValue(rv).DateTimeOK() }
205
206
207
208 func (rv RawValue) Time() time.Time { return convertToCoreValue(rv).Time() }
209
210
211
212 func (rv RawValue) TimeOK() (time.Time, bool) { return convertToCoreValue(rv).TimeOK() }
213
214
215
216 func (rv RawValue) Regex() (pattern, options string) { return convertToCoreValue(rv).Regex() }
217
218
219
220 func (rv RawValue) RegexOK() (pattern, options string, ok bool) {
221 return convertToCoreValue(rv).RegexOK()
222 }
223
224
225
226 func (rv RawValue) DBPointer() (string, primitive.ObjectID) {
227 return convertToCoreValue(rv).DBPointer()
228 }
229
230
231
232 func (rv RawValue) DBPointerOK() (string, primitive.ObjectID, bool) {
233 return convertToCoreValue(rv).DBPointerOK()
234 }
235
236
237
238 func (rv RawValue) JavaScript() string { return convertToCoreValue(rv).JavaScript() }
239
240
241
242 func (rv RawValue) JavaScriptOK() (string, bool) { return convertToCoreValue(rv).JavaScriptOK() }
243
244
245
246 func (rv RawValue) Symbol() string { return convertToCoreValue(rv).Symbol() }
247
248
249
250 func (rv RawValue) SymbolOK() (string, bool) { return convertToCoreValue(rv).SymbolOK() }
251
252
253
254 func (rv RawValue) CodeWithScope() (string, Raw) {
255 code, scope := convertToCoreValue(rv).CodeWithScope()
256 return code, Raw(scope)
257 }
258
259
260
261 func (rv RawValue) CodeWithScopeOK() (string, Raw, bool) {
262 code, scope, ok := convertToCoreValue(rv).CodeWithScopeOK()
263 return code, Raw(scope), ok
264 }
265
266
267
268 func (rv RawValue) Int32() int32 { return convertToCoreValue(rv).Int32() }
269
270
271
272 func (rv RawValue) Int32OK() (int32, bool) { return convertToCoreValue(rv).Int32OK() }
273
274
275
276
277
278
279 func (rv RawValue) AsInt32() int32 { return convertToCoreValue(rv).AsInt32() }
280
281
282
283
284
285
286 func (rv RawValue) AsInt32OK() (int32, bool) { return convertToCoreValue(rv).AsInt32OK() }
287
288
289
290 func (rv RawValue) Timestamp() (t, i uint32) { return convertToCoreValue(rv).Timestamp() }
291
292
293
294 func (rv RawValue) TimestampOK() (t, i uint32, ok bool) { return convertToCoreValue(rv).TimestampOK() }
295
296
297
298 func (rv RawValue) Int64() int64 { return convertToCoreValue(rv).Int64() }
299
300
301
302 func (rv RawValue) Int64OK() (int64, bool) { return convertToCoreValue(rv).Int64OK() }
303
304
305
306 func (rv RawValue) AsInt64() int64 { return convertToCoreValue(rv).AsInt64() }
307
308
309
310 func (rv RawValue) AsInt64OK() (int64, bool) { return convertToCoreValue(rv).AsInt64OK() }
311
312
313
314 func (rv RawValue) Decimal128() primitive.Decimal128 { return convertToCoreValue(rv).Decimal128() }
315
316
317
318 func (rv RawValue) Decimal128OK() (primitive.Decimal128, bool) {
319 return convertToCoreValue(rv).Decimal128OK()
320 }
321
View as plain text