-
Notifications
You must be signed in to change notification settings - Fork 399
/
bson.go
311 lines (264 loc) · 7.29 KB
/
bson.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package bson implements encoding and decoding of BSON as defined by https://bsonspec.org/spec.html.
//
// # Types
//
// The following BSON types are supported:
//
// BSON Go
//
// Document/Object *bson.Document or bson.RawDocument
// Array *bson.Array or bson.RawArray
//
// Double float64
// String string
// Binary data bson.Binary
// ObjectId bson.ObjectID
// Boolean bool
// Date time.Time
// Null bson.NullType
// Regular Expression bson.Regex
// 32-bit integer int32
// Timestamp bson.Timestamp
// 64-bit integer int64
//
// Composite types (Document and Array) are passed by pointers.
// Raw composite type and scalars are passed by values.
package bson
import (
"fmt"
"time"
"github.com/cristalhq/bson/bsonproto"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
type (
// ScalarType represents a BSON scalar type.
//
// CString is not included as it is not a real BSON type.
ScalarType = bsonproto.ScalarType
// Binary represents BSON scalar type binary.
Binary = bsonproto.Binary
// BinarySubtype represents BSON Binary's subtype.
BinarySubtype = bsonproto.BinarySubtype
// NullType represents BSON scalar type null.
NullType = bsonproto.NullType
// ObjectID represents BSON scalar type ObjectID.
ObjectID = bsonproto.ObjectID
// Regex represents BSON scalar type regular expression.
Regex = bsonproto.Regex
// Timestamp represents BSON scalar type timestamp.
Timestamp = bsonproto.Timestamp
)
// Null represents BSON scalar value null.
var Null = bsonproto.Null
//go:generate ../../bin/stringer -linecomment -type decodeMode
// decodeMode represents a mode for decoding BSON.
type decodeMode int
const (
_ decodeMode = iota
// DecodeShallow represents a mode in which only top-level fields/elements are decoded;
// nested documents and arrays are converted to RawDocument and RawArray respectively,
// using raw's subslices without copying.
decodeShallow
// DecodeDeep represents a mode in which nested documents and arrays are decoded recursively;
// RawDocuments and RawArrays are never returned.
decodeDeep
)
var (
// ErrDecodeShortInput is returned wrapped by Decode functions if the input bytes slice is too short.
ErrDecodeShortInput = bsonproto.ErrDecodeShortInput
// ErrDecodeInvalidInput is returned wrapped by Decode functions if the input bytes slice is invalid.
ErrDecodeInvalidInput = bsonproto.ErrDecodeInvalidInput
)
// SizeCString returns a size of the encoding of v cstring in bytes.
func SizeCString(s string) int {
return bsonproto.SizeCString(s)
}
// EncodeCString encodes cstring value v into b.
//
// Slice must be at least len(v)+1 ([SizeCString]) bytes long; otherwise, EncodeString will panic.
// Only b[0:len(v)+1] bytes are modified.
func EncodeCString(b []byte, v string) {
bsonproto.EncodeCString(b, v)
}
// DecodeCString decodes cstring value from b.
//
// If there is not enough bytes, DecodeCString will return a wrapped [ErrDecodeShortInput].
// If the input is otherwise invalid, a wrapped [ErrDecodeInvalidInput] is returned.
func DecodeCString(b []byte) (string, error) {
return bsonproto.DecodeCString(b)
}
// Type represents a BSON type.
type Type interface {
ScalarType | CompositeType
}
// CompositeType represents a BSON composite type (including raw types).
type CompositeType interface {
*Document | *Array | RawDocument | RawArray
}
// validBSONType checks if v is a valid BSON type (including raw types).
func validBSONType(v any) error {
switch v := v.(type) {
case *Document:
case RawDocument:
case *Array:
case RawArray:
case float64:
case string:
case Binary:
case ObjectID:
case bool:
case time.Time:
case NullType:
case Regex:
case int32:
case Timestamp:
case int64:
default:
return lazyerrors.Errorf("invalid BSON type %T", v)
}
return nil
}
// convertToTypes converts valid BSON value of that package to types package type.
//
// Conversions of composite types (including raw types) may cause errors.
// Invalid types cause panics.
func convertToTypes(v any) (any, error) {
switch v := v.(type) {
case *Document:
doc, err := v.Convert()
if err != nil {
return nil, lazyerrors.Error(err)
}
return doc, nil
case RawDocument:
d, err := v.Decode()
if err != nil {
return nil, lazyerrors.Error(err)
}
doc, err := d.Convert()
if err != nil {
return nil, lazyerrors.Error(err)
}
return doc, nil
case *Array:
arr, err := v.Convert()
if err != nil {
return nil, lazyerrors.Error(err)
}
return arr, nil
case RawArray:
a, err := v.Decode()
if err != nil {
return nil, lazyerrors.Error(err)
}
arr, err := a.Convert()
if err != nil {
return nil, lazyerrors.Error(err)
}
return arr, nil
case float64:
return v, nil
case string:
return v, nil
case Binary:
// Special case to prevent it from being stored as null in sjson.
// TODO https://github.com/FerretDB/FerretDB/issues/260
if v.B == nil {
v.B = []byte{}
}
return types.Binary{
B: v.B,
Subtype: types.BinarySubtype(v.Subtype),
}, nil
case ObjectID:
return types.ObjectID(v), nil
case bool:
return v, nil
case time.Time:
return v, nil
case NullType:
return types.Null, nil
case Regex:
return types.Regex{
Pattern: v.Pattern,
Options: v.Options,
}, nil
case int32:
return v, nil
case Timestamp:
return types.Timestamp(v), nil
case int64:
return v, nil
default:
panic(fmt.Sprintf("invalid BSON type %T", v))
}
}
// Convert converts valid types package values to BSON values of that package.
//
// Conversions of composite types may cause errors.
func Convert[T types.Type](v T) (any, error) {
return convertFromTypes(v)
}
// convertFromTypes is a variant of [Convert] without type parameters (generics).
//
// Invalid types cause panics.
func convertFromTypes(v any) (any, error) {
switch v := v.(type) {
case *types.Document:
doc, err := ConvertDocument(v)
if err != nil {
return nil, lazyerrors.Error(err)
}
return doc, nil
case *types.Array:
arr, err := ConvertArray(v)
if err != nil {
return nil, lazyerrors.Error(err)
}
return arr, nil
case float64:
return v, nil
case string:
return v, nil
case types.Binary:
return Binary{
B: v.B,
Subtype: BinarySubtype(v.Subtype),
}, nil
case types.ObjectID:
return ObjectID(v), nil
case bool:
return v, nil
case time.Time:
return v, nil
case types.NullType:
return Null, nil
case types.Regex:
return Regex{
Pattern: v.Pattern,
Options: v.Options,
}, nil
case int32:
return v, nil
case types.Timestamp:
return Timestamp(v), nil
case int64:
return v, nil
default:
panic(fmt.Sprintf("invalid type %T", v))
}
}