用户登录和接口鉴权
This commit is contained in:
354
vendor/github.com/bytedance/sonic/ast/node.go
generated
vendored
354
vendor/github.com/bytedance/sonic/ast/node.go
generated
vendored
@@ -17,15 +17,13 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
|
||||
"github.com/bytedance/sonic/internal/native/types"
|
||||
"github.com/bytedance/sonic/internal/rt"
|
||||
`encoding/json`
|
||||
`fmt`
|
||||
`strconv`
|
||||
`unsafe`
|
||||
|
||||
`github.com/bytedance/sonic/internal/native/types`
|
||||
`github.com/bytedance/sonic/internal/rt`
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -38,7 +36,7 @@ const (
|
||||
_V_ARRAY_LAZY = _V_LAZY | types.V_ARRAY
|
||||
_V_OBJECT_LAZY = _V_LAZY | types.V_OBJECT
|
||||
_MASK_LAZY = _V_LAZY - 1
|
||||
_MASK_RAW = _V_RAW - 1
|
||||
_MASK_RAW = _V_RAW - 1
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -58,20 +56,19 @@ type Node struct {
|
||||
t types.ValueType
|
||||
l uint
|
||||
p unsafe.Pointer
|
||||
m *sync.RWMutex
|
||||
}
|
||||
|
||||
// UnmarshalJSON is just an adapter to json.Unmarshaler.
|
||||
// If you want better performance, use Searcher.GetByPath() directly
|
||||
func (self *Node) UnmarshalJSON(data []byte) (err error) {
|
||||
*self = newRawNode(rt.Mem2Str(data), switchRawType(data[0]), false)
|
||||
return nil
|
||||
*self = NewRaw(string(data))
|
||||
return self.Check()
|
||||
}
|
||||
|
||||
/** Node Type Accessor **/
|
||||
|
||||
// Type returns json type represented by the node
|
||||
// It will be one of bellows:
|
||||
// It will be one of belows:
|
||||
// V_NONE = 0 (empty node, key not exists)
|
||||
// V_ERROR = 1 (error node)
|
||||
// V_NULL = 2 (json value `null`, key exists)
|
||||
@@ -82,39 +79,17 @@ func (self *Node) UnmarshalJSON(data []byte) (err error) {
|
||||
// V_STRING = 7 (json value string)
|
||||
// V_NUMBER = 33 (json value number )
|
||||
// V_ANY = 34 (golang interface{})
|
||||
//
|
||||
// Deprecated: not concurrent safe. Use TypeSafe instead
|
||||
func (self Node) Type() int {
|
||||
return int(self.t & _MASK_LAZY & _MASK_RAW)
|
||||
}
|
||||
|
||||
// Type concurrently-safe returns json type represented by the node
|
||||
// It will be one of bellows:
|
||||
// V_NONE = 0 (empty node, key not exists)
|
||||
// V_ERROR = 1 (error node)
|
||||
// V_NULL = 2 (json value `null`, key exists)
|
||||
// V_TRUE = 3 (json value `true`)
|
||||
// V_FALSE = 4 (json value `false`)
|
||||
// V_ARRAY = 5 (json value array)
|
||||
// V_OBJECT = 6 (json value object)
|
||||
// V_STRING = 7 (json value string)
|
||||
// V_NUMBER = 33 (json value number )
|
||||
// V_ANY = 34 (golang interface{})
|
||||
func (self *Node) TypeSafe() int {
|
||||
return int(self.loadt() & _MASK_LAZY & _MASK_RAW)
|
||||
}
|
||||
|
||||
func (self *Node) itype() types.ValueType {
|
||||
func (self Node) itype() types.ValueType {
|
||||
return self.t & _MASK_LAZY & _MASK_RAW
|
||||
}
|
||||
|
||||
// Exists returns false only if the self is nil or empty node V_NONE
|
||||
func (self *Node) Exists() bool {
|
||||
if self == nil {
|
||||
return false
|
||||
}
|
||||
t := self.loadt()
|
||||
return t != V_ERROR && t != _V_NONE
|
||||
return self.Valid() && self.t != _V_NONE
|
||||
}
|
||||
|
||||
// Valid reports if self is NOT V_ERROR or nil
|
||||
@@ -122,7 +97,7 @@ func (self *Node) Valid() bool {
|
||||
if self == nil {
|
||||
return false
|
||||
}
|
||||
return self.loadt() != V_ERROR
|
||||
return self.t != V_ERROR
|
||||
}
|
||||
|
||||
// Check checks if the node itself is valid, and return:
|
||||
@@ -131,31 +106,24 @@ func (self *Node) Valid() bool {
|
||||
func (self *Node) Check() error {
|
||||
if self == nil {
|
||||
return ErrNotExist
|
||||
} else if self.loadt() != V_ERROR {
|
||||
} else if self.t != V_ERROR {
|
||||
return nil
|
||||
} else {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
// isRaw returns true if node's underlying value is raw json
|
||||
//
|
||||
// Deprecated: not concurrent safe
|
||||
func (self Node) IsRaw() bool {
|
||||
return self.t & _V_RAW != 0
|
||||
}
|
||||
|
||||
// IsRaw returns true if node's underlying value is raw json
|
||||
func (self *Node) isRaw() bool {
|
||||
return self.loadt() & _V_RAW != 0
|
||||
func (self Node) IsRaw() bool {
|
||||
return self.t&_V_RAW != 0
|
||||
}
|
||||
|
||||
func (self *Node) isLazy() bool {
|
||||
return self != nil && self.t & _V_LAZY != 0
|
||||
return self != nil && self.t&_V_LAZY != 0
|
||||
}
|
||||
|
||||
func (self *Node) isAny() bool {
|
||||
return self != nil && self.loadt() == _V_ANY
|
||||
return self != nil && self.t == _V_ANY
|
||||
}
|
||||
|
||||
/** Simple Value Methods **/
|
||||
@@ -165,26 +133,18 @@ func (self *Node) Raw() (string, error) {
|
||||
if self == nil {
|
||||
return "", ErrNotExist
|
||||
}
|
||||
lock := self.rlock()
|
||||
if !self.isRaw() {
|
||||
if lock {
|
||||
self.runlock()
|
||||
}
|
||||
if !self.IsRaw() {
|
||||
buf, err := self.MarshalJSON()
|
||||
return rt.Mem2Str(buf), err
|
||||
}
|
||||
ret := self.toString()
|
||||
if lock {
|
||||
self.runlock()
|
||||
}
|
||||
return ret, nil
|
||||
return self.toString(), nil
|
||||
}
|
||||
|
||||
func (self *Node) checkRaw() error {
|
||||
if err := self.Check(); err != nil {
|
||||
return err
|
||||
}
|
||||
if self.isRaw() {
|
||||
if self.IsRaw() {
|
||||
self.parseRaw(false)
|
||||
}
|
||||
return self.Check()
|
||||
@@ -440,7 +400,7 @@ func (self *Node) String() (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// StrictString returns string value (unescaped), including V_STRING, V_ANY of string.
|
||||
// StrictString returns string value (unescaped), includeing V_STRING, V_ANY of string.
|
||||
// In other cases, it will return empty string.
|
||||
func (self *Node) StrictString() (string, error) {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
@@ -509,24 +469,7 @@ func (self *Node) Float64() (float64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Node) StrictBool() (bool, error) {
|
||||
if err := self.checkRaw(); err!= nil {
|
||||
return false, err
|
||||
}
|
||||
switch self.t {
|
||||
case types.V_TRUE : return true, nil
|
||||
case types.V_FALSE : return false, nil
|
||||
case _V_ANY :
|
||||
any := self.packAny()
|
||||
switch v := any.(type) {
|
||||
case bool : return v, nil
|
||||
default : return false, ErrUnsupportType
|
||||
}
|
||||
default : return false, ErrUnsupportType
|
||||
}
|
||||
}
|
||||
|
||||
// Float64 exports underlying float64 value, including V_NUMBER, V_ANY
|
||||
// Float64 exports underlying float64 value, includeing V_NUMBER, V_ANY
|
||||
func (self *Node) StrictFloat64() (float64, error) {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
return 0.0, err
|
||||
@@ -544,7 +487,7 @@ func (self *Node) StrictFloat64() (float64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Sequential Value Methods **/
|
||||
/** Sequencial Value Methods **/
|
||||
|
||||
// Len returns children count of a array|object|string node
|
||||
// WARN: For partially loaded node, it also works but only counts the parsed children
|
||||
@@ -561,7 +504,7 @@ func (self *Node) Len() (int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Node) len() int {
|
||||
func (self Node) len() int {
|
||||
return int(self.l)
|
||||
}
|
||||
|
||||
@@ -584,7 +527,7 @@ func (self *Node) Cap() (int, error) {
|
||||
//
|
||||
// If self is V_NONE or V_NULL, it becomes V_OBJECT and sets the node at the key.
|
||||
func (self *Node) Set(key string, node Node) (bool, error) {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
if err := self.Check(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := node.Check(); err != nil {
|
||||
@@ -592,7 +535,7 @@ func (self *Node) Set(key string, node Node) (bool, error) {
|
||||
}
|
||||
|
||||
if self.t == _V_NONE || self.t == types.V_NULL {
|
||||
*self = NewObject([]Pair{NewPair(key, node)})
|
||||
*self = NewObject([]Pair{{key, node}})
|
||||
return false, nil
|
||||
} else if self.itype() != types.V_OBJECT {
|
||||
return false, ErrUnsupportType
|
||||
@@ -606,7 +549,7 @@ func (self *Node) Set(key string, node Node) (bool, error) {
|
||||
*self = newObject(new(linkedPairs))
|
||||
}
|
||||
s := (*linkedPairs)(self.p)
|
||||
s.Push(NewPair(key, node))
|
||||
s.Push(Pair{key, node})
|
||||
self.l++
|
||||
return false, nil
|
||||
|
||||
@@ -625,10 +568,10 @@ func (self *Node) SetAny(key string, val interface{}) (bool, error) {
|
||||
|
||||
// Unset REMOVE (soft) the node of given key under object parent, and reports if the key has existed.
|
||||
func (self *Node) Unset(key string) (bool, error) {
|
||||
if err := self.should(types.V_OBJECT); err != nil {
|
||||
if err := self.should(types.V_OBJECT, "an object"); err != nil {
|
||||
return false, err
|
||||
}
|
||||
// NOTICE: must get accurate length before deduct
|
||||
// NOTICE: must get acurate length before deduct
|
||||
if err := self.skipAllKey(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -646,7 +589,7 @@ func (self *Node) Unset(key string) (bool, error) {
|
||||
//
|
||||
// The index must be within self's children.
|
||||
func (self *Node) SetByIndex(index int, node Node) (bool, error) {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
if err := self.Check(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := node.Check(); err != nil {
|
||||
@@ -674,7 +617,7 @@ func (self *Node) SetAnyByIndex(index int, val interface{}) (bool, error) {
|
||||
return self.SetByIndex(index, NewAny(val))
|
||||
}
|
||||
|
||||
// UnsetByIndex REMOVE (softly) the node of given index.
|
||||
// UnsetByIndex REOMVE (softly) the node of given index.
|
||||
//
|
||||
// WARN: this will change address of elements, which is a dangerous action.
|
||||
// Use Unset() for object or Pop() for array instead.
|
||||
@@ -726,7 +669,7 @@ func (self *Node) UnsetByIndex(index int) (bool, error) {
|
||||
//
|
||||
// If self is V_NONE or V_NULL, it becomes V_ARRAY and sets the node at index 0.
|
||||
func (self *Node) Add(node Node) error {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
if err := self.Check(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -734,7 +677,7 @@ func (self *Node) Add(node Node) error {
|
||||
*self = NewArray([]Node{node})
|
||||
return nil
|
||||
}
|
||||
if err := self.should(types.V_ARRAY); err != nil {
|
||||
if err := self.should(types.V_ARRAY, "an array"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -793,11 +736,11 @@ func (self *Node) Pop() error {
|
||||
}
|
||||
|
||||
// Move moves the child at src index to dst index,
|
||||
// meanwhile slides siblings from src+1 to dst.
|
||||
// meanwhile slides sliblings from src+1 to dst.
|
||||
//
|
||||
// WARN: this will change address of elements, which is a dangerous action.
|
||||
func (self *Node) Move(dst, src int) error {
|
||||
if err := self.should(types.V_ARRAY); err != nil {
|
||||
if err := self.should(types.V_ARRAY, "an array"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -833,7 +776,7 @@ func (self *Node) Move(dst, src int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddAny wraps val with V_ANY node, and Add() the node.
|
||||
// SetAny wraps val with V_ANY node, and Add() the node.
|
||||
func (self *Node) AddAny(val interface{}) error {
|
||||
return self.Add(NewAny(val))
|
||||
}
|
||||
@@ -869,7 +812,7 @@ func (self *Node) GetByPath(path ...interface{}) *Node {
|
||||
|
||||
// Get loads given key of an object node on demands
|
||||
func (self *Node) Get(key string) *Node {
|
||||
if err := self.should(types.V_OBJECT); err != nil {
|
||||
if err := self.should(types.V_OBJECT, "an object"); err != nil {
|
||||
return unwrapError(err)
|
||||
}
|
||||
n, _ := self.skipKey(key)
|
||||
@@ -902,14 +845,14 @@ func (self *Node) Index(idx int) *Node {
|
||||
// IndexPair indexies pair at given idx,
|
||||
// node type MUST be either V_OBJECT
|
||||
func (self *Node) IndexPair(idx int) *Pair {
|
||||
if err := self.should(types.V_OBJECT); err != nil {
|
||||
if err := self.should(types.V_OBJECT, "an object"); err != nil {
|
||||
return nil
|
||||
}
|
||||
return self.skipIndexPair(idx)
|
||||
}
|
||||
|
||||
func (self *Node) indexOrGet(idx int, key string) (*Node, int) {
|
||||
if err := self.should(types.V_OBJECT); err != nil {
|
||||
if err := self.should(types.V_OBJECT, "an object"); err != nil {
|
||||
return unwrapError(err), idx
|
||||
}
|
||||
|
||||
@@ -946,16 +889,16 @@ func (self *Node) Map() (map[string]interface{}, error) {
|
||||
return nil, ErrUnsupportType
|
||||
}
|
||||
}
|
||||
if err := self.should(types.V_OBJECT); err != nil {
|
||||
if err := self.should(types.V_OBJECT, "an object"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := self.loadAllKey(false); err != nil {
|
||||
if err := self.loadAllKey(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericObject()
|
||||
}
|
||||
|
||||
// MapUseNumber loads all keys of an object node, with numeric nodes cast to json.Number
|
||||
// MapUseNumber loads all keys of an object node, with numeric nodes casted to json.Number
|
||||
func (self *Node) MapUseNumber() (map[string]interface{}, error) {
|
||||
if self.isAny() {
|
||||
any := self.packAny()
|
||||
@@ -965,16 +908,16 @@ func (self *Node) MapUseNumber() (map[string]interface{}, error) {
|
||||
return nil, ErrUnsupportType
|
||||
}
|
||||
}
|
||||
if err := self.should(types.V_OBJECT); err != nil {
|
||||
if err := self.should(types.V_OBJECT, "an object"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := self.loadAllKey(false); err != nil {
|
||||
if err := self.loadAllKey(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericObjectUseNumber()
|
||||
}
|
||||
|
||||
// MapUseNode scans both parsed and non-parsed children nodes,
|
||||
// MapUseNode scans both parsed and non-parsed chidren nodes,
|
||||
// and map them by their keys
|
||||
func (self *Node) MapUseNode() (map[string]Node, error) {
|
||||
if self.isAny() {
|
||||
@@ -985,7 +928,7 @@ func (self *Node) MapUseNode() (map[string]Node, error) {
|
||||
return nil, ErrUnsupportType
|
||||
}
|
||||
}
|
||||
if err := self.should(types.V_OBJECT); err != nil {
|
||||
if err := self.should(types.V_OBJECT, "an object"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := self.skipAllKey(); err != nil {
|
||||
@@ -1091,16 +1034,16 @@ func (self *Node) Array() ([]interface{}, error) {
|
||||
return nil, ErrUnsupportType
|
||||
}
|
||||
}
|
||||
if err := self.should(types.V_ARRAY); err != nil {
|
||||
if err := self.should(types.V_ARRAY, "an array"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := self.loadAllIndex(false); err != nil {
|
||||
if err := self.loadAllIndex(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericArray()
|
||||
}
|
||||
|
||||
// ArrayUseNumber loads all indexes of an array node, with numeric nodes cast to json.Number
|
||||
// ArrayUseNumber loads all indexes of an array node, with numeric nodes casted to json.Number
|
||||
func (self *Node) ArrayUseNumber() ([]interface{}, error) {
|
||||
if self.isAny() {
|
||||
any := self.packAny()
|
||||
@@ -1110,16 +1053,16 @@ func (self *Node) ArrayUseNumber() ([]interface{}, error) {
|
||||
return nil, ErrUnsupportType
|
||||
}
|
||||
}
|
||||
if err := self.should(types.V_ARRAY); err != nil {
|
||||
if err := self.should(types.V_ARRAY, "an array"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := self.loadAllIndex(false); err != nil {
|
||||
if err := self.loadAllIndex(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericArrayUseNumber()
|
||||
}
|
||||
|
||||
// ArrayUseNode copies both parsed and non-parsed children nodes,
|
||||
// ArrayUseNode copys both parsed and non-parsed chidren nodes,
|
||||
// and indexes them by original order
|
||||
func (self *Node) ArrayUseNode() ([]Node, error) {
|
||||
if self.isAny() {
|
||||
@@ -1130,7 +1073,7 @@ func (self *Node) ArrayUseNode() ([]Node, error) {
|
||||
return nil, ErrUnsupportType
|
||||
}
|
||||
}
|
||||
if err := self.should(types.V_ARRAY); err != nil {
|
||||
if err := self.should(types.V_ARRAY, "an array"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := self.skipAllIndex(); err != nil {
|
||||
@@ -1164,9 +1107,9 @@ func (self *Node) unsafeArray() (*linkedNodes, error) {
|
||||
return (*linkedNodes)(self.p), nil
|
||||
}
|
||||
|
||||
// Interface loads all children under all paths from this node,
|
||||
// Interface loads all children under all pathes from this node,
|
||||
// and converts itself as generic type.
|
||||
// WARN: all numeric nodes are cast to float64
|
||||
// WARN: all numberic nodes are casted to float64
|
||||
func (self *Node) Interface() (interface{}, error) {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
return nil, err
|
||||
@@ -1186,12 +1129,12 @@ func (self *Node) Interface() (interface{}, error) {
|
||||
}
|
||||
return v, nil
|
||||
case _V_ARRAY_LAZY :
|
||||
if err := self.loadAllIndex(false); err != nil {
|
||||
if err := self.loadAllIndex(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericArray()
|
||||
case _V_OBJECT_LAZY :
|
||||
if err := self.loadAllKey(false); err != nil {
|
||||
if err := self.loadAllKey(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericObject()
|
||||
@@ -1210,7 +1153,7 @@ func (self *Node) packAny() interface{} {
|
||||
}
|
||||
|
||||
// InterfaceUseNumber works same with Interface()
|
||||
// except numeric nodes are cast to json.Number
|
||||
// except numberic nodes are casted to json.Number
|
||||
func (self *Node) InterfaceUseNumber() (interface{}, error) {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
return nil, err
|
||||
@@ -1225,12 +1168,12 @@ func (self *Node) InterfaceUseNumber() (interface{}, error) {
|
||||
case types.V_STRING : return self.toString(), nil
|
||||
case _V_NUMBER : return self.toNumber(), nil
|
||||
case _V_ARRAY_LAZY :
|
||||
if err := self.loadAllIndex(false); err != nil {
|
||||
if err := self.loadAllIndex(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericArrayUseNumber()
|
||||
case _V_OBJECT_LAZY :
|
||||
if err := self.loadAllKey(false); err != nil {
|
||||
if err := self.loadAllKey(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.toGenericObjectUseNumber()
|
||||
@@ -1262,30 +1205,70 @@ func (self *Node) InterfaceUseNode() (interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// LoadAll loads the node's children
|
||||
// and ensure all its children can be READ concurrently (include its children's children)
|
||||
// LoadAll loads all the node's children and children's children as parsed.
|
||||
// After calling it, the node can be safely used on concurrency
|
||||
func (self *Node) LoadAll() error {
|
||||
return self.Load()
|
||||
if self.IsRaw() {
|
||||
self.parseRaw(true)
|
||||
return self.Check()
|
||||
}
|
||||
|
||||
switch self.itype() {
|
||||
case types.V_ARRAY:
|
||||
e := self.len()
|
||||
if err := self.loadAllIndex(); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := 0; i < e; i++ {
|
||||
n := self.nodeAt(i)
|
||||
if n.IsRaw() {
|
||||
n.parseRaw(true)
|
||||
}
|
||||
if err := n.Check(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
case types.V_OBJECT:
|
||||
e := self.len()
|
||||
if err := self.loadAllKey(); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := 0; i < e; i++ {
|
||||
n := self.pairAt(i)
|
||||
if n.Value.IsRaw() {
|
||||
n.Value.parseRaw(true)
|
||||
}
|
||||
if err := n.Value.Check(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return self.Check()
|
||||
}
|
||||
}
|
||||
|
||||
// Load loads the node's children as parsed.
|
||||
// and ensure all its children can be READ concurrently (include its children's children)
|
||||
// After calling it, only the node itself can be used on concurrency (not include its children)
|
||||
func (self *Node) Load() error {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch self.t {
|
||||
case _V_ARRAY_LAZY: self.loadAllIndex(true)
|
||||
case _V_OBJECT_LAZY: self.loadAllKey(true)
|
||||
case V_ERROR: return self
|
||||
case V_NONE: return nil
|
||||
case _V_ARRAY_LAZY:
|
||||
return self.skipAllIndex()
|
||||
case _V_OBJECT_LAZY:
|
||||
return self.skipAllKey()
|
||||
default:
|
||||
return self.Check()
|
||||
}
|
||||
if self.m == nil {
|
||||
self.m = new(sync.RWMutex)
|
||||
}
|
||||
return self.checkRaw()
|
||||
}
|
||||
|
||||
/**---------------------------------- Internal Helper Methods ----------------------------------**/
|
||||
|
||||
func (self *Node) should(t types.ValueType) error {
|
||||
func (self *Node) should(t types.ValueType, s string) error {
|
||||
if err := self.checkRaw(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1456,17 +1439,13 @@ func (self *Node) skipIndexPair(index int) *Pair {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Node) loadAllIndex(loadOnce bool) error {
|
||||
func (self *Node) loadAllIndex() error {
|
||||
if !self.isLazy() {
|
||||
return nil
|
||||
}
|
||||
var err types.ParsingError
|
||||
parser, stack := self.getParserAndArrayStack()
|
||||
if !loadOnce {
|
||||
parser.noLazy = true
|
||||
} else {
|
||||
parser.loadOnce = true
|
||||
}
|
||||
parser.noLazy = true
|
||||
*self, err = parser.decodeArray(&stack.v)
|
||||
if err != 0 {
|
||||
return parser.ExportError(err)
|
||||
@@ -1474,19 +1453,14 @@ func (self *Node) loadAllIndex(loadOnce bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Node) loadAllKey(loadOnce bool) error {
|
||||
func (self *Node) loadAllKey() error {
|
||||
if !self.isLazy() {
|
||||
return nil
|
||||
}
|
||||
var err types.ParsingError
|
||||
parser, stack := self.getParserAndObjectStack()
|
||||
if !loadOnce {
|
||||
parser.noLazy = true
|
||||
*self, err = parser.decodeObject(&stack.v)
|
||||
} else {
|
||||
parser.loadOnce = true
|
||||
*self, err = parser.decodeObject(&stack.v)
|
||||
}
|
||||
parser.noLazy = true
|
||||
*self, err = parser.decodeObject(&stack.v)
|
||||
if err != 0 {
|
||||
return parser.ExportError(err)
|
||||
}
|
||||
@@ -1655,23 +1629,7 @@ func NewRaw(json string) Node {
|
||||
if it == _V_NONE {
|
||||
return Node{}
|
||||
}
|
||||
return newRawNode(parser.s[start:parser.p], it, false)
|
||||
}
|
||||
|
||||
// NewRawConcurrentRead creates a node of raw json, which can be READ
|
||||
// (GetByPath/Get/Index/GetOrIndex/Int64/Bool/Float64/String/Number/Interface/Array/Map/Raw/MarshalJSON) concurrently.
|
||||
// If the input json is invalid, NewRaw returns a error Node.
|
||||
func NewRawConcurrentRead(json string) Node {
|
||||
parser := NewParserObj(json)
|
||||
start, err := parser.skip()
|
||||
if err != 0 {
|
||||
return *newError(err, err.Message())
|
||||
}
|
||||
it := switchRawType(parser.s[start])
|
||||
if it == _V_NONE {
|
||||
return Node{}
|
||||
}
|
||||
return newRawNode(parser.s[start:parser.p], it, true)
|
||||
return newRawNode(parser.s[start:parser.p], it)
|
||||
}
|
||||
|
||||
// NewAny creates a node of type V_ANY if any's type isn't Node or *Node,
|
||||
@@ -1695,7 +1653,7 @@ func NewBytes(src []byte) Node {
|
||||
if len(src) == 0 {
|
||||
panic("empty src bytes")
|
||||
}
|
||||
out := rt.EncodeBase64ToString(src)
|
||||
out := encodeBase64(src)
|
||||
return NewString(out)
|
||||
}
|
||||
|
||||
@@ -1731,15 +1689,15 @@ func NewNumber(v string) Node {
|
||||
}
|
||||
}
|
||||
|
||||
func (node *Node) toNumber() json.Number {
|
||||
func (node Node) toNumber() json.Number {
|
||||
return json.Number(rt.StrFrom(node.p, int64(node.l)))
|
||||
}
|
||||
|
||||
func (self *Node) toString() string {
|
||||
func (self Node) toString() string {
|
||||
return rt.StrFrom(self.p, int64(self.l))
|
||||
}
|
||||
|
||||
func (node *Node) toFloat64() (float64, error) {
|
||||
func (node Node) toFloat64() (float64, error) {
|
||||
ret, err := node.toNumber().Float64()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -1747,7 +1705,7 @@ func (node *Node) toFloat64() (float64, error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (node *Node) toInt64() (int64, error) {
|
||||
func (node Node) toInt64() (int64, error) {
|
||||
ret,err := node.toNumber().Int64()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -1783,8 +1741,6 @@ func NewArray(v []Node) Node {
|
||||
return newArray(s)
|
||||
}
|
||||
|
||||
const _Threshold_Index = 16
|
||||
|
||||
func newArray(v *linkedNodes) Node {
|
||||
return Node{
|
||||
t: types.V_ARRAY,
|
||||
@@ -1808,9 +1764,6 @@ func NewObject(v []Pair) Node {
|
||||
}
|
||||
|
||||
func newObject(v *linkedPairs) Node {
|
||||
if v.size > _Threshold_Index {
|
||||
v.BuildIndex()
|
||||
}
|
||||
return Node{
|
||||
t: types.V_OBJECT,
|
||||
l: uint(v.Len()),
|
||||
@@ -1819,42 +1772,53 @@ func newObject(v *linkedPairs) Node {
|
||||
}
|
||||
|
||||
func (self *Node) setObject(v *linkedPairs) {
|
||||
if v.size > _Threshold_Index {
|
||||
v.BuildIndex()
|
||||
}
|
||||
self.t = types.V_OBJECT
|
||||
self.l = uint(v.Len())
|
||||
self.p = unsafe.Pointer(v)
|
||||
}
|
||||
|
||||
func (self *Node) parseRaw(full bool) {
|
||||
lock := self.lock()
|
||||
defer self.unlock()
|
||||
if !self.isRaw() {
|
||||
return
|
||||
func newRawNode(str string, typ types.ValueType) Node {
|
||||
return Node{
|
||||
t: _V_RAW | typ,
|
||||
p: rt.StrPtr(str),
|
||||
l: uint(len(str)),
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Node) parseRaw(full bool) {
|
||||
raw := self.toString()
|
||||
parser := NewParserObj(raw)
|
||||
var e types.ParsingError
|
||||
if full {
|
||||
parser.noLazy = true
|
||||
*self, e = parser.Parse()
|
||||
} else if lock {
|
||||
var n Node
|
||||
parser.noLazy = true
|
||||
parser.loadOnce = true
|
||||
n, e = parser.Parse()
|
||||
self.assign(n)
|
||||
} else {
|
||||
*self, e = parser.Parse()
|
||||
parser.skipValue = false
|
||||
}
|
||||
var e types.ParsingError
|
||||
*self, e = parser.Parse()
|
||||
if e != 0 {
|
||||
*self = *newSyntaxError(parser.syntaxError(e))
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Node) assign(n Node) {
|
||||
self.l = n.l
|
||||
self.p = n.p
|
||||
atomic.StoreInt64(&self.t, n.t)
|
||||
var typeJumpTable = [256]types.ValueType{
|
||||
'"' : types.V_STRING,
|
||||
'-' : _V_NUMBER,
|
||||
'0' : _V_NUMBER,
|
||||
'1' : _V_NUMBER,
|
||||
'2' : _V_NUMBER,
|
||||
'3' : _V_NUMBER,
|
||||
'4' : _V_NUMBER,
|
||||
'5' : _V_NUMBER,
|
||||
'6' : _V_NUMBER,
|
||||
'7' : _V_NUMBER,
|
||||
'8' : _V_NUMBER,
|
||||
'9' : _V_NUMBER,
|
||||
'[' : types.V_ARRAY,
|
||||
'f' : types.V_FALSE,
|
||||
'n' : types.V_NULL,
|
||||
't' : types.V_TRUE,
|
||||
'{' : types.V_OBJECT,
|
||||
}
|
||||
|
||||
func switchRawType(c byte) types.ValueType {
|
||||
return typeJumpTable[c]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user