Documentation ¶
Index ¶
- Variables
- func FreeBytesToPool(b []byte)
- func FreePathNode(p *PathNode)
- func NewBytesFromPool() []byte
- type Node
- func NewComplexNode(t proto.Type, et proto.Type, kt proto.Type, src []byte) (ret Node)
- func NewNode(t proto.Type, src []byte) Node
- func NewNodeBool(val bool) Node
- func NewNodeByte(val byte) Node
- func NewNodeBytes(val []byte) Node
- func NewNodeDouble(val float64) Node
- func NewNodeEnum(val int32) Node
- func NewNodeFixed32(val uint32) Node
- func NewNodeFixed64(val uint64) Node
- func NewNodeFloat(val float32) Node
- func NewNodeInt32(val int32) Node
- func NewNodeInt64(val int64) Node
- func NewNodeSfixed32(val int32) Node
- func NewNodeSfixed64(val int64) Node
- func NewNodeSint32(val int32) Node
- func NewNodeSint64(val int64) Node
- func NewNodeString(val string) Node
- func NewNodeUint32(val uint32) Node
- func NewNodeUint64(val uint64) Node
- func (self Node) Binary() ([]byte, error)
- func (self Node) Bool() (bool, error)
- func (self *Node) Check() error
- func (self Node) Children(out *[]PathNode, recurse bool, opts *Options, desc *proto.TypeDescriptor) (err error)
- func (self Node) ElemType() proto.Type
- func (self Node) Enum() (int, error)
- func (self Node) ErrCode() meta.ErrCode
- func (self Node) Error() string
- func (self Node) Field(id proto.FieldNumber, rootLayer bool, msgDesc *proto.MessageDescriptor) (v Node, f *proto.FieldDescriptor)
- func (self Node) Fields(ids []PathNode, rootLayer bool, msgDesc *proto.MessageDescriptor, ...) error
- func (self Node) Float64() (float64, error)
- func (self Node) Fork() Node
- func (self Node) GetByInt(key int) (v Node)
- func (self Node) GetByStr(key string) (v Node)
- func (self Node) Gets(keys []PathNode, opts *Options) error
- func (self Node) Index(idx int) (v Node)
- func (self Node) Indexes(ins []PathNode, opts *Options) error
- func (self Node) Int() (int, error)
- func (self Node) IsErrNotFound() bool
- func (self Node) IsError() bool
- func (self Node) IsUnKnown() bool
- func (self Node) KeyType() proto.Type
- func (self Node) Len() (int, error)
- func (self Node) Raw() []byte
- func (self *Node) SetElemType(et proto.Type)
- func (self *Node) SetKeyType(kt proto.Type)
- func (self Node) String() (string, error)
- func (self Node) Type() proto.Type
- func (self Node) Uint() (uint, error)
- type Options
- type Path
- type PathNode
- type PathType
- type Value
- func (self Value) Field(id proto.FieldNumber) (v Value)
- func (self Value) FieldByName(name string) (v Value)
- func (self Value) Fields(ids []PathNode, opts *Options) error
- func (self Value) Fork() Value
- func (self Value) GetByInt(key int) (v Value)
- func (self Value) GetByPath(pathes ...Path) Value
- func (self Value) GetByPathWithAddress(pathes ...Path) (Value, []int)
- func (self Value) GetByStr(key string) (v Value)
- func (self Value) GetMany(pathes []PathNode, opts *Options) error
- func (self Value) Index(i int) (v Value)
- func (self Value) IntMap(opts *Options) (map[int]interface{}, error)
- func (self Value) Interface(opts *Options) (interface{}, error)
- func (self Value) List(opts *Options) ([]interface{}, error)
- func (self Value) MarshalTo(to *proto.TypeDescriptor, opts *Options) ([]byte, error)
- func (self *Value) SetByPath(sub Node, path ...Path) (exist bool, err error)
- func (self *Value) SetMany(pathes []PathNode, opts *Options, root *Value, address []int, path ...Path) (err error)
- func (self Value) StrMap(opts *Options) (map[string]interface{}, 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 proto value // This only works for single-value searching API like GetByInt()/GetByRaw()/GetByStr()/Field()/Index()/GetByPath() methods. 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 DefaultTagSliceCap = 8 )
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 FreeBytesToPool ¶
func FreeBytesToPool(b []byte)
func NewBytesFromPool ¶
func NewBytesFromPool() []byte
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
func NewComplexNode ¶
NewComplexNode can deal with all the types, only if the src is a valid byte slice
func NewNodeBool ¶
func NewNodeByte ¶
func NewNodeBytes ¶
func NewNodeDouble ¶
func NewNodeEnum ¶
func NewNodeFixed32 ¶
func NewNodeFixed64 ¶
func NewNodeFloat ¶
func NewNodeInt32 ¶
func NewNodeInt64 ¶
func NewNodeSfixed32 ¶
func NewNodeSfixed64 ¶
func NewNodeSint32 ¶
func NewNodeSint64 ¶
func NewNodeString ¶
func NewNodeUint32 ¶
func NewNodeUint64 ¶
func (Node) Children ¶
func (self Node) Children(out *[]PathNode, recurse bool, opts *Options, desc *proto.TypeDescriptor) (err error)
returns all the children of a node, when recurse is false, it switch to lazyload mode, only direct children are returned
func (Node) Field ¶
func (self Node) Field(id proto.FieldNumber, rootLayer bool, msgDesc *proto.MessageDescriptor) (v Node, f *proto.FieldDescriptor)
func (Node) Int ¶
Int returns the int value contained by a INT32/SINT32/SFIX32/INT64/SINT64/SFIX64 node
func (Node) IsErrNotFound ¶
IsErrorNotFound tells if the node is not-found-data error
func (*Node) SetElemType ¶
func (*Node) SetKeyType ¶
type Options ¶
type Options struct { // DisallowUnknown indicates to report error when read unknown fields. DisallowUnknown bool // MapStructById indicates to use FieldId instead of int as map key instead of when call Node.Interface() on STRUCT type. MapStructById bool // ClearDirtyValues indicates one multi-query (includeing // Fields()/GetMany()/Gets()/Indexies()) to clear out all nodes // in passed []PathNode first ClearDirtyValues bool // CastStringAsBinary indicates to cast STRING type to []byte when call Node.Interface()/Map(). CastStringAsBinary bool WriteDefault bool // not implemented UseNativeSkip bool // not implemented NotScanParentNode bool // not implemented StoreChildrenById bool // not implemented StoreChildrenByHash bool // not implemented IterateStructByName bool // not implemented }
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 ¶
func NewPathFieldId(id proto.FieldNumber) Path
NewPathFieldId creates a PathFieldId path
func NewPathFieldName ¶
NewPathFieldName creates a PathFieldName path
func (Path) Id ¶
func (self Path) Id() proto.FieldNumber
Id returns the field id of a PathFieldId path
type PathNode ¶
PathNode is a three node of DOM tree
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
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
type PathType ¶
type PathType uint8
PathType is the type of path
const ( // PathFieldId represents a field id of MESSAGE type PathFieldId PathType = 1 + iota // PathFieldName represents a field name of MESSAGE type // NOTICE: it is only supported by Value PathFieldName // PathIndex represents a index of LIST 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 // not supported in protobuf MapKeyDescriptor )
type Value ¶
type Value struct { Node Desc *proto.TypeDescriptor IsRoot bool }
func NewComplexValue ¶
func NewComplexValue(desc *proto.TypeDescriptor, src []byte) Value
only for LIST/MAP parent Node
func NewRootValue ¶
func NewRootValue(desc *proto.TypeDescriptor, src []byte) Value
func (Value) Field ¶
func (self Value) Field(id proto.FieldNumber) (v Value)
Field returns a sub node at the given field id from a MESSAGE value.
func (Value) FieldByName ¶
FieldByName returns a sub node at the given field name from a MESSAGE value.
func (Value) GetByPathWithAddress ¶
func (Value) GetMany ¶
GetMany searches transversely and returns all the sub nodes along with the given pathes.
Example ¶
desc := getExample3Desc() data := getExample3Data() v := NewRootValue(desc, data) ps := []PathNode{ {Path: NewPathFieldId(1)}, {Path: NewPathFieldId(3)}, {Path: NewPathFieldId(32767)}, } err := v.GetMany(ps, opts) if err != nil { panic(err) }
Output:
func (Value) Interface ¶
Interface returns the go interface value contained by a node. If the node is a MESSAGE, it will return map[proto.FieldNumber]interface{} or map[int]interface{}. If it is a map, it will return map[int|string]interface{}, which depends on the key type
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.
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 ¶
desc := getExample3Desc() data := getExample3Data() v := NewRootValue(desc, data) p := binary.NewBinaryProtol([]byte{}) exp := "中文" p.WriteString(exp) buf := p.RawBuf() vv := NewNode(proto.STRING, buf) ps := []Path{NewPathFieldName("InnerBase2"), NewPathFieldName("Base"), NewPathFieldName("Extra"), NewPathStrKey("b")} exist, err2 := v.SetByPath(vv, ps...) if err2 != nil { panic(err2) } fmt.Println(exist) // false // check inserted value s2 := v.GetByPath(ps...) if s2.Error() != "" { panic(s2.Error()) } f2, _ := s2.String() fmt.Println(f2) // 中文
Output:
func (*Value) SetMany ¶
func (self *Value) SetMany(pathes []PathNode, opts *Options, root *Value, address []int, path ...Path) (err error)
SetMany: set a list of sub nodes at the given pathes from the value. root *Value: the root Node self *Value: the current Node (maybe root Node) address []int: the address from target Nodes to the root Node path ...Path: the path from root Node to target Nodes
func (*Value) UnsetByPath ¶
UnsetByPath searches longitudinally and unsets a sub value at the given path from the value.