Documentation ¶
Index ¶
- Variables
- func DescriptorToPathNode(desc *thrift.TypeDescriptor, root *PathNode, opts *Options) error
- func FreePathNode(p *PathNode)
- func GetDescByPath(desc *thrift.TypeDescriptor, path ...Path) (ret *thrift.TypeDescriptor, err error)
- type Node
- func NewNode(t thrift.Type, src []byte) Node
- func NewNodeBinary(val []byte) Node
- func NewNodeBool(val bool) Node
- func NewNodeByte(val byte) Node
- func NewNodeDouble(val float64) Node
- func NewNodeInt16(val int16) Node
- func NewNodeInt32(val int32) Node
- func NewNodeInt64(val int64) Node
- func NewNodeString(val string) Node
- func NewTypedNode(typ thrift.Type, et thrift.Type, kt thrift.Type) (ret Node)
- func (self Node) Binary() ([]byte, error)
- func (self Node) Bool() (bool, error)
- func (self Node) Byte() (byte, error)
- func (self *Node) Check() error
- func (self Node) Children(out *[]PathNode, recurse bool, opts *Options) (err error)
- func (self Node) ElemType() thrift.Type
- func (self Node) ErrCode() meta.ErrCode
- func (self Node) Error() string
- func (self Node) Field(id thrift.FieldID) (v Node)
- func (self Node) Fields(ids []PathNode, opts *Options) (err error)
- func (self Node) Float64() (float64, error)
- func (self Node) Foreach(handler func(path Path, node Node) bool, opts *Options) error
- func (self Node) ForeachKV(handler func(key Node, val Node) bool, opts *Options) error
- func (self Node) Fork() Node
- func (self Node) GetByInt(key int) (v Node)
- func (self Node) GetByPath(pathes ...Path) Node
- func (self Node) GetByRaw(key []byte) (v Node)
- func (self Node) GetByStr(key string) (v Node)
- func (self Node) GetMany(pathes []PathNode, opts *Options) error
- func (self Node) GetTree(tree *PathNode, opts *Options) error
- func (self Node) Gets(keys []PathNode, opts *Options) (err error)
- func (self Node) Index(i int) (v Node)
- func (self Node) Indexes(ins []PathNode, opts *Options) (err error)
- func (self Node) Int() (int, error)
- func (self Node) IntMap(opts *Options) (map[int]interface{}, error)
- func (self Node) Interface(opts *Options) (interface{}, error)
- func (self Node) InterfaceMap(opts *Options) (map[interface{}]interface{}, error)
- func (self Node) IsEmpty() bool
- func (self Node) IsErrNotFound() bool
- func (self Node) IsError() bool
- func (self Node) KeyType() thrift.Type
- func (self Node) Len() (int, error)
- func (self Node) List(opts *Options) ([]interface{}, error)
- func (self Node) Raw() []byte
- func (self *Node) SetByPath(sub Node, path ...Path) (exist bool, err error)
- func (self *Node) SetMany(pathes []PathNode, opts *Options) (err error)
- func (self Node) StrMap(opts *Options) (map[string]interface{}, error)
- func (self Node) String() (string, error)
- func (self Node) Type() thrift.Type
- func (self *Node) UnsetByPath(path ...Path) error
- type Options
- type Path
- type PathNode
- func (self *PathNode) Assgin(recurse bool, opts *Options) error
- func (self *PathNode) Check() error
- func (self PathNode) CopyTo(to *PathNode)
- func (self PathNode) Error() string
- func (self *PathNode) Field(id thrift.FieldID, opts *Options) *PathNode
- func (self PathNode) Fork() PathNode
- func (self *PathNode) GetByInt(key int, opts *Options) *PathNode
- func (self *PathNode) GetByStr(key string, opts *Options) *PathNode
- func (self *PathNode) Load(recurse bool, opts *Options) error
- func (self PathNode) Marshal(opt *Options) (out []byte, err error)
- func (self PathNode) MarshalIntoBuffer(out *[]byte, opt *Options) error
- func (self *PathNode) ResetAll()
- func (self *PathNode) ResetValue()
- func (self *PathNode) SetByInt(key int, val Node, opts *Options) (bool, error)
- func (self *PathNode) SetByStr(key string, val Node, opts *Options) (bool, error)
- func (self *PathNode) SetField(id thrift.FieldID, val Node, opts *Options) (bool, error)
- type PathType
- type Value
- func (self Value) Field(id thrift.FieldID) (v Value)
- func (self Value) FieldByName(name string) (v Value)
- func (self Value) Fork() Value
- func (self Value) GetByInt(key int) (v Value)
- func (self Value) GetByPath(pathes ...Path) Value
- func (self Value) GetByStr(key string) (v Value)
- func (self Value) Index(i int) (v Value)
- func (self Value) MarshalTo(to *thrift.TypeDescriptor, opts *Options) ([]byte, error)
- func (self *Value) SetByPath(sub Value, path ...Path) (exist bool, err error)
- func (self *Value) UnsetByPath(path ...Path) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // UseNativeSkipForGet indicates to use native.Skip (instead of go.Skip) method to skip thrift value // This only works for single-value searching API like GetByInt()/GetByRaw()/GetByStr()/Field()/Index()/GetByPath() methods. // WARN: this will promote performance when thrift value to be skipped is large, but may decrease preformance when thrift value is small. UseNativeSkipForGet = false // DefaultNodeSliceCap is the default capacity of a Node or NodePath slice // Usually, a Node or NodePath slice is used to store intermediate or consequential elements of a generic API like Children()|Interface()|SetMany() DefaultNodeSliceCap = 16 )
var ( // StoreChildrenByIdShreshold is the maximum id to store children node by id. StoreChildrenByIdShreshold = 256 // StoreChildrenByIdShreshold is the minimum id to store children node by hash. StoreChildrenByIntHashShreshold = DefaultNodeSliceCap )
Functions ¶
func DescriptorToPathNode ¶
func DescriptorToPathNode(desc *thrift.TypeDescriptor, root *PathNode, opts *Options) error
DescriptorToPathNode converts a thrift type descriptor to a DOM, assgining path to root NOTICE: it only recursively converts STRUCT type
func GetDescByPath ¶
func GetDescByPath(desc *thrift.TypeDescriptor, path ...Path) (ret *thrift.TypeDescriptor, err error)
GetDescByPath searches longitudinally and returns the sub descriptor of the desc specified by path
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a generic wrap of raw thrift data
func NewNode ¶
NewNodeBool create a new node with given type and data WARN: it WON'T check the correctness of the data
func NewNodeBinary ¶
NewNodeBinary converts a []byte value to a STRING node
func NewNodeDouble ¶
NewNodeDouble converts a float64 value to a DOUBLE node
func NewNodeInt16 ¶
NewNodeInt16 converts a int16 value to a I16 node
func NewNodeInt32 ¶
NewNodeInt32 converts a int32 value to a I32 node
func NewNodeInt64 ¶
NewNodeInt64 converts a int64 value to a I64 node
func NewNodeString ¶
NewNodeString converts a string value to a STRING node
func NewTypedNode ¶
NewTypedNode creates a new Node with the given typ, including element type (for LIST/SET/MAP) and key type (for MAP)
func (Node) Children ¶
Children loads all its children and children's children recursively (if recurse is true). out is used for store children, and it is always reset to zero length before use.
NOTICE: if opts.NotScanParentNode is true, the parent nodes (PathNode.Node) of complex (LIST/SET/MAP/STRUCT) type won't be assgined data
func (Node) Fields ¶
Fields returns all sub nodes ids along with the given int path from a STRUCT node.
func (Node) Foreach ¶
Foreach scan each element of a complex type (LIST/SET/MAP/STRUCT), and call handler sequentially with corresponding path and node
func (Node) ForeachKV ¶
ForeachKV scan each element of a MAP type, and call handler sequentially with corresponding key and value
func (Node) GetByPath ¶
GetByPath searches longitudinally and return a sub node at the given path from the node.
The path is a list of PathFieldId, PathIndex, PathStrKey, PathBinKey, PathIntKey, Each path MUST be a valid path type for current layer (e.g. PathFieldId is only valid for STRUCT). Empty path will return the current node.
func (Node) GetByRaw ¶
GetByInt returns a sub node at the given bytes key from a MAP node. The key must be deep equal (bytes.Equal) to the key in the map.
func (Node) GetMany ¶
GetMany searches transversely and returns all the sub nodes along with the given pathes.
func (Node) GetTree ¶
GetTree returns a tree of all sub nodes along with the given path on the tree. It supports longitudinally search (like GetByPath) and transversely search (like GetMany) both.
func (Node) Gets ¶
Gets returns all sub nodes along with the given key (PathStrKey|PathIntKey|PathBinKey) path from a MAP node.
func (Node) Indexes ¶
Indexes returns all sub nodes along with the given int path from a LIST/SET node.
func (Node) IntMap ¶
StrMap returns the integer keys and interface elements contained by a MAP<I8|I16|I32|I64,XX> node
func (Node) Interface ¶
Interface returns the go interface value contained by a node. If the node is a STRUCT, it will return a map[thrift.FieldID]interface{} If it is a map, it will return map[int|string|interface{}]interface{}, which depends on the key type
func (Node) InterfaceMap ¶
InterfaceMap returns the interface keys and interface elements contained by a MAP node. If the key type is complex (LIST/SET/MAP/STRUCT), it will be stored using its pointer since its value are not supported by Go
func (Node) IsErrNotFound ¶
IsErrorNotFound tells if the node is not-found-data error
func (Node) StrMap ¶
StrMap returns the string keys and interface elements contained by a MAP<STRING,XX> node
func (*Node) UnsetByPath ¶
type Options ¶
type Options struct { // DisallowUnknow indicates to report error when read unknown fields. DisallowUnknow bool // WriteDefault indicates to write value if a DEFAULT requireness field is not set. WriteDefault bool // NoCheckRequireNess indicates not to check requiredness when writing. NotCheckRequireNess bool // UseNativeSkip indicates to use native.Skip (instead of go.Skip) method to skip thrift value // WARN: this will promote performance when thrift value to be skipped is large, but may decrease preformance when thrift value is small. UseNativeSkip bool // MapStructById indicates to use field id as map key instead of when call Node.Interface() on STRUCT type. MapStructById bool // CastStringAsBinary indicates to cast STRING type to []byte when call Node.Interface()/Map(). CastStringAsBinary bool // NotScanParentNode indicates to only assign children node when PathNode.Load()/Node.Children. // Thies will promote performance but may be misued when handle PathNode. NotScanParentNode bool // ClearDirtyValues indicates one multi-query (includeing // Fields()/GetMany()/Gets()/Indexies()) to clear out all nodes // in passed []PathNode first ClearDirtyValues bool // StoreChildrenById indicates to store children node by id when call Node.Children() or PathNode.Load(). // When field id exceeds StoreChildrenByIdShreshold, children node will be stored sequentially after the threshold. StoreChildrenById bool // StoreChildrenByHash indicates to store children node by str hash (mod parent's size) when call Node.Children() or PathNode.Load(). StoreChildrenByHash bool }
Opions for generic.Node
type Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path represents the relative position of a sub node in a complex parent node
func NewPathFieldId ¶
NewPathFieldId creates a PathFieldId path
func NewPathFieldName ¶
NewPathFieldName creates a PathFieldName path
type PathNode ¶
PathNode is a three node of DOM tree
func (*PathNode) Assgin ¶
Assgin assigns self's raw Value according to its Next Path, which must be set before calling this method.
func (*PathNode) Field ¶
Field get the child node by field id. Only support STRUCT.
If opts.StoreChildrenById is true, it will try to use id (O(1)) as index to search the key. However, if the struct fields have changed, it may fallback to O(n) search.
func (*PathNode) GetByInt ¶
GetByInt get the child node by integer. Only support MAP with integer-type key.
If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key. However, if the map size has changed, it may fallback to O(n) search.
func (*PathNode) GetByStr ¶
GetByInt get the child node by string. Only support MAP with string-type key.
If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key. However, if the map size has changed, it may fallback to O(n) search.
func (*PathNode) Load ¶
Load loads self's all children ( and children's children if recurse is true) into self.Next, no matter whether self.Next is empty or set before (will be reset). NOTICE: if opts.NotScanParentNode is true, the parent nodes (PathNode.Node) of complex (map/list/struct) type won't be assgined data
Example ¶
// make root PathNode data := getExampleData() root := PathNode{ Node: NewNode(thrift.STRUCT, data), } // load first level children err := root.Load(false, opts) if err != nil { panic(err) } fmt.Printf("%#v", root) // load all level children err = root.Load(true, opts) if err != nil { panic(err) } fmt.Printf("%#v", root) // reuse PathNode memory reuse := pathNodePool.Get().(*PathNode) root.Node = NewNode(thrift.STRUCT, data) err = root.Load(true, opts) if err != nil { panic(err) } fmt.Printf("%#v", root) reuse.ResetValue() pathNodePool.Put(reuse)
Output:
func (PathNode) MarshalIntoBuffer ¶
MarshalIntoBuffer marshals self to thrift bytes into a buffer
func (*PathNode) ResetAll ¶
func (self *PathNode) ResetAll()
ResetAll resets self and its children, including path and node both
func (*PathNode) ResetValue ¶
func (self *PathNode) ResetValue()
ResetValue resets self's node and its children's node
func (*PathNode) SetByInt ¶
SetByInt set the child node by integer. Only support MAP with integer-type key. If the key already exists, it will be overwritten and return true.
If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key. However, if the map hash size has changed, it may fallback to O(n) search.
func (*PathNode) SetByStr ¶
SetByStr set the child node by string. Only support MAP with string-type key. If the key already exists, it will be overwritten and return true.
If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key. However, if the map hash size has changed, it may fallback to O(n) search.
func (*PathNode) SetField ¶
SetField set the child node by field id. Only support STRUCT. If the key already exists, it will be overwritten and return true.
If opts.StoreChildrenById is true, it will try to use id (O(1)) as index to search the key. However, if the struct fields have changed, it may fallback to O(n) search.
type PathType ¶
type PathType uint8
PathType is the type of path
const ( // PathFieldId represents a field id of STRUCT type PathFieldId PathType = 1 + iota // PathFieldName represents a field name of STRUCT type // NOTICE: it is only supported by Value PathFieldName // PathIndex represents a index of LIST\SET type PathIndex // Path represents a string key of MAP type PathStrKey // Path represents a int key of MAP type PathIntKey // Path represents a raw-bytes key of MAP type // It is usually used for neither-string-nor-integer type key PathBinKey )
type Value ¶
type Value struct { Node Desc *thrift.TypeDescriptor }
Value is a generic API wrapper for operations on thrift data. Node is the underlying raw bytes. Desc is the corresponding type descriptor.
func NewValue ¶
func NewValue(desc *thrift.TypeDescriptor, src []byte) Value
NewValue creates a new Value from a raw byte slice.
func (Value) FieldByName ¶
FieldByName returns a sub node at the given field name from a STRUCT value.
func (Value) GetByPath ¶
GetByPath searches longitudinally and return a sub value at the given path from the value.
The path is a list of PathFieldId, PathFieldName, PathIndex, PathStrKey, PathBinKey, PathIntKey, Each path MUST be a valid path type for current layer (e.g. PathFieldId is only valid for STRUCT). Empty path will return the current value.
func (Value) MarshalTo ¶
MarshalTo marshals self value into a sub value descripted by the to descriptor, alse called as "Cutting". Usually, the to descriptor is a subset of self descriptor.
Example ¶
// make full value desc := getExampleDesc() data := getExampleData() v := NewValue(desc, data) // print full value full, err := NewNode(thrift.STRUCT, data).Interface(opts) if err != nil { panic(err) } fmt.Printf("%#v", full) // get partial descriptor pdesc := getExamplePartialDesc() // cut full value to partial value out, err := v.MarshalTo(pdesc, opts) if err != nil { panic(err) } // print partial value partial, err := NewNode(thrift.STRUCT, out).Interface(opts) if err != nil { panic(err) } fmt.Printf("%#v", partial)
Output:
func (*Value) SetByPath ¶
SetByPath searches longitudinally and sets a sub value at the given path from the value. exist tells whether the node is already exists.
Example ¶
// pack root value desc := getExampleDesc() data := getExampleData() v := NewValue(desc, data) // pack insert value d := desc.Struct().FieldByKey("Base").Type().Struct().FieldByKey("Extra").Type().Elem() p := thrift.NewBinaryProtocol([]byte{}) exp := "中文" p.WriteString(exp) buf := p.RawBuf() vv := NewValue(d, buf) // insert value ps := []Path{NewPathFieldName("Base"), NewPathFieldName("Extra"), NewPathStrKey("b")} exist, err2 := v.SetByPath(vv, ps...) if err2 != nil { panic(err2) } println(exist) // false // check inserted value s2 := v.GetByPath(ps...) if s2.Error() != "" { panic(s2.Error()) } f2, _ := s2.String() println(f2) // 中文
Output:
func (*Value) UnsetByPath ¶
UnsetByPath searches longitudinally and unsets a sub value at the given path from the value.