-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
321 lines (279 loc) · 6.59 KB
/
value.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
312
313
314
315
316
317
318
319
320
321
package bsonex
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"strconv"
"time"
gbson "github.com/globalsign/mgo/bson"
"github.com/sbunce/bson"
)
type (
ValueType = byte
ObjectId = gbson.ObjectId
RegEx = gbson.RegEx
DBPointer = gbson.DBPointer
MongoTimestamp = gbson.MongoTimestamp
M = gbson.M
)
var (
Undefined = gbson.Undefined
MinKey = gbson.MinKey
MaxKey = gbson.MaxKey
NewObjectId = gbson.NewObjectId
NewMongoTimestamp = gbson.NewMongoTimestamp
Marshal = gbson.Marshal
Unmarshal = gbson.Unmarshal
NewEncoder = gbson.NewEncoder
ReadOne = bson.ReadOne
)
const (
TypeEmpty ValueType = iota
TypeDouble // 0x01 64-bit binary floating point
TypeString // 0x02 UTF-8 string
TypeDocument // 0x03 Embedded document
TypeArray // 0x04
TypeBinary // 0x05 Binary data
TypeUndefined // 0x06 Undefined (value) — Deprecated
TypeObjectId // 0x07
TypeBoolean // 0x08
TypeDatetime // 0x09 UTC datetime
TypeNull // 0x0A
TypeRegex // 0x0B Regular expression
TypeDBPointer // 0x0C Deprecated
TypeJSCode // 0x0D
TypeSymbol // 0x0E Deprecated
TypeJSCodeScope // 0x0F Deprecated
TypeInt32 // 0x10
TypeTimestamp // 0x11
TypeInt64 // 0x12
TypeDecimal128 // 0x13 128-bit decimal floating point
TypeMinKey byte = 0xFF
TypeMaxKey byte = 0x7F
)
type Value struct {
valueType ValueType
valueData []byte
}
func (v Value) IsEmpty() bool {
return v.valueType == TypeEmpty
}
func (v Value) Type() ValueType {
return v.valueType
}
func (v Value) RawValue() []byte {
return v.valueData
}
func (v Value) checkType(expects ...byte) {
for _, e := range expects {
if e == v.valueType {
return
}
}
panic(fmt.Sprintf("invalid type, expect: %v, real: %v",
expects, v.valueType))
}
func (v Value) checkValueLength(expect int) {
if expect != len(v.valueData) {
panic(fmt.Sprintf("invalid length, expect: %v, real: %v",
expect, len(v.valueData)))
}
}
func (v Value) Uint64() uint64 {
if len(v.valueData) == 0 {
return 0
}
if v.valueType == TypeInt32 {
return uint64(v.Uint32())
}
v.checkValueLength(8)
return binary.LittleEndian.Uint64(v.valueData)
}
func (v Value) Int64() int64 {
if v.valueType == TypeInt32 {
return int64(v.Int32())
}
return int64(v.Uint64())
}
func (v Value) Uint32() uint32 {
if len(v.valueData) == 0 {
return 0
}
v.checkType(TypeInt32)
v.checkValueLength(4)
return binary.LittleEndian.Uint32(v.valueData)
}
func (v Value) Int32() int32 {
return int32(v.Uint32())
}
func (v Value) Float64() float64 {
if len(v.valueData) == 0 {
return 0
}
v.checkType(TypeDouble)
return math.Float64frombits(v.Uint64())
}
func (v Value) Str() string {
if len(v.valueData) == 0 {
return ""
}
v.checkType(TypeString)
return string(v.valueData[4 : len(v.valueData)-1])
}
func (v Value) String() string {
b, err := json.Marshal(v.Value())
if err != nil {
return fmt.Sprint(v.Value())
}
return string(b)
}
func (v Value) Document() BSON {
v.checkType(TypeDocument, TypeArray)
return BSON(v.valueData)
}
func (v Value) Map() M {
return v.Document().Map()
}
func (v Value) ValueMap() map[string]Value {
return v.Document().ToValueMap()
}
func (v Value) Array() (a []interface{}) {
v.checkType(TypeArray)
return BSON(v.valueData).Array()
}
func (v Value) ValueArray() (a []Value) {
v.checkType(TypeArray)
return BSON(v.valueData).ToValueArray()
}
func (v Value) ArrayOf(i int) Value {
v.checkType(TypeArray)
return BSON(v.valueData).Lookup(strconv.Itoa(i))
}
func (v Value) Objid() ObjectId {
v.checkType(TypeObjectId)
return ObjectId(v.valueData)
}
func (v Value) Bool() bool {
if len(v.valueData) == 0 {
return false
}
v.checkType(TypeBoolean)
return v.valueData[0] == 0x1
}
func (v Value) Time() time.Time {
if len(v.valueData) == 0 {
return time.Time{}
}
v.checkType(TypeDatetime)
v.checkValueLength(8)
ns := v.Int64() * int64(time.Millisecond)
return time.Unix(ns/1e9, ns%1e9)
}
func (v Value) Regexp() RegEx {
if len(v.valueData) == 0 {
return RegEx{}
}
v.checkType(TypeRegex)
i := bytes.IndexByte(v.valueData, 0x00)
return RegEx{
Pattern: string(v.valueData[:i]),
Options: string(v.valueData[i+1 : len(v.valueData)-1]),
}
}
func (v Value) DBPointer() DBPointer {
v.checkType(TypeDBPointer)
return DBPointer{
Namespace: string(v.valueData[4 : len(v.valueData)-13]),
Id: ObjectId(v.valueData[len(v.valueData)-12:]),
}
}
func (v Value) MongoTimestamp() MongoTimestamp {
v.checkType(TypeTimestamp)
return MongoTimestamp(v.Int64())
}
func (v Value) IsNull() bool {
return v.valueType == TypeNull
}
func (v Value) IsUndefined() bool {
return v.valueType == TypeUndefined
}
func (v Value) IsMinKey() bool {
return v.valueType == TypeMinKey
}
func (v Value) IsMaxKey() bool {
return v.valueType == TypeMaxKey
}
func (v Value) Binary() Binary {
if len(v.valueData) == 0 {
return Binary{}
}
v.checkType(TypeBinary)
return Binary{gbson.Binary{
Kind: v.valueData[4],
Data: v.valueData[5:],
}}
}
func (v Value) MarshalJSON() (bs []byte, err error) {
return json.Marshal(v.Value())
}
func (v Value) MarshalBSON() (bs []byte, err error) {
return v.valueData, nil
}
func (v Value) GetBSON() (interface{}, error) {
return v.Value(), nil
}
func (v Value) Value() (r interface{}) {
switch v.valueType {
case TypeDouble:
return v.Float64()
case TypeString:
return v.Str()
case TypeDocument:
return v.Map()
case TypeArray:
return v.Array()
case TypeBinary:
return v.Binary()
case TypeUndefined:
return Undefined
case TypeObjectId:
return v.Objid()
case TypeBoolean:
return v.Bool()
case TypeDatetime:
return v.Time()
case TypeNull:
return nil
case TypeRegex:
return v.Regexp()
case TypeDBPointer:
return v.DBPointer()
case TypeJSCode, TypeSymbol, TypeJSCodeScope, TypeDecimal128:
panic("not supported")
case TypeInt32:
return v.Int32()
case TypeTimestamp:
return v.MongoTimestamp()
case TypeInt64:
return v.Int64()
case TypeMinKey:
return MinKey
case TypeMaxKey:
return MaxKey
default:
panic(fmt.Sprintf("invalid bson type %#v", v.valueType))
}
}
type Binary struct {
gbson.Binary
}
func (b Binary) MarshalJSON() (bs []byte, err error) {
s := base64.StdEncoding.EncodeToString(b.Data)
return json.Marshal(s)
}
func (v Binary) GetBSON() (interface{}, error) {
return v.Binary.Data, nil
}