File: constructor.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 (117 lines) | stat: -rw-r--r-- 2,627 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
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
package json

import (
	"fmt"
	"reflect"
)

const CtorNumArgsErrorf = "expected %v argument%v to %v constructor, but %v received"

// Transition functions for recognizing object constructors.
// Adapted from encoding/json/scanner.go.

// stateConstructor is the state after reading a constructor name.
func stateConstructor(s *scanner, c int) int {
	if c <= ' ' && isSpace(rune(c)) {
		return scanSkipSpace
	}
	if c == '(' {
		s.step = stateBeginCtorOrEmpty
		s.pushParseState(parseCtorArg)
		return scanBeginCtor
	}
	return s.error(c, "expected '('")
}

// stateBeginCtorOrEmpty is the state after reading `(`.
func stateBeginCtorOrEmpty(s *scanner, c int) int {
	if c <= ' ' && isSpace(rune(c)) {
		return scanSkipSpace
	}
	if c == ')' {
		return stateEndValue(s, c)
	}
	return stateBeginValue(s, c)
}

// ctor consumes a constructor from d.data[d.off-1:], given a type specification t.
// the first byte of the constructor ('(') has been read already.
func (d *decodeState) ctor(name string, t []reflect.Type) ([]reflect.Value, error) {
	result := make([]reflect.Value, 0, len(t))

	i := 0
	for {
		// Look ahead for ) - can only happen on first iteration.
		op := d.scanWhile(scanSkipSpace)
		if op == scanEndCtor {
			break
		}

		// Back up so d.value can have the byte we just read.
		d.off--
		d.scan.undo(op)

		if i < len(t) {
			v := reflect.New(t[i]).Elem()

			// Get argument of constructor
			d.value(v)

			result = append(result, v)
			i++
		}

		// Next token must be , or ).
		op = d.scanWhile(scanSkipSpace)
		if op == scanEndCtor {
			break
		}
		if op != scanCtorArg {
			d.error(errPhase)
		}
	}

	return result, ctorNumArgsMismatch(name, len(t), i)
}

// ctorInterface is like ctor but returns []interface{}.
func (d *decodeState) ctorInterface() []interface{} {
	var v = make([]interface{}, 0)
	for {
		// Look ahead for ) - can only happen on first iteration.
		op := d.scanWhile(scanSkipSpace)
		if op == scanEndCtor {
			break
		}

		// Back up so d.value can have the byte we just read.
		d.off--
		d.scan.undo(op)

		v = append(v, d.valueInterface(false))

		// Next token must be , or ).
		op = d.scanWhile(scanSkipSpace)
		if op == scanEndCtor {
			break
		}
		if op != scanCtorArg {
			d.error(errPhase)
		}
	}
	return v
}

// Returns a descriptive error message if the number of arguments given
// to the constructor do not match what is expected.
func ctorNumArgsMismatch(name string, expected, actual int) error {
	if expected == actual {
		return nil
	}

	quantifier := ""
	if expected > 1 {
		quantifier = "s"
	}
	return fmt.Errorf(CtorNumArgsErrorf, expected, quantifier, name, actual)
}