/
objectid.go
61 lines (53 loc) · 1.64 KB
/
objectid.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
// Copyright (C) MongoDB, Inc. 2014-present.
//
// 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
package json
import (
"fmt"
"reflect"
)
// Transition functions for recognizing ObjectId.
// Adapted from encoding/json/scanner.go.
// stateO is the state after reading `O`.
func stateO(s *scanner, c int) int {
if c == 'b' {
s.step = generateState("ObjectId", []byte("jectId"), stateConstructor)
return scanContinue
}
return s.error(c, "in literal ObjectId (expecting 'b')")
}
// Decodes an ObjectId literal stored in the underlying byte data into v.
func (d *decodeState) storeObjectId(v reflect.Value) {
op := d.scanWhile(scanSkipSpace)
if op != scanBeginCtor {
d.error(fmt.Errorf("expected beginning of constructor"))
}
args, err := d.ctor("ObjectId", []reflect.Type{objectIdType})
if err != nil {
d.error(err)
}
switch kind := v.Kind(); kind {
case reflect.Interface:
v.Set(args[0])
default:
d.error(fmt.Errorf("cannot store %v value into %v type", objectIdType, kind))
}
}
// Returns an ObjectId literal from the underlying byte data.
func (d *decodeState) getObjectId() interface{} {
op := d.scanWhile(scanSkipSpace)
if op != scanBeginCtor {
d.error(fmt.Errorf("expected beginning of constructor"))
}
args := d.ctorInterface()
if err := ctorNumArgsMismatch("ObjectId", 1, len(args)); err != nil {
d.error(err)
}
arg0, ok := args[0].(string)
if !ok {
d.error(fmt.Errorf("expected string for first argument of ObjectId constructor"))
}
return ObjectId(arg0)
}