3 Star 0 Fork 1

tym_hmm/mongo-tool

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
constructor.go 2.83 KB
一键复制 编辑 原始数据 按行查看 历史
天蝎儿 提交于 2021-11-03 19:22 +08:00 . init
// 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"
)
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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tym_hmm/mongo-tool.git
git@gitee.com:tym_hmm/mongo-tool.git
tym_hmm
mongo-tool
mongo-tool
v1.0.5

搜索帮助