@@ -333,6 +333,12 @@ func descriptorToPathNode(recurse int, desc *thrift.TypeDescriptor, root *PathNo
333
333
ns = ns [:0 ]
334
334
}
335
335
for _ , f := range fs {
336
+ if f .Required () == thrift .OptionalRequireness && ! opts .DescriptorToPathNodeWriteOptional {
337
+ continue
338
+ }
339
+ if f .Required () == thrift .DefaultRequireness && ! opts .DescriptorToPathNodeWriteDefualt {
340
+ continue
341
+ }
336
342
var p PathNode
337
343
// if opts.FieldByName {
338
344
// p.Path = NewPathFieldName(f.Name())
@@ -365,7 +371,7 @@ func descriptorToPathNode(recurse int, desc *thrift.TypeDescriptor, root *PathNo
365
371
if ty := desc .Key ().Type (); ty .IsInt () {
366
372
next [i ].Path = NewPathIntKey (i ) // NOTICE: use index as int key here
367
373
} else if ty == thrift .STRING {
368
- next [i ].Path = NewPathStrKey (strconv .Itoa (i ))// NOTICE: use index as string key here
374
+ next [i ].Path = NewPathStrKey (strconv .Itoa (i )) // NOTICE: use index as string key here
369
375
} else {
370
376
buf := thrift .NewBinaryProtocol ([]byte {})
371
377
_ = buf .WriteEmpty (desc .Key ()) // NOTICE: use emtpy value as binary key here
@@ -759,7 +765,7 @@ func (self PathNode) marshal(p *thrift.BinaryProtocol, opts *Options) error {
759
765
func guardPathNodeSlice (con * []PathNode , l int ) {
760
766
c := cap (* con )
761
767
if l >= c {
762
- tmp := make ([]PathNode , len (* con ), l + DefaultNodeSliceCap )
768
+ tmp := make ([]PathNode , len (* con ), l + DefaultNodeSliceCap )
763
769
copy (tmp , * con )
764
770
* con = tmp
765
771
}
@@ -858,7 +864,7 @@ func (self *PathNode) should2(op string, t thrift.Type, t2 thrift.Type) *PathNod
858
864
if self == nil {
859
865
return errPathNode (meta .ErrNotFound , op , nil )
860
866
}
861
- if self .Node .t != t && self .Node .t != t2 {
867
+ if self .Node .t != t && self .Node .t != t2 {
862
868
return errPathNode (meta .ErrDismatchType , op , nil )
863
869
}
864
870
return nil
@@ -914,7 +920,7 @@ func (self *PathNode) GetByStr(key string, opts *Options) *PathNode {
914
920
// fast path: use hash to find the key.
915
921
if opts .StoreChildrenByHash {
916
922
n , _ := self .Node .len ()
917
- N := n * 2
923
+ N := n * 2
918
924
// TODO: cap may change after Set. Use better way to store hash size
919
925
if cap (self .Next ) >= N {
920
926
if s := getStrHash (& self .Next , key , N ); s != nil {
@@ -934,7 +940,7 @@ func (self *PathNode) GetByStr(key string, opts *Options) *PathNode {
934
940
935
941
// SetByStr set the child node by string. Only support MAP with string-type key.
936
942
// If the key already exists, it will be overwritten and return true.
937
- //
943
+ //
938
944
// If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key.
939
945
// However, if the map hash size has changed, it may fallback to O(n) search.
940
946
func (self * PathNode ) SetByStr (key string , val Node , opts * Options ) (bool , error ) {
@@ -947,7 +953,7 @@ func (self *PathNode) SetByStr(key string, val Node, opts *Options) (bool, error
947
953
// fast path: use hash to find the key.
948
954
if opts .StoreChildrenByHash {
949
955
n , _ := self .Node .len ()
950
- N := n * 2
956
+ N := n * 2
951
957
// TODO: cap may change after Set. Use better way to store hash size
952
958
if cap (self .Next ) >= N {
953
959
if s := getStrHash (& self .Next , key , N ); s != nil {
@@ -972,7 +978,7 @@ func (self *PathNode) SetByStr(key string, val Node, opts *Options) (bool, error
972
978
}
973
979
974
980
// GetByInt get the child node by integer. Only support MAP with integer-type key.
975
- //
981
+ //
976
982
// If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key.
977
983
// However, if the map size has changed, it may fallback to O(n) search.
978
984
func (self * PathNode ) GetByInt (key int , opts * Options ) * PathNode {
@@ -986,7 +992,7 @@ func (self *PathNode) GetByInt(key int, opts *Options) *PathNode {
986
992
if opts .StoreChildrenByHash {
987
993
// TODO: size may change after Set. Use better way to store hash size
988
994
n , _ := self .Node .len ()
989
- N := n * 2
995
+ N := n * 2
990
996
if cap (self .Next ) >= N {
991
997
if s := getIntHash (& self .Next , uint64 (key ), N ); s != nil {
992
998
return s
@@ -1018,7 +1024,7 @@ func (self *PathNode) SetByInt(key int, val Node, opts *Options) (bool, error) {
1018
1024
// fast path: use hash to find the key.
1019
1025
if opts .StoreChildrenByHash {
1020
1026
n , _ := self .Node .len ()
1021
- N := n * 2
1027
+ N := n * 2
1022
1028
if cap (self .Next ) >= N {
1023
1029
if s := getIntHash (& self .Next , uint64 (key ), N ); s != nil {
1024
1030
s .Node = val
@@ -1062,8 +1068,8 @@ func (self *PathNode) Field(id thrift.FieldID, opts *Options) *PathNode {
1062
1068
if v .Path .t == PathFieldId && v .Path .id () == id {
1063
1069
return v
1064
1070
}
1065
- }
1066
- for i := 0 ; i < len (self .Next ) && i < StoreChildrenByIdShreshold ; i ++ {
1071
+ }
1072
+ for i := 0 ; i < len (self .Next ) && i < StoreChildrenByIdShreshold ; i ++ {
1067
1073
v := & self .Next [i ]
1068
1074
if v .Path .t == PathFieldId && v .Path .id () == id {
1069
1075
return v
@@ -1096,7 +1102,7 @@ func (self *PathNode) SetField(id thrift.FieldID, val Node, opts *Options) (bool
1096
1102
return true , nil
1097
1103
}
1098
1104
}
1099
- for i := 0 ; i < len (self .Next ) && i < StoreChildrenByIdShreshold ; i ++ {
1105
+ for i := 0 ; i < len (self .Next ) && i < StoreChildrenByIdShreshold ; i ++ {
1100
1106
v := & self .Next [i ]
1101
1107
if v .Path .t == PathFieldId && v .Path .id () == id {
1102
1108
v .Node = val
@@ -1174,7 +1180,7 @@ func (self *PathNode) scanChildren(p *thrift.BinaryProtocol, recurse bool, opts
1174
1180
// fast path: use hash to store the key.
1175
1181
if opts .StoreChildrenByHash && size > StoreChildrenByIntHashShreshold {
1176
1182
// NOTE: we use original count*2 as the capacity of the hash table.
1177
- N = size * 2
1183
+ N = size * 2
1178
1184
guardPathNodeSlice (& con , N - 1 )
1179
1185
conAddr = * (* unsafe .Pointer )(unsafe .Pointer (& con ))
1180
1186
c = N
@@ -1198,7 +1204,7 @@ func (self *PathNode) scanChildren(p *thrift.BinaryProtocol, recurse bool, opts
1198
1204
// fast path: use hash to store the key.
1199
1205
if opts .StoreChildrenByHash && size > StoreChildrenByIntHashShreshold {
1200
1206
// NOTE: we use original count*2 as the capacity of the hash table.
1201
- N = size * 2
1207
+ N = size * 2
1202
1208
guardPathNodeSlice (& con , N - 1 )
1203
1209
conAddr = * (* unsafe .Pointer )(unsafe .Pointer (& con ))
1204
1210
c = N
@@ -1238,4 +1244,4 @@ func (self *PathNode) scanChildren(p *thrift.BinaryProtocol, recurse bool, opts
1238
1244
1239
1245
self .Next = con
1240
1246
return nil
1241
- }
1247
+ }
0 commit comments