summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/common/json/mongo_extjson.go
blob: 606b1368b3c16fc9056b5d62c69ccead4497b899 (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package json

import (
	"fmt"
	"gopkg.in/mgo.v2/bson"
	"reflect"
)

// Represents base-64 encoded binary data
type BinData struct {
	Type   byte
	Base64 string
}

// Represents the number of milliseconds since the Unix epoch.
type Date int64

type ISODate string

type ObjectId string

// Represents a reference to another document.
type DBRef struct {
	Collection string
	Id         interface{}
	Database   string // optional
}

// Refers to a document in some namespace by wrapping a string containing the namespace
// and the objectId in which the _id of the document is contained
type DBPointer struct {
	Namespace string
	Id        bson.ObjectId
}

// Represents the literal MinKey.
type MinKey struct{}

// Represents the literal MaxKey.
type MaxKey struct{}

// Represents a signed 32-bit integer.
type NumberInt int32

// Represents a signed 64-bit integer.
type NumberLong int64

// Represents a signed 64-bit float.
type NumberFloat float64

type Decimal128 struct {
	bson.Decimal128
}

// Represents a regular expression.
type RegExp struct {
	Pattern string
	Options string
}

// Represents a timestamp value.
type Timestamp struct {
	Seconds   uint32
	Increment uint32
}

type JavaScript struct {
	Code  string
	Scope interface{}
}

type Float float64

// Represents the literal undefined.
type Undefined struct{}

var (
	// primitive types
	byteType   = reflect.TypeOf(byte(0))
	stringType = reflect.TypeOf(string(""))
	uint32Type = reflect.TypeOf(uint32(0))

	// object types
	binDataType     = reflect.TypeOf(BinData{})
	dateType        = reflect.TypeOf(Date(0))
	isoDateType     = reflect.TypeOf(ISODate(""))
	dbRefType       = reflect.TypeOf(DBRef{})
	dbPointerType   = reflect.TypeOf(DBPointer{})
	maxKeyType      = reflect.TypeOf(MaxKey{})
	minKeyType      = reflect.TypeOf(MinKey{})
	numberIntType   = reflect.TypeOf(NumberInt(0))
	numberLongType  = reflect.TypeOf(NumberLong(0))
	numberFloatType = reflect.TypeOf(NumberFloat(0))
	objectIdType    = reflect.TypeOf(ObjectId(""))
	regexpType      = reflect.TypeOf(RegExp{})
	timestampType   = reflect.TypeOf(Timestamp{})
	undefinedType   = reflect.TypeOf(Undefined{})
	orderedBSONType = reflect.TypeOf(bson.D{})
	interfaceType   = reflect.TypeOf((*interface{})(nil))
)

func (d Date) isFormatable() bool {
	return int64(d) < int64(32535215999000)
}

func stateBeginExtendedValue(s *scanner, c int) int {
	switch c {
	case 'u': // beginning of undefined
		s.step = stateU
	case 'B': // beginning of BinData or Boolean
		s.step = stateB
	case 'D': // beginning of Date
		s.step = stateD
	case 'I': // beginning of Infinity or ISODate
		s.step = stateI
	case 'M': // beginning of MinKey or MaxKey
		s.step = stateM
	case 'N': // beginning of NaN or NumberXX
		s.step = stateUpperN
	case 'O': // beginning of ObjectId
		s.step = stateO
	case 'R': // beginning of RegExp
		s.step = stateR
	case 'T': // beginning of Timestamp
		s.step = stateUpperT
	case '/': // beginning of /foo/i
		s.step = stateInRegexpPattern
	default:
		return s.error(c, "looking for beginning of value")
	}

	return scanBeginLiteral
}

// stateB is the state after reading `B`.
func stateB(s *scanner, c int) int {
	if c == 'i' {
		s.step = stateBi
		return scanContinue
	}
	if c == 'o' {
		s.step = stateBo
		return scanContinue
	}
	return s.error(c, "in literal BinData or Boolean (expecting 'i' or 'o')")
}

// stateUpperN is the state after reading `N`.
func stateUpperN(s *scanner, c int) int {
	if c == 'a' {
		s.step = stateUpperNa
		return scanContinue
	}
	if c == 'u' {
		s.step = stateUpperNu
		return scanContinue
	}
	return s.error(c, "in literal NaN or Number (expecting 'a' or 'u')")
}

// stateM is the state after reading `M`.
func stateM(s *scanner, c int) int {
	if c == 'a' {
		s.step = stateUpperMa
		return scanContinue
	}
	if c == 'i' {
		s.step = stateUpperMi
		return scanContinue
	}
	return s.error(c, "in literal MaxKey or MinKey (expecting 'a' or 'i')")
}

// stateD is the state after reading `D`.
func stateD(s *scanner, c int) int {
	switch c {
	case 'a':
		s.step = stateDa
	case 'B':
		s.step = stateDB
	case 'b':
		s.step = stateDb
	default:
		return s.error(c, "in literal Date or DBRef (expecting 'a' or 'B')")
	}
	return scanContinue
}

// stateDB is the state after reading `DB`.
func stateDB(s *scanner, c int) int {
	if c == 'R' {
		s.step = stateDBR
		return scanContinue
	}
	if c == 'P' {
		s.step = stateDBP
		return scanContinue
	}
	return s.error(c, "in state DB (expecting 'R or P')")
}

// stateI is the state after reading `I`.
func stateI(s *scanner, c int) int {
	switch c {
	case 'n':
		s.step = stateIn
	case 'S':
		s.step = stateIS
	default:
		return s.error(c, "in literal Infinity or ISO (expecting 'n' or 'S')")
	}
	return scanContinue
}

// Decodes a literal stored in item into v.
func (d *decodeState) storeExtendedLiteral(item []byte, v reflect.Value, fromQuoted bool) bool {
	switch c := item[0]; c {
	case 'n':
		d.storeNewLiteral(v, fromQuoted)

	case 'u': // undefined
		switch kind := v.Kind(); kind {
		case reflect.Interface:
			v.Set(reflect.ValueOf(Undefined{}))
		default:
			d.error(fmt.Errorf("cannot store %v value into %v type", undefinedType, kind))
		}

	case 'B': // BinData or Boolean
		switch item[1] {
		case 'i': // BinData
			d.storeBinData(v)
		case 'o': // Boolean
			d.storeBoolean(v)
		}
	case 'D': // Date, DBRef, DBPointer, Dbpointer,or Dbref
		switch item[1] {
		case 'a': // Date
			d.storeDate(v)
		case 'b': // Dbref
			d.storeDBRef(v)
		case 'B': // DBRef or DBPointer
			switch item[2] {
			case 'R': //DBRef
				d.storeDBRef(v)
			case 'P': //DBPointer
				d.storeDBPointer(v)
			}
		}
	case 'I':
		switch item[1] {
		case 'S': // ISODate
			d.storeISODate(v)
		}

	case 'M': // MinKey or MaxKey
		switch item[1] {
		case 'i': // MinKey
			switch kind := v.Kind(); kind {
			case reflect.Interface:
				v.Set(reflect.ValueOf(MinKey{}))
			default:
				d.error(fmt.Errorf("cannot store %v value into %v type", minKeyType, kind))
			}
		case 'a': // MaxKey
			switch kind := v.Kind(); kind {
			case reflect.Interface:
				v.Set(reflect.ValueOf(MaxKey{}))
			default:
				d.error(fmt.Errorf("cannot store %v value into %v type", maxKeyType, kind))
			}
		}

	case 'O': // ObjectId
		d.storeObjectId(v)

	case 'N': // NumberInt or NumberLong
		switch item[6] {
		case 'I': // NumberInt
			d.storeNumberInt(v)
		case 'L': // NumberLong
			d.storeNumberLong(v)
		}

	case 'R': // RegExp constructor
		d.storeRegexp(v)

	case 'T': // Timestamp
		d.storeTimestamp(v)

	case '/': // regular expression literal
		op := d.scanWhile(scanSkipSpace)
		if op != scanRegexpPattern {
			d.error(fmt.Errorf("expected beginning of regular expression pattern"))
		}

		pattern, options, err := d.regexp()
		if err != nil {
			d.error(err)
		}
		switch kind := v.Kind(); kind {
		case reflect.Interface:
			v.Set(reflect.ValueOf(RegExp{pattern, options}))
		default:
			d.error(fmt.Errorf("cannot store %v value into %v type", regexpType, kind))
		}

	default:
		return false
	}

	return true
}

// Returns a literal from the underlying byte data.
func (d *decodeState) getExtendedLiteral(item []byte) (interface{}, bool) {
	switch c := item[0]; c {
	case 'n':
		return d.getNewLiteral(), true

	case 'u': // undefined
		return Undefined{}, true

	case 'B': // BinData or Boolean
		switch item[1] {
		case 'i': // BinData
			return d.getBinData(), true
		case 'o': // Boolean
			return d.getBoolean(), true
		}

	case 'D': // Date, DBRef, or Dbref
		switch item[1] {
		case 'a': // Date
			return d.getDate(), true
		case 'b': // Dbref
			return d.getDBRef(), true
		case 'B': // DBRef or DBPoiner
			switch item[2] {
			case 'R': // DBRef
				return d.getDBRef(), true
			case 'P': // DBPointer
				return d.getDBPointer(), true
			}
		}

	case 'M': // MinKey or MaxKey
		switch item[1] {
		case 'i': // MinKey
			return MinKey{}, true
		case 'a': // MaxKey
			return MaxKey{}, true
		}

	case 'O': // ObjectId
		return d.getObjectId(), true

	case 'N': // NumberInt or NumberLong
		switch item[6] {
		case 'I': // NumberInt
			return d.getNumberInt(), true
		case 'L': // NumberLong
			return d.getNumberLong(), true
		}

	case 'R': // RegExp constructor
		return d.getRegexp(), true

	case 'T': // Timestamp
		return d.getTimestamp(), true

	case 'I': // ISO Date
		switch item[1] {
		case 'S': // ISODate
			return d.getDate(), true
		}

	case '/': // regular expression literal
		op := d.scanWhile(scanSkipSpace)
		if op != scanRegexpPattern {
			d.error(fmt.Errorf("expected beginning of regular expression pattern"))
		}

		pattern, options, err := d.regexp()
		if err != nil {
			d.error(err)
		}
		return RegExp{pattern, options}, true
	}

	return nil, false
}