Skip to content

Latest commit

 

History

History
1141 lines (772 loc) · 30 KB

README.md

File metadata and controls

1141 lines (772 loc) · 30 KB

generic

import "github.com/cloudwego/dynamicgo/proto/generic"

Index

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
)

func FreeBytesToPool(b []byte)

func FreePathNode(p *PathNode)

FreePathNode put a PathNode back to memory pool

func NewBytesFromPool() []byte

type Node

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

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 (Node) Binary

func (self Node) Binary() ([]byte, error)

Binary returns the bytes value contained by a BYTE node

func (Node) Bool

func (self Node) Bool() (bool, error)

Bool returns the bool value contained by a BOOL node

func (*Node) Check

func (self *Node) Check() error

Check checks if it is a ERROR node and returns corresponding error

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) ElemType

func (self Node) ElemType() proto.Type

ElemType returns the thrift type of a LIST/MAP node's element

func (Node) ErrCode

func (self Node) ErrCode() meta.ErrCode

ErrCode return the meta.ErrCode of a ERROR node

func (Node) Error

func (self Node) Error() string

Error return error message if it is a ERROR node

func (Node) Field

func (self Node) Field(id proto.FieldNumber, rootLayer bool, msgDesc *proto.MessageDescriptor) (v Node, f *proto.FieldDescriptor)

func (Node) Fields

func (self Node) Fields(ids []PathNode, rootLayer bool, msgDesc *proto.MessageDescriptor, opts *Options) error

func (Node) Float64

func (self Node) Float64() (float64, error)

Float64 returns the float64 value contained by a DOUBLE node

func (Node) Fork

func (self Node) Fork() Node

Fork forks the node to a new node, copy underlying data as well

func (Node) GetByInt

func (self Node) GetByInt(key int) (v Node)

Get int key of a MAP node

func (Node) GetByStr

func (self Node) GetByStr(key string) (v Node)

Get string key of a MAP node

func (Node) Gets

func (self Node) Gets(keys []PathNode, opts *Options) error

func (Node) Index

func (self Node) Index(idx int) (v Node)

Get idx element of a LIST node

func (Node) Indexes

func (self Node) Indexes(ins []PathNode, opts *Options) error

func (Node) Int

func (self Node) Int() (int, error)

Int returns the int value contained by a INT32/SINT32/SFIX32/INT64/SINT64/SFIX64 node

func (Node) IsErrNotFound

func (self Node) IsErrNotFound() bool

IsErrorNotFound tells if the node is not-found-data error

func (Node) IsError

func (self Node) IsError() bool

IsEmtpy tells if the node is PROTOBUF.ERROR

func (Node) IsUnKnown

func (self Node) IsUnKnown() bool

IsEmpty tells if the node is proto.UNKNOWN

func (Node) KeyType

func (self Node) KeyType() proto.Type

KeyType returns the thrift type of a MAP node's key

func (Node) Len

func (self Node) Len() (int, error)

Len returns the element count of container-kind type (LIST/MAP)

func (Node) Raw

func (self Node) Raw() []byte

Return its underlying raw data

func (*Node) SetElemType

func (self *Node) SetElemType(et proto.Type)

func (*Node) SetKeyType

func (self *Node) SetKeyType(kt proto.Type)

func (Node) String

func (self Node) String() (string, error)

String returns the string value contianed by a STRING node

func (Node) Type

func (self Node) Type() proto.Type

Type returns the proto type of the node

func (Node) Uint

func (self Node) Uint() (uint, error)

Uint returns the uint value contained by a UINT32/UINT64/FIX32/FIX64 node

type Options

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
}

type Path

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 (Path) Id

func (self Path) Id() proto.FieldNumber

Id returns the field id of a PathFieldId path

func (Path) Int

func (self Path) Int() int

Int returns the int value of a PathIndexPathIntKey path

func (Path) Str

func (self Path) Str() string

Str returns the string value of a PathFieldNamePathStrKey path

func (Path) ToRaw

func (self Path) ToRaw(t proto.Type) []byte

ToRaw converts underlying value to protobuf-encoded bytes

func (Path) Type

func (self Path) Type() PathType

Type returns the type of a Path

func (Path) Value

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 (PathNode) CopyTo

func (self PathNode) CopyTo(to *PathNode)

CopyTo deeply copy self and its children to a PathNode

func (*PathNode) Load

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 (PathNode) Marshal

func (self PathNode) Marshal(opt *Options) (out []byte, err error)

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

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

type Value struct {
    Node
    Desc   *proto.TypeDescriptor
    IsRoot bool
}
Example (-arshal All)

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 (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

func (self Value) FieldByName(name string) (v Value)

FieldByName returns a sub node at the given field name from a MESSAGE value.

func (Value) Fields

func (self Value) Fields(ids []PathNode, opts *Options) error

func (Value) Fork

func (self Value) Fork() Value

NewValueFromNode copy both Node and TypeDescriptor from another Value.

func (Value) GetByInt

func (self Value) GetByInt(key int) (v Value)

GetByInt returns a sub node at the given key from a MAP value.

func (Value) GetByPath

func (self Value) GetByPath(pathes ...Path) Value

func (self Value) GetByPathWithAddress(pathes ...Path) (Value, []int)

func (Value) GetByStr

func (self Value) GetByStr(key string) (v Value)

GetByInt returns a sub node at the given key from a MAP value.

func (Value) GetMany

func (self Value) GetMany(pathes []PathNode, opts *Options) error

GetMany searches transversely and returns all the sub nodes along with the given pathes.

Example

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 (Value) Index

func (self Value) Index(i int) (v Value)

Index returns a sub node at the given index from a LIST value.

func (Value) IntMap

func (self Value) IntMap(opts *Options) (map[int]interface{}, error)

func (Value) Interface

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 (Value) List

func (self Value) List(opts *Options) ([]interface{}, error)

func (Value) MarshalTo

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 (*Value) SetByPath

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.

Example

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 (*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) StrMap

func (self Value) StrMap(opts *Options) (map[string]interface{}, error)

func (*Value) UnsetByPath

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