import "github.com/cloudwego/dynamicgo/proto/generic"
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
)
func FreeBytesToPool(b []byte)
func FreePathNode(p *PathNode)
FreePathNode put a PathNode back to memory pool
func NewBytesFromPool() []byte
type Node struct {
// contains filtered or unexported fields
}
func NewComplexNode(t proto.Type, et proto.Type, kt proto.Type, src []byte) (ret Node)
NewComplexNode can deal with all the types, only if the src is a valid byte slice
func NewNode(t proto.Type, src []byte) Node
NewNode method: creates a new node from a byte slice
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)
Binary returns the bytes value contained by a BYTE node
func (self Node) Bool() (bool, error)
Bool returns the bool value contained by a BOOL node
func (self *Node) Check() error
Check checks if it is a ERROR node and returns corresponding error
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 (self Node) ElemType() proto.Type
ElemType returns the thrift type of a LIST/MAP node's element
func (self Node) ErrCode() meta.ErrCode
ErrCode return the meta.ErrCode of a ERROR node
func (self Node) Error() string
Error return error message if it is a ERROR node
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, opts *Options) error
func (self Node) Float64() (float64, error)
Float64 returns the float64 value contained by a DOUBLE node
func (self Node) Fork() Node
Fork forks the node to a new node, copy underlying data as well
func (self Node) GetByInt(key int) (v Node)
Get int key of a MAP node
func (self Node) GetByStr(key string) (v Node)
Get string key of a MAP node
func (self Node) Gets(keys []PathNode, opts *Options) error
func (self Node) Index(idx int) (v Node)
Get idx element of a LIST node
func (self Node) Indexes(ins []PathNode, opts *Options) error
func (self Node) Int() (int, error)
Int returns the int value contained by a INT32/SINT32/SFIX32/INT64/SINT64/SFIX64 node
func (self Node) IsErrNotFound() bool
IsErrorNotFound tells if the node is not-found-data error
func (self Node) IsError() bool
IsEmtpy tells if the node is PROTOBUF.ERROR
func (self Node) IsUnKnown() bool
IsEmpty tells if the node is proto.UNKNOWN
func (self Node) KeyType() proto.Type
KeyType returns the thrift type of a MAP node's key
func (self Node) Len() (int, error)
Len returns the element count of container-kind type (LIST/MAP)
func (self Node) Raw() []byte
Return its underlying raw data
func (self *Node) SetElemType(et proto.Type)
func (self *Node) SetKeyType(kt proto.Type)
func (self Node) String() (string, error)
String returns the string value contianed by a STRING node
func (self Node) Type() proto.Type
Type returns the proto type of the node
func (self Node) Uint() (uint, error)
Uint returns the uint value contained by a UINT32/UINT64/FIX32/FIX64 node
Opions for generic.Node
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
}
Path represents the relative position of a sub node in a complex parent node
type Path struct {
// contains filtered or unexported fields
}
func NewPathFieldId(id proto.FieldNumber) Path
NewPathFieldId creates a PathFieldId path
func NewPathFieldName(name string) Path
NewPathFieldName creates a PathFieldName path
func NewPathIndex(index int) Path
NewPathIndex creates a PathIndex path
func NewPathIntKey(key int) Path
NewPathIntKey creates a PathIntKey path
func NewPathStrKey(key string) Path
NewPathStrKey creates a PathStrKey path
func (self Path) Id() proto.FieldNumber
Id returns the field id of a PathFieldId path
func (self Path) Int() int
Int returns the int value of a PathIndexPathIntKey path
func (self Path) Str() string
Str returns the string value of a PathFieldNamePathStrKey path
func (self Path) ToRaw(t proto.Type) []byte
ToRaw converts underlying value to protobuf-encoded bytes
func (self Path) Type() PathType
Type returns the type of a Path
func (self Path) Value() interface{}
Value returns the equivalent go interface of a Path
PathNode is a three node of DOM tree
type PathNode struct {
Path
Node
Next []PathNode
}
func NewPathNode() *PathNode
NewPathNode get a new PathNode from memory pool
func (self PathNode) CopyTo(to *PathNode)
CopyTo deeply copy self and its children to a PathNode
func (self *PathNode) Load(recurse bool, opts *Options, desc *proto.TypeDescriptor) error
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 (self PathNode) Marshal(opt *Options) (out []byte, err error)
func (self *PathNode) ResetAll()
ResetAll resets self and its children, including path and node both
func (self *PathNode) ResetValue()
ResetValue resets self's node and its children's node
PathType is the type of path
type PathType uint8
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 struct {
Node
Desc *proto.TypeDescriptor
IsRoot bool
}
desc := getExample2Desc()
data := getExample2Data()
v := NewRootValue(desc, data)
p := PathNode{
Node: v.Node,
}
if err := p.Load(true, opts, desc); err != nil {
panic(err)
}
out, err := p.Marshal(opts)
if err != nil {
panic(err)
}
msg, err := NewRootValue(desc, out).Interface(opts)
if err != nil {
panic(err)
}
fmt.Printf("%#v", msg)
func NewComplexValue(desc *proto.TypeDescriptor, src []byte) Value
only for LIST/MAP parent Node
func NewRootValue(desc *proto.TypeDescriptor, src []byte) Value
func NewValue(desc *proto.TypeDescriptor, src []byte) Value
only for basic Node
func (self Value) Field(id proto.FieldNumber) (v Value)
Field returns a sub node at the given field id from a MESSAGE value.
func (self Value) FieldByName(name string) (v Value)
FieldByName returns a sub node at the given field name from a MESSAGE value.
func (self Value) Fields(ids []PathNode, opts *Options) error
func (self Value) Fork() Value
NewValueFromNode copy both Node and TypeDescriptor from another Value.
func (self Value) GetByInt(key int) (v Value)
GetByInt returns a sub node at the given key from a MAP value.
func (self Value) GetByPath(pathes ...Path) Value
func (self Value) GetByPathWithAddress(pathes ...Path) (Value, []int)
func (self Value) GetByStr(key string) (v Value)
GetByInt returns a sub node at the given key from a MAP value.
func (self Value) GetMany(pathes []PathNode, opts *Options) error
GetMany searches transversely and returns all the sub nodes along with the given pathes.
desc := getExample2Desc()
data := getExample2Data()
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)
}
func (self Value) Index(i int) (v Value)
Index returns a sub node at the given index from a LIST value.
func (self Value) IntMap(opts *Options) (map[int]interface{}, error)
func (self Value) Interface(opts *Options) (interface{}, error)
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 (self Value) List(opts *Options) ([]interface{}, error)
func (self Value) MarshalTo(to *proto.TypeDescriptor, opts *Options) ([]byte, error)
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 (self *Value) SetByPath(sub Node, path ...Path) (exist bool, err error)
SetByPath searches longitudinally and sets a sub value at the given path from the value. exist tells whether the node is already exists.
desc := getExample2Desc()
data := getExample2Data()
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) // 中文
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 (self Value) StrMap(opts *Options) (map[string]interface{}, error)
func (self *Value) UnsetByPath(path ...Path) error
UnsetByPath searches longitudinally and unsets a sub value at the given path from the value.
Generated by gomarkdoc
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。