github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/generic/README.md (about) 1 <!-- Code generated by gomarkdoc. DO NOT EDIT --> 2 3 # generic 4 5 ```go 6 import "github.com/cloudwego/dynamicgo/thrift/generic" 7 ``` 8 9 ## Index 10 11 - [Variables](<#variables>) 12 - [func DescriptorToPathNode(desc *thrift.TypeDescriptor, root *PathNode, opts *Options) error](<#func-descriptortopathnode>) 13 - [func FreePathNode(p *PathNode)](<#func-freepathnode>) 14 - [func GetDescByPath(desc *thrift.TypeDescriptor, path ...Path) (ret *thrift.TypeDescriptor, err error)](<#func-getdescbypath>) 15 - [type Node](<#type-node>) 16 - [func NewNode(t thrift.Type, src []byte) Node](<#func-newnode>) 17 - [func NewNodeBinary(val []byte) Node](<#func-newnodebinary>) 18 - [func NewNodeBool(val bool) Node](<#func-newnodebool>) 19 - [func NewNodeByte(val byte) Node](<#func-newnodebyte>) 20 - [func NewNodeDouble(val float64) Node](<#func-newnodedouble>) 21 - [func NewNodeInt16(val int16) Node](<#func-newnodeint16>) 22 - [func NewNodeInt32(val int32) Node](<#func-newnodeint32>) 23 - [func NewNodeInt64(val int64) Node](<#func-newnodeint64>) 24 - [func NewNodeString(val string) Node](<#func-newnodestring>) 25 - [func NewTypedNode(typ thrift.Type, et thrift.Type, kt thrift.Type) (ret Node)](<#func-newtypednode>) 26 - [func (self Node) Binary() ([]byte, error)](<#func-node-binary>) 27 - [func (self Node) Bool() (bool, error)](<#func-node-bool>) 28 - [func (self Node) Byte() (byte, error)](<#func-node-byte>) 29 - [func (self *Node) Check() error](<#func-node-check>) 30 - [func (self Node) Children(out *[]PathNode, recurse bool, opts *Options) (err error)](<#func-node-children>) 31 - [func (self Node) ElemType() thrift.Type](<#func-node-elemtype>) 32 - [func (self Node) ErrCode() meta.ErrCode](<#func-node-errcode>) 33 - [func (self Node) Error() string](<#func-node-error>) 34 - [func (self Node) Field(id thrift.FieldID) (v Node)](<#func-node-field>) 35 - [func (self Node) Fields(ids []PathNode, opts *Options) (err error)](<#func-node-fields>) 36 - [func (self Node) Float64() (float64, error)](<#func-node-float64>) 37 - [func (self Node) Foreach(handler func(path Path, node Node) bool, opts *Options) error](<#func-node-foreach>) 38 - [func (self Node) ForeachKV(handler func(key Node, val Node) bool, opts *Options) error](<#func-node-foreachkv>) 39 - [func (self Node) Fork() Node](<#func-node-fork>) 40 - [func (self Node) GetByInt(key int) (v Node)](<#func-node-getbyint>) 41 - [func (self Node) GetByPath(pathes ...Path) Node](<#func-node-getbypath>) 42 - [func (self Node) GetByRaw(key []byte) (v Node)](<#func-node-getbyraw>) 43 - [func (self Node) GetByStr(key string) (v Node)](<#func-node-getbystr>) 44 - [func (self Node) GetMany(pathes []PathNode, opts *Options) error](<#func-node-getmany>) 45 - [func (self Node) GetTree(tree *PathNode, opts *Options) error](<#func-node-gettree>) 46 - [func (self Node) Gets(keys []PathNode, opts *Options) (err error)](<#func-node-gets>) 47 - [func (self Node) Index(i int) (v Node)](<#func-node-index>) 48 - [func (self Node) Indexes(ins []PathNode, opts *Options) (err error)](<#func-node-indexes>) 49 - [func (self Node) Int() (int, error)](<#func-node-int>) 50 - [func (self Node) IntMap(opts *Options) (map[int]interface{}, error)](<#func-node-intmap>) 51 - [func (self Node) Interface(opts *Options) (interface{}, error)](<#func-node-interface>) 52 - [func (self Node) InterfaceMap(opts *Options) (map[interface{}]interface{}, error)](<#func-node-interfacemap>) 53 - [func (self Node) IsEmpty() bool](<#func-node-isempty>) 54 - [func (self Node) IsErrNotFound() bool](<#func-node-iserrnotfound>) 55 - [func (self Node) IsError() bool](<#func-node-iserror>) 56 - [func (self Node) KeyType() thrift.Type](<#func-node-keytype>) 57 - [func (self Node) Len() (int, error)](<#func-node-len>) 58 - [func (self Node) List(opts *Options) ([]interface{}, error)](<#func-node-list>) 59 - [func (self Node) Raw() []byte](<#func-node-raw>) 60 - [func (self *Node) SetByPath(sub Node, path ...Path) (exist bool, err error)](<#func-node-setbypath>) 61 - [func (self *Node) SetMany(pathes []PathNode, opts *Options) (err error)](<#func-node-setmany>) 62 - [func (self Node) StrMap(opts *Options) (map[string]interface{}, error)](<#func-node-strmap>) 63 - [func (self Node) String() (string, error)](<#func-node-string>) 64 - [func (self Node) Type() thrift.Type](<#func-node-type>) 65 - [func (self *Node) UnsetByPath(path ...Path) error](<#func-node-unsetbypath>) 66 - [type Options](<#type-options>) 67 - [type Path](<#type-path>) 68 - [func NewPathBinKey(key []byte) Path](<#func-newpathbinkey>) 69 - [func NewPathFieldId(id thrift.FieldID) Path](<#func-newpathfieldid>) 70 - [func NewPathFieldName(name string) Path](<#func-newpathfieldname>) 71 - [func NewPathIndex(index int) Path](<#func-newpathindex>) 72 - [func NewPathIntKey(key int) Path](<#func-newpathintkey>) 73 - [func NewPathStrKey(key string) Path](<#func-newpathstrkey>) 74 - [func (self Path) Bin() []byte](<#func-path-bin>) 75 - [func (self Path) Id() thrift.FieldID](<#func-path-id>) 76 - [func (self Path) Int() int](<#func-path-int>) 77 - [func (self Path) Str() string](<#func-path-str>) 78 - [func (self Path) String() string](<#func-path-string>) 79 - [func (self Path) ToRaw(t thrift.Type) []byte](<#func-path-toraw>) 80 - [func (self Path) Type() PathType](<#func-path-type>) 81 - [func (self Path) Value() interface{}](<#func-path-value>) 82 - [type PathNode](<#type-pathnode>) 83 - [func NewPathNode() *PathNode](<#func-newpathnode>) 84 - [func (self *PathNode) Assgin(recurse bool, opts *Options) error](<#func-pathnode-assgin>) 85 - [func (self *PathNode) Check() error](<#func-pathnode-check>) 86 - [func (self PathNode) CopyTo(to *PathNode)](<#func-pathnode-copyto>) 87 - [func (self PathNode) Error() string](<#func-pathnode-error>) 88 - [func (self *PathNode) Field(id thrift.FieldID, opts *Options) *PathNode](<#func-pathnode-field>) 89 - [func (self PathNode) Fork() PathNode](<#func-pathnode-fork>) 90 - [func (self *PathNode) GetByInt(key int, opts *Options) *PathNode](<#func-pathnode-getbyint>) 91 - [func (self *PathNode) GetByStr(key string, opts *Options) *PathNode](<#func-pathnode-getbystr>) 92 - [func (self *PathNode) Load(recurse bool, opts *Options) error](<#func-pathnode-load>) 93 - [func (self PathNode) Marshal(opt *Options) (out []byte, err error)](<#func-pathnode-marshal>) 94 - [func (self PathNode) MarshalIntoBuffer(out *[]byte, opt *Options) error](<#func-pathnode-marshalintobuffer>) 95 - [func (self *PathNode) ResetAll()](<#func-pathnode-resetall>) 96 - [func (self *PathNode) ResetValue()](<#func-pathnode-resetvalue>) 97 - [func (self *PathNode) SetByInt(key int, val Node, opts *Options) (bool, error)](<#func-pathnode-setbyint>) 98 - [func (self *PathNode) SetByStr(key string, val Node, opts *Options) (bool, error)](<#func-pathnode-setbystr>) 99 - [func (self *PathNode) SetField(id thrift.FieldID, val Node, opts *Options) (bool, error)](<#func-pathnode-setfield>) 100 - [type PathType](<#type-pathtype>) 101 - [type Value](<#type-value>) 102 - [func NewValue(desc *thrift.TypeDescriptor, src []byte) Value](<#func-newvalue>) 103 - [func (self Value) Field(id thrift.FieldID) (v Value)](<#func-value-field>) 104 - [func (self Value) FieldByName(name string) (v Value)](<#func-value-fieldbyname>) 105 - [func (self Value) Fork() Value](<#func-value-fork>) 106 - [func (self Value) GetByInt(key int) (v Value)](<#func-value-getbyint>) 107 - [func (self Value) GetByPath(pathes ...Path) Value](<#func-value-getbypath>) 108 - [func (self Value) GetByStr(key string) (v Value)](<#func-value-getbystr>) 109 - [func (self Value) Index(i int) (v Value)](<#func-value-index>) 110 - [func (self Value) MarshalTo(to *thrift.TypeDescriptor, opts *Options) ([]byte, error)](<#func-value-marshalto>) 111 - [func (self *Value) SetByPath(sub Value, path ...Path) (exist bool, err error)](<#func-value-setbypath>) 112 - [func (self *Value) UnsetByPath(path ...Path) error](<#func-value-unsetbypath>) 113 114 115 ## Variables 116 117 ```go 118 var ( 119 // UseNativeSkipForGet indicates to use native.Skip (instead of go.Skip) method to skip thrift value 120 // This only works for single-value searching API like GetByInt()/GetByRaw()/GetByStr()/Field()/Index()/GetByPath() methods. 121 // WARN: this will promote performance when thrift value to be skipped is large, but may decrease preformance when thrift value is small. 122 UseNativeSkipForGet = false 123 124 // DefaultNodeSliceCap is the default capacity of a Node or NodePath slice 125 // Usually, a Node or NodePath slice is used to store intermediate or consequential elements of a generic API like Children()|Interface()|SetMany() 126 DefaultNodeSliceCap = 16 127 ) 128 ``` 129 130 ```go 131 var ( 132 // StoreChildrenByIdShreshold is the maximum id to store children node by id. 133 StoreChildrenByIdShreshold = 256 134 135 // StoreChildrenByIdShreshold is the minimum id to store children node by hash. 136 StoreChildrenByIntHashShreshold = DefaultNodeSliceCap 137 ) 138 ``` 139 140 ## func DescriptorToPathNode 141 142 ```go 143 func DescriptorToPathNode(desc *thrift.TypeDescriptor, root *PathNode, opts *Options) error 144 ``` 145 146 DescriptorToPathNode converts a thrift type descriptor to a DOM, assgining path to root NOTICE: it only recursively converts STRUCT type 147 148 ## func FreePathNode 149 150 ```go 151 func FreePathNode(p *PathNode) 152 ``` 153 154 FreePathNode put a PathNode back to memory pool 155 156 ## func GetDescByPath 157 158 ```go 159 func GetDescByPath(desc *thrift.TypeDescriptor, path ...Path) (ret *thrift.TypeDescriptor, err error) 160 ``` 161 162 GetDescByPath searches longitudinally and returns the sub descriptor of the desc specified by path 163 164 ## type Node 165 166 Node is a generic wrap of raw thrift data 167 168 ```go 169 type Node struct { 170 // contains filtered or unexported fields 171 } 172 ``` 173 174 ### func NewNode 175 176 ```go 177 func NewNode(t thrift.Type, src []byte) Node 178 ``` 179 180 NewNodeBool create a new node with given type and data WARN: it WON'T check the correctness of the data 181 182 ### func NewNodeBinary 183 184 ```go 185 func NewNodeBinary(val []byte) Node 186 ``` 187 188 NewNodeBinary converts a \[\]byte value to a STRING node 189 190 ### func NewNodeBool 191 192 ```go 193 func NewNodeBool(val bool) Node 194 ``` 195 196 NewNodeBool converts a bool value to a BOOL node 197 198 ### func NewNodeByte 199 200 ```go 201 func NewNodeByte(val byte) Node 202 ``` 203 204 NewNodeInt8 converts a byte value to a BYTE node 205 206 ### func NewNodeDouble 207 208 ```go 209 func NewNodeDouble(val float64) Node 210 ``` 211 212 NewNodeDouble converts a float64 value to a DOUBLE node 213 214 ### func NewNodeInt16 215 216 ```go 217 func NewNodeInt16(val int16) Node 218 ``` 219 220 NewNodeInt16 converts a int16 value to a I16 node 221 222 ### func NewNodeInt32 223 224 ```go 225 func NewNodeInt32(val int32) Node 226 ``` 227 228 NewNodeInt32 converts a int32 value to a I32 node 229 230 ### func NewNodeInt64 231 232 ```go 233 func NewNodeInt64(val int64) Node 234 ``` 235 236 NewNodeInt64 converts a int64 value to a I64 node 237 238 ### func NewNodeString 239 240 ```go 241 func NewNodeString(val string) Node 242 ``` 243 244 NewNodeString converts a string value to a STRING node 245 246 ### func NewTypedNode 247 248 ```go 249 func NewTypedNode(typ thrift.Type, et thrift.Type, kt thrift.Type) (ret Node) 250 ``` 251 252 NewTypedNode creates a new Node with the given typ, including element type \(for LIST/SET/MAP\) and key type \(for MAP\) 253 254 ### func \(Node\) Binary 255 256 ```go 257 func (self Node) Binary() ([]byte, error) 258 ``` 259 260 Binary returns the bytes value contained by a BINARY node 261 262 ### func \(Node\) Bool 263 264 ```go 265 func (self Node) Bool() (bool, error) 266 ``` 267 268 Bool returns the bool value contained by a BOOL node 269 270 ### func \(Node\) Byte 271 272 ```go 273 func (self Node) Byte() (byte, error) 274 ``` 275 276 Byte returns the byte value contained by a I8/BYTE node 277 278 ### func \(\*Node\) Check 279 280 ```go 281 func (self *Node) Check() error 282 ``` 283 284 Check checks if it is a ERROR node and returns corresponding error 285 286 ### func \(Node\) Children 287 288 ```go 289 func (self Node) Children(out *[]PathNode, recurse bool, opts *Options) (err error) 290 ``` 291 292 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. 293 294 NOTICE: if opts.NotScanParentNode is true, the parent nodes \(PathNode.Node\) of complex \(LIST/SET/MAP/STRUCT\) type won't be assgined data 295 296 ### func \(Node\) ElemType 297 298 ```go 299 func (self Node) ElemType() thrift.Type 300 ``` 301 302 ElemType returns the thrift type of a LIST/SET/MAP node's element 303 304 ### func \(Node\) ErrCode 305 306 ```go 307 func (self Node) ErrCode() meta.ErrCode 308 ``` 309 310 ErrCode return the meta.ErrCode of a ERROR node 311 312 ### func \(Node\) Error 313 314 ```go 315 func (self Node) Error() string 316 ``` 317 318 Error return error message if it is a ERROR node 319 320 ### func \(Node\) Field 321 322 ```go 323 func (self Node) Field(id thrift.FieldID) (v Node) 324 ``` 325 326 Field returns a sub node at the given field id from a STRUCT node. 327 328 ### func \(Node\) Fields 329 330 ```go 331 func (self Node) Fields(ids []PathNode, opts *Options) (err error) 332 ``` 333 334 Fields returns all sub nodes ids along with the given int path from a STRUCT node. 335 336 ### func \(Node\) Float64 337 338 ```go 339 func (self Node) Float64() (float64, error) 340 ``` 341 342 Float64 returns the float64 value contained by a DOUBLE node 343 344 ### func \(Node\) Foreach 345 346 ```go 347 func (self Node) Foreach(handler func(path Path, node Node) bool, opts *Options) error 348 ``` 349 350 Foreach scan each element of a complex type \(LIST/SET/MAP/STRUCT\), and call handler sequentially with corresponding path and node 351 352 ### func \(Node\) ForeachKV 353 354 ```go 355 func (self Node) ForeachKV(handler func(key Node, val Node) bool, opts *Options) error 356 ``` 357 358 ForeachKV scan each element of a MAP type, and call handler sequentially with corresponding key and value 359 360 ### func \(Node\) Fork 361 362 ```go 363 func (self Node) Fork() Node 364 ``` 365 366 Fork forks the node to a new node, copy underlying data as well 367 368 ### func \(Node\) GetByInt 369 370 ```go 371 func (self Node) GetByInt(key int) (v Node) 372 ``` 373 374 GetByInt returns a sub node at the given int key from a MAP\<INT,xx\> node. 375 376 ### func \(Node\) GetByPath 377 378 ```go 379 func (self Node) GetByPath(pathes ...Path) Node 380 ``` 381 382 GetByPath searches longitudinally and return a sub node at the given path from the node. 383 384 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. 385 386 ### func \(Node\) GetByRaw 387 388 ```go 389 func (self Node) GetByRaw(key []byte) (v Node) 390 ``` 391 392 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. 393 394 ### func \(Node\) GetByStr 395 396 ```go 397 func (self Node) GetByStr(key string) (v Node) 398 ``` 399 400 GetByInt returns a sub node at the given int key from a MAP\<STRING,xx\> node. 401 402 ### func \(Node\) GetMany 403 404 ```go 405 func (self Node) GetMany(pathes []PathNode, opts *Options) error 406 ``` 407 408 GetMany searches transversely and returns all the sub nodes along with the given pathes. 409 410 ### func \(Node\) GetTree 411 412 ```go 413 func (self Node) GetTree(tree *PathNode, opts *Options) error 414 ``` 415 416 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. 417 418 ### func \(Node\) Gets 419 420 ```go 421 func (self Node) Gets(keys []PathNode, opts *Options) (err error) 422 ``` 423 424 Gets returns all sub nodes along with the given key \(PathStrKey|PathIntKey|PathBinKey\) path from a MAP node. 425 426 ### func \(Node\) Index 427 428 ```go 429 func (self Node) Index(i int) (v Node) 430 ``` 431 432 Index returns a sub node at the given index from a LIST/SET node. 433 434 ### func \(Node\) Indexes 435 436 ```go 437 func (self Node) Indexes(ins []PathNode, opts *Options) (err error) 438 ``` 439 440 Indexes returns all sub nodes along with the given int path from a LIST/SET node. 441 442 ### func \(Node\) Int 443 444 ```go 445 func (self Node) Int() (int, error) 446 ``` 447 448 Int returns the int value contaned by a I8/I16/I32/I64 node 449 450 ### func \(Node\) IntMap 451 452 ```go 453 func (self Node) IntMap(opts *Options) (map[int]interface{}, error) 454 ``` 455 456 StrMap returns the integer keys and interface elements contained by a MAP\<I8|I16|I32|I64,XX\> node 457 458 ### func \(Node\) Interface 459 460 ```go 461 func (self Node) Interface(opts *Options) (interface{}, error) 462 ``` 463 464 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 465 466 ### func \(Node\) InterfaceMap 467 468 ```go 469 func (self Node) InterfaceMap(opts *Options) (map[interface{}]interface{}, error) 470 ``` 471 472 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 473 474 ### func \(Node\) IsEmpty 475 476 ```go 477 func (self Node) IsEmpty() bool 478 ``` 479 480 IsEmpty tells if the node is thrift.STOP 481 482 ### func \(Node\) IsErrNotFound 483 484 ```go 485 func (self Node) IsErrNotFound() bool 486 ``` 487 488 IsErrorNotFound tells if the node is not\-found\-data error 489 490 ### func \(Node\) IsError 491 492 ```go 493 func (self Node) IsError() bool 494 ``` 495 496 IsEmtpy tells if the node is thrift.ERROR 497 498 ### func \(Node\) KeyType 499 500 ```go 501 func (self Node) KeyType() thrift.Type 502 ``` 503 504 KeyType returns the thrift type of a MAP node's key 505 506 ### func \(Node\) Len 507 508 ```go 509 func (self Node) Len() (int, error) 510 ``` 511 512 Len returns the element count of container\-kind type \(LIST/SET/MAP\) 513 514 ### func \(Node\) List 515 516 ```go 517 func (self Node) List(opts *Options) ([]interface{}, error) 518 ``` 519 520 List returns interface elements contained by a LIST/SET node 521 522 ### func \(Node\) Raw 523 524 ```go 525 func (self Node) Raw() []byte 526 ``` 527 528 Return its underlying raw data 529 530 ### func \(\*Node\) SetByPath 531 532 ```go 533 func (self *Node) SetByPath(sub Node, path ...Path) (exist bool, err error) 534 ``` 535 536 SetByPath sets the sub node at the given path. 537 538 ### func \(\*Node\) SetMany 539 540 ```go 541 func (self *Node) SetMany(pathes []PathNode, opts *Options) (err error) 542 ``` 543 544 SetMany searches longitudinally and sets the sub nodes at the given pathes. 545 546 ### func \(Node\) StrMap 547 548 ```go 549 func (self Node) StrMap(opts *Options) (map[string]interface{}, error) 550 ``` 551 552 StrMap returns the string keys and interface elements contained by a MAP\<STRING,XX\> node 553 554 ### func \(Node\) String 555 556 ```go 557 func (self Node) String() (string, error) 558 ``` 559 560 String returns the string value contianed by a STRING node 561 562 ### func \(Node\) Type 563 564 ```go 565 func (self Node) Type() thrift.Type 566 ``` 567 568 Type returns the thrift type of the node 569 570 ### func \(\*Node\) UnsetByPath 571 572 ```go 573 func (self *Node) UnsetByPath(path ...Path) error 574 ``` 575 576 ## type Options 577 578 Opions for generic.Node 579 580 ```go 581 type Options struct { 582 // DisallowUnknow indicates to report error when read unknown fields. 583 DisallowUnknow bool 584 585 // WriteDefault indicates to write value if a DEFAULT requireness field is not set. 586 WriteDefault bool 587 588 // NoCheckRequireNess indicates not to check requiredness when writing. 589 NotCheckRequireNess bool 590 591 // UseNativeSkip indicates to use native.Skip (instead of go.Skip) method to skip thrift value 592 // WARN: this will promote performance when thrift value to be skipped is large, but may decrease preformance when thrift value is small. 593 UseNativeSkip bool 594 595 // MapStructById indicates to use field id as map key instead of when call Node.Interface() on STRUCT type. 596 MapStructById bool 597 598 // CastStringAsBinary indicates to cast STRING type to []byte when call Node.Interface()/Map(). 599 CastStringAsBinary bool 600 601 // NotScanParentNode indicates to only assign children node when PathNode.Load()/Node.Children. 602 // Thies will promote performance but may be misued when handle PathNode. 603 NotScanParentNode bool 604 605 // ClearDirtyValues indicates one multi-query (includeing 606 // Fields()/GetMany()/Gets()/Indexies()) to clear out all nodes 607 // in passed []PathNode first 608 ClearDirtyValues bool 609 610 // StoreChildrenById indicates to store children node by id when call Node.Children() or PathNode.Load(). 611 // When field id exceeds StoreChildrenByIdShreshold, children node will be stored sequentially after the threshold. 612 StoreChildrenById bool 613 614 // StoreChildrenByHash indicates to store children node by str hash (mod parent's size) when call Node.Children() or PathNode.Load(). 615 StoreChildrenByHash bool 616 } 617 ``` 618 619 ## type Path 620 621 Path represents the relative position of a sub node in a complex parent node 622 623 ```go 624 type Path struct { 625 // contains filtered or unexported fields 626 } 627 ``` 628 629 ### func NewPathBinKey 630 631 ```go 632 func NewPathBinKey(key []byte) Path 633 ``` 634 635 NewPathBinKey creates a PathBinKey path 636 637 ### func NewPathFieldId 638 639 ```go 640 func NewPathFieldId(id thrift.FieldID) Path 641 ``` 642 643 NewPathFieldId creates a PathFieldId path 644 645 ### func NewPathFieldName 646 647 ```go 648 func NewPathFieldName(name string) Path 649 ``` 650 651 NewPathFieldName creates a PathFieldName path 652 653 ### func NewPathIndex 654 655 ```go 656 func NewPathIndex(index int) Path 657 ``` 658 659 NewPathIndex creates a PathIndex path 660 661 ### func NewPathIntKey 662 663 ```go 664 func NewPathIntKey(key int) Path 665 ``` 666 667 NewPathIntKey creates a PathIntKey path 668 669 ### func NewPathStrKey 670 671 ```go 672 func NewPathStrKey(key string) Path 673 ``` 674 675 NewPathStrKey creates a PathStrKey path 676 677 ### func \(Path\) Bin 678 679 ```go 680 func (self Path) Bin() []byte 681 ``` 682 683 Bin returns the raw bytes value of a PathBinKey path 684 685 ### func \(Path\) Id 686 687 ```go 688 func (self Path) Id() thrift.FieldID 689 ``` 690 691 Id returns the field id of a PathFieldId path 692 693 ### func \(Path\) Int 694 695 ```go 696 func (self Path) Int() int 697 ``` 698 699 Int returns the int value of a PathIndex\\PathIntKey path 700 701 ### func \(Path\) Str 702 703 ```go 704 func (self Path) Str() string 705 ``` 706 707 Str returns the string value of a PathFieldName\\PathStrKey path 708 709 ### func \(Path\) String 710 711 ```go 712 func (self Path) String() string 713 ``` 714 715 String returns the string representation of a Path 716 717 ### func \(Path\) ToRaw 718 719 ```go 720 func (self Path) ToRaw(t thrift.Type) []byte 721 ``` 722 723 ToRaw converts underlying value to thrift\-encoded bytes 724 725 ### func \(Path\) Type 726 727 ```go 728 func (self Path) Type() PathType 729 ``` 730 731 Type returns the type of a Path 732 733 ### func \(Path\) Value 734 735 ```go 736 func (self Path) Value() interface{} 737 ``` 738 739 Value returns the equivalent go interface of a Path 740 741 ## type PathNode 742 743 PathNode is a three node of DOM tree 744 745 ```go 746 type PathNode struct { 747 Path 748 Node 749 Next []PathNode 750 } 751 ``` 752 753 ### func NewPathNode 754 755 ```go 756 func NewPathNode() *PathNode 757 ``` 758 759 NewPathNode get a new PathNode from memory pool 760 761 ### func \(\*PathNode\) Assgin 762 763 ```go 764 func (self *PathNode) Assgin(recurse bool, opts *Options) error 765 ``` 766 767 Assgin assigns self's raw Value according to its Next Path, which must be set before calling this method. 768 769 ### func \(\*PathNode\) Check 770 771 ```go 772 func (self *PathNode) Check() error 773 ``` 774 775 Check returns non\-nil error if the PathNode has error 776 777 ### func \(PathNode\) CopyTo 778 779 ```go 780 func (self PathNode) CopyTo(to *PathNode) 781 ``` 782 783 CopyTo deeply copy self and its children to a PathNode 784 785 ### func \(PathNode\) Error 786 787 ```go 788 func (self PathNode) Error() string 789 ``` 790 791 Error returns non\-empty string if the PathNode has error 792 793 ### func \(\*PathNode\) Field 794 795 ```go 796 func (self *PathNode) Field(id thrift.FieldID, opts *Options) *PathNode 797 ``` 798 799 Field get the child node by field id. Only support STRUCT. 800 801 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. 802 803 ### func \(PathNode\) Fork 804 805 ```go 806 func (self PathNode) Fork() PathNode 807 ``` 808 809 Fork deeply copy self and its children to a new PathNode 810 811 ### func \(\*PathNode\) GetByInt 812 813 ```go 814 func (self *PathNode) GetByInt(key int, opts *Options) *PathNode 815 ``` 816 817 GetByInt get the child node by integer. Only support MAP with integer\-type key. 818 819 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. 820 821 ### func \(\*PathNode\) GetByStr 822 823 ```go 824 func (self *PathNode) GetByStr(key string, opts *Options) *PathNode 825 ``` 826 827 GetByInt get the child node by string. Only support MAP with string\-type key. 828 829 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. 830 831 ### func \(\*PathNode\) Load 832 833 ```go 834 func (self *PathNode) Load(recurse bool, opts *Options) error 835 ``` 836 837 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 838 839 <details><summary>Example</summary> 840 <p> 841 842 ```go 843 { 844 845 data := getExampleData() 846 root := PathNode{ 847 Node: NewNode(thrift.STRUCT, data), 848 } 849 850 err := root.Load(false, opts) 851 if err != nil { 852 panic(err) 853 } 854 fmt.Printf("%#v", root) 855 856 err = root.Load(true, opts) 857 if err != nil { 858 panic(err) 859 } 860 fmt.Printf("%#v", root) 861 862 reuse := pathNodePool.Get().(*PathNode) 863 root.Node = NewNode(thrift.STRUCT, data) 864 err = root.Load(true, opts) 865 if err != nil { 866 panic(err) 867 } 868 fmt.Printf("%#v", root) 869 reuse.ResetValue() 870 pathNodePool.Put(reuse) 871 872 } 873 ``` 874 875 </p> 876 </details> 877 878 ### func \(PathNode\) Marshal 879 880 ```go 881 func (self PathNode) Marshal(opt *Options) (out []byte, err error) 882 ``` 883 884 Marshal marshals self to thrift bytes 885 886 ### func \(PathNode\) MarshalIntoBuffer 887 888 ```go 889 func (self PathNode) MarshalIntoBuffer(out *[]byte, opt *Options) error 890 ``` 891 892 MarshalIntoBuffer marshals self to thrift bytes into a buffer 893 894 ### func \(\*PathNode\) ResetAll 895 896 ```go 897 func (self *PathNode) ResetAll() 898 ``` 899 900 ResetAll resets self and its children, including path and node both 901 902 ### func \(\*PathNode\) ResetValue 903 904 ```go 905 func (self *PathNode) ResetValue() 906 ``` 907 908 ResetValue resets self's node and its children's node 909 910 ### func \(\*PathNode\) SetByInt 911 912 ```go 913 func (self *PathNode) SetByInt(key int, val Node, opts *Options) (bool, error) 914 ``` 915 916 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. 917 918 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. 919 920 ### func \(\*PathNode\) SetByStr 921 922 ```go 923 func (self *PathNode) SetByStr(key string, val Node, opts *Options) (bool, error) 924 ``` 925 926 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. 927 928 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. 929 930 ### func \(\*PathNode\) SetField 931 932 ```go 933 func (self *PathNode) SetField(id thrift.FieldID, val Node, opts *Options) (bool, error) 934 ``` 935 936 SetField set the child node by field id. Only support STRUCT. If the key already exists, it will be overwritten and return true. 937 938 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. 939 940 ## type PathType 941 942 PathType is the type of path 943 944 ```go 945 type PathType uint8 946 ``` 947 948 ```go 949 const ( 950 // PathFieldId represents a field id of STRUCT type 951 PathFieldId PathType = 1 + iota 952 953 // PathFieldName represents a field name of STRUCT type 954 // NOTICE: it is only supported by Value 955 PathFieldName 956 957 // PathIndex represents a index of LIST\SET type 958 PathIndex 959 960 // Path represents a string key of MAP type 961 PathStrKey 962 963 // Path represents a int key of MAP type 964 PathIntKey 965 966 // Path represents a raw-bytes key of MAP type 967 // It is usually used for neither-string-nor-integer type key 968 PathBinKey 969 ) 970 ``` 971 972 ## type Value 973 974 Value is a generic API wrapper for operations on thrift data. Node is the underlying raw bytes. Desc is the corresponding type descriptor. 975 976 ```go 977 type Value struct { 978 Node 979 Desc *thrift.TypeDescriptor 980 } 981 ``` 982 983 <details><summary>Example (3et Many)</summary> 984 <p> 985 986 ```go 987 { 988 989 desc := getExampleDesc() 990 data := getExampleData() 991 d1 := desc.Struct().FieldByKey("Msg").Type() 992 d2 := desc.Struct().FieldByKey("Subfix").Type() 993 v := NewValue(desc, data) 994 995 p := thrift.NewBinaryProtocol([]byte{}) 996 e1 := "test1" 997 p.WriteString(e1) 998 v1 := NewValue(d1, p.RawBuf()) 999 p = thrift.NewBinaryProtocol([]byte{}) 1000 e2 := float64(-255.0001) 1001 p.WriteDouble(e2) 1002 v2 := NewValue(d2, p.RawBuf()) 1003 v3 := v.GetByPath(NewPathFieldName("Base")) 1004 1005 ps := []PathNode{ 1006 { 1007 Path: NewPathFieldId(1), 1008 Node: v1.Node, 1009 }, 1010 { 1011 Path: NewPathFieldId(32767), 1012 Node: v2.Node, 1013 }, 1014 { 1015 Path: NewPathFieldId(255), 1016 Node: v3.Node, 1017 }, 1018 } 1019 1020 err := v.SetMany(ps, opts) 1021 if err != nil { 1022 panic(err) 1023 } 1024 any, err := v.Interface(opts) 1025 if err != nil { 1026 panic(err) 1027 } 1028 fmt.Printf("%#v", any) 1029 1030 ps2 := []PathNode{ 1031 { 1032 Path: NewPathFieldId(1), 1033 }, 1034 { 1035 Path: NewPathFieldId(32767), 1036 }, 1037 { 1038 Path: NewPathFieldId(255), 1039 }, 1040 } 1041 if err := v.GetMany(ps2, opts); err != nil { 1042 panic(err) 1043 } 1044 any0, err := ps2[2].Node.Interface(opts) 1045 if err != nil { 1046 panic(err) 1047 } 1048 fmt.Printf("%#v", any0) 1049 } 1050 ``` 1051 1052 </p> 1053 </details> 1054 1055 ### func NewValue 1056 1057 ```go 1058 func NewValue(desc *thrift.TypeDescriptor, src []byte) Value 1059 ``` 1060 1061 NewValue creates a new Value from a raw byte slice. 1062 1063 ### func \(Value\) Field 1064 1065 ```go 1066 func (self Value) Field(id thrift.FieldID) (v Value) 1067 ``` 1068 1069 Field returns a sub node at the given field id from a STRUCT value. 1070 1071 ### func \(Value\) FieldByName 1072 1073 ```go 1074 func (self Value) FieldByName(name string) (v Value) 1075 ``` 1076 1077 FieldByName returns a sub node at the given field name from a STRUCT value. 1078 1079 ### func \(Value\) Fork 1080 1081 ```go 1082 func (self Value) Fork() Value 1083 ``` 1084 1085 NewValueFromNode copy both Node and TypeDescriptor from another Value. 1086 1087 ### func \(Value\) GetByInt 1088 1089 ```go 1090 func (self Value) GetByInt(key int) (v Value) 1091 ``` 1092 1093 GetByInt returns a sub node at the given int key from a MAP value. 1094 1095 ### func \(Value\) GetByPath 1096 1097 ```go 1098 func (self Value) GetByPath(pathes ...Path) Value 1099 ``` 1100 1101 GetByPath searches longitudinally and return a sub value at the given path from the value. 1102 1103 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. 1104 1105 ### func \(Value\) GetByStr 1106 1107 ```go 1108 func (self Value) GetByStr(key string) (v Value) 1109 ``` 1110 1111 GetByStr returns a sub node at the given string key from a MAP value. 1112 1113 ### func \(Value\) Index 1114 1115 ```go 1116 func (self Value) Index(i int) (v Value) 1117 ``` 1118 1119 Index returns a sub node at the given index from a LIST value. 1120 1121 ### func \(Value\) MarshalTo 1122 1123 ```go 1124 func (self Value) MarshalTo(to *thrift.TypeDescriptor, opts *Options) ([]byte, error) 1125 ``` 1126 1127 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. 1128 1129 <details><summary>Example</summary> 1130 <p> 1131 1132 ```go 1133 { 1134 1135 desc := getExampleDesc() 1136 data := getExampleData() 1137 v := NewValue(desc, data) 1138 1139 full, err := NewNode(thrift.STRUCT, data).Interface(opts) 1140 if err != nil { 1141 panic(err) 1142 } 1143 fmt.Printf("%#v", full) 1144 1145 pdesc := getExamplePartialDesc() 1146 1147 out, err := v.MarshalTo(pdesc, opts) 1148 if err != nil { 1149 panic(err) 1150 } 1151 1152 partial, err := NewNode(thrift.STRUCT, out).Interface(opts) 1153 if err != nil { 1154 panic(err) 1155 } 1156 fmt.Printf("%#v", partial) 1157 } 1158 ``` 1159 1160 </p> 1161 </details> 1162 1163 ### func \(\*Value\) SetByPath 1164 1165 ```go 1166 func (self *Value) SetByPath(sub Value, path ...Path) (exist bool, err error) 1167 ``` 1168 1169 SetByPath searches longitudinally and sets a sub value at the given path from the value. exist tells whether the node is already exists. 1170 1171 <details><summary>Example</summary> 1172 <p> 1173 1174 ```go 1175 { 1176 1177 desc := getExampleDesc() 1178 data := getExampleData() 1179 v := NewValue(desc, data) 1180 1181 d := desc.Struct().FieldByKey("Base").Type().Struct().FieldByKey("Extra").Type().Elem() 1182 p := thrift.NewBinaryProtocol([]byte{}) 1183 exp := "中文" 1184 p.WriteString(exp) 1185 buf := p.RawBuf() 1186 vv := NewValue(d, buf) 1187 1188 ps := []Path{NewPathFieldName("Base"), NewPathFieldName("Extra"), NewPathStrKey("b")} 1189 exist, err2 := v.SetByPath(vv, ps...) 1190 if err2 != nil { 1191 panic(err2) 1192 } 1193 println(exist) 1194 1195 s2 := v.GetByPath(ps...) 1196 if s2.Error() != "" { 1197 panic(s2.Error()) 1198 } 1199 f2, _ := s2.String() 1200 println(f2) 1201 } 1202 ``` 1203 1204 </p> 1205 </details> 1206 1207 ### func \(\*Value\) UnsetByPath 1208 1209 ```go 1210 func (self *Value) UnsetByPath(path ...Path) error 1211 ``` 1212 1213 UnsetByPath searches longitudinally and unsets a sub value at the given path from the value. 1214 1215 1216 1217 Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)