File: objectid.go

package info (click to toggle)
mongo-tools 3.4.14-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,612 kB
  • sloc: python: 9,281; asm: 4,229; ansic: 1,606; sh: 817; makefile: 67; cpp: 59; lisp: 19
file content (55 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download | duplicates (2)
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
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)
}