Skip to content

Commit 497e7b3

Browse files
authored
feat:(thrift/generic) add option DescriptorToPathNodeWriteOptional (#39)
feat:(thrift/generic) add option `DescriptorToPathNodeWriteOptional` for API `DescriptorToPathNode()`
1 parent bda904c commit 497e7b3

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

thrift/generic/option.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,18 @@ type Options struct {
8181

8282
// DescriptorToPathNodeMaxDepth indicates max recurse limits (>0) for API `DescriptorToPathNode`
8383
DescriptorToPathNodeMaxDepth int
84+
85+
// DescriptorToPathNodeWriteOptional indicates writing empty value for optional fields for API `DescriptorToPathNode`
86+
DescriptorToPathNodeWriteOptional bool
87+
88+
// DescriptorToPathNodeWriteDefualt indicates writing empty value for default fields for API `DescriptorToPathNode`
89+
DescriptorToPathNodeWriteDefualt bool
8490
}
8591

8692
var (
8793
// StoreChildrenByIdShreshold is the maximum id to store children node by id.
88-
StoreChildrenByIdShreshold = 256
94+
StoreChildrenByIdShreshold = 256
8995

9096
// StoreChildrenByIdShreshold is the minimum id to store children node by hash.
9197
StoreChildrenByIntHashShreshold = DefaultNodeSliceCap
9298
)
93-

thrift/generic/path.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,12 @@ func descriptorToPathNode(recurse int, desc *thrift.TypeDescriptor, root *PathNo
333333
ns = ns[:0]
334334
}
335335
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+
}
336342
var p PathNode
337343
// if opts.FieldByName {
338344
// p.Path = NewPathFieldName(f.Name())
@@ -365,7 +371,7 @@ func descriptorToPathNode(recurse int, desc *thrift.TypeDescriptor, root *PathNo
365371
if ty := desc.Key().Type(); ty.IsInt() {
366372
next[i].Path = NewPathIntKey(i) // NOTICE: use index as int key here
367373
} 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
369375
} else {
370376
buf := thrift.NewBinaryProtocol([]byte{})
371377
_ = 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 {
759765
func guardPathNodeSlice(con *[]PathNode, l int) {
760766
c := cap(*con)
761767
if l >= c {
762-
tmp := make([]PathNode, len(*con), l + DefaultNodeSliceCap)
768+
tmp := make([]PathNode, len(*con), l+DefaultNodeSliceCap)
763769
copy(tmp, *con)
764770
*con = tmp
765771
}
@@ -858,7 +864,7 @@ func (self *PathNode) should2(op string, t thrift.Type, t2 thrift.Type) *PathNod
858864
if self == nil {
859865
return errPathNode(meta.ErrNotFound, op, nil)
860866
}
861-
if self.Node.t != t && self.Node.t != t2{
867+
if self.Node.t != t && self.Node.t != t2 {
862868
return errPathNode(meta.ErrDismatchType, op, nil)
863869
}
864870
return nil
@@ -914,7 +920,7 @@ func (self *PathNode) GetByStr(key string, opts *Options) *PathNode {
914920
// fast path: use hash to find the key.
915921
if opts.StoreChildrenByHash {
916922
n, _ := self.Node.len()
917-
N := n*2
923+
N := n * 2
918924
// TODO: cap may change after Set. Use better way to store hash size
919925
if cap(self.Next) >= N {
920926
if s := getStrHash(&self.Next, key, N); s != nil {
@@ -934,7 +940,7 @@ func (self *PathNode) GetByStr(key string, opts *Options) *PathNode {
934940

935941
// SetByStr set the child node by string. Only support MAP with string-type key.
936942
// If the key already exists, it will be overwritten and return true.
937-
//
943+
//
938944
// If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key.
939945
// However, if the map hash size has changed, it may fallback to O(n) search.
940946
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
947953
// fast path: use hash to find the key.
948954
if opts.StoreChildrenByHash {
949955
n, _ := self.Node.len()
950-
N := n*2
956+
N := n * 2
951957
// TODO: cap may change after Set. Use better way to store hash size
952958
if cap(self.Next) >= N {
953959
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
972978
}
973979

974980
// GetByInt get the child node by integer. Only support MAP with integer-type key.
975-
//
981+
//
976982
// If opts.StoreChildrenByHash is true, it will try to use hash (O(1)) to search the key.
977983
// However, if the map size has changed, it may fallback to O(n) search.
978984
func (self *PathNode) GetByInt(key int, opts *Options) *PathNode {
@@ -986,7 +992,7 @@ func (self *PathNode) GetByInt(key int, opts *Options) *PathNode {
986992
if opts.StoreChildrenByHash {
987993
// TODO: size may change after Set. Use better way to store hash size
988994
n, _ := self.Node.len()
989-
N := n*2
995+
N := n * 2
990996
if cap(self.Next) >= N {
991997
if s := getIntHash(&self.Next, uint64(key), N); s != nil {
992998
return s
@@ -1018,7 +1024,7 @@ func (self *PathNode) SetByInt(key int, val Node, opts *Options) (bool, error) {
10181024
// fast path: use hash to find the key.
10191025
if opts.StoreChildrenByHash {
10201026
n, _ := self.Node.len()
1021-
N := n*2
1027+
N := n * 2
10221028
if cap(self.Next) >= N {
10231029
if s := getIntHash(&self.Next, uint64(key), N); s != nil {
10241030
s.Node = val
@@ -1062,8 +1068,8 @@ func (self *PathNode) Field(id thrift.FieldID, opts *Options) *PathNode {
10621068
if v.Path.t == PathFieldId && v.Path.id() == id {
10631069
return v
10641070
}
1065-
}
1066-
for i := 0; i < len(self.Next) && i<StoreChildrenByIdShreshold; i++ {
1071+
}
1072+
for i := 0; i < len(self.Next) && i < StoreChildrenByIdShreshold; i++ {
10671073
v := &self.Next[i]
10681074
if v.Path.t == PathFieldId && v.Path.id() == id {
10691075
return v
@@ -1096,7 +1102,7 @@ func (self *PathNode) SetField(id thrift.FieldID, val Node, opts *Options) (bool
10961102
return true, nil
10971103
}
10981104
}
1099-
for i := 0; i < len(self.Next) && i<StoreChildrenByIdShreshold; i++ {
1105+
for i := 0; i < len(self.Next) && i < StoreChildrenByIdShreshold; i++ {
11001106
v := &self.Next[i]
11011107
if v.Path.t == PathFieldId && v.Path.id() == id {
11021108
v.Node = val
@@ -1174,7 +1180,7 @@ func (self *PathNode) scanChildren(p *thrift.BinaryProtocol, recurse bool, opts
11741180
// fast path: use hash to store the key.
11751181
if opts.StoreChildrenByHash && size > StoreChildrenByIntHashShreshold {
11761182
// NOTE: we use original count*2 as the capacity of the hash table.
1177-
N = size*2
1183+
N = size * 2
11781184
guardPathNodeSlice(&con, N-1)
11791185
conAddr = *(*unsafe.Pointer)(unsafe.Pointer(&con))
11801186
c = N
@@ -1198,7 +1204,7 @@ func (self *PathNode) scanChildren(p *thrift.BinaryProtocol, recurse bool, opts
11981204
// fast path: use hash to store the key.
11991205
if opts.StoreChildrenByHash && size > StoreChildrenByIntHashShreshold {
12001206
// NOTE: we use original count*2 as the capacity of the hash table.
1201-
N = size*2
1207+
N = size * 2
12021208
guardPathNodeSlice(&con, N-1)
12031209
conAddr = *(*unsafe.Pointer)(unsafe.Pointer(&con))
12041210
c = N
@@ -1238,4 +1244,4 @@ func (self *PathNode) scanChildren(p *thrift.BinaryProtocol, recurse bool, opts
12381244

12391245
self.Next = con
12401246
return nil
1241-
}
1247+
}

thrift/generic/path_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ func checkHelper2(t *testing.T, exp []PathNode, act []PathNode, checkNode bool)
727727
// }
728728
// }
729729

730-
731730
func DeepEqual(exp interface{}, act interface{}) bool {
732731
switch ev := exp.(type) {
733732
case map[int]interface{}:
@@ -884,7 +883,8 @@ func TestUnknownFields(t *testing.T) {
884883
t.Run("Assgin(true, )", func(t *testing.T) {
885884
// t.Run("allow", func(t *testing.T) {
886885
opts := Options{
887-
// DisallowUnknow: false,
886+
DescriptorToPathNodeWriteDefualt: true,
887+
DescriptorToPathNodeWriteOptional: true,
888888
}
889889
path := PathNode{
890890
Node: v.Node,
@@ -897,13 +897,11 @@ func TestUnknownFields(t *testing.T) {
897897
require.NoError(t, err)
898898
act := PathNodeToInterface(path, &opts, true)
899899
exp := toInterface2(sample.Example2Obj, false, b2s)
900+
// require.Equal(t, exp, act)
900901
if !DeepEqual(exp, act) {
902+
spew.Dump(exp, act)
901903
t.Fatal()
902904
}
903-
spew.Dump(exp, "\nnext:\n", act)
904-
// if !DeepEqual(act, exp) {
905-
// t.Fatal()
906-
// }
907905

908906
// })
909907
// t.Run("disallow", func(t *testing.T) {
@@ -1003,24 +1001,24 @@ func TestDescriptorToPathNode(t *testing.T) {
10031001
args args
10041002
wantErr bool
10051003
}{
1006-
{name:"defualt", args: args{
1004+
{name: "defualt", args: args{
10071005
desc: getExampleDesc(),
10081006
root: new(PathNode),
10091007
opts: &Options{},
10101008
}, wantErr: false},
1011-
{name:"array size 1", args: args{
1009+
{name: "array size 1", args: args{
10121010
desc: getExampleDesc(),
10131011
root: new(PathNode),
10141012
opts: &Options{
10151013
DescriptorToPathNodeArraySize: 1,
1016-
DescriptorToPathNodeMaxDepth: 256,
1014+
DescriptorToPathNodeMaxDepth: 256,
10171015
},
10181016
}, wantErr: false},
1019-
{name:"map size 1", args: args{
1017+
{name: "map size 1", args: args{
10201018
desc: getExampleDesc(),
10211019
root: new(PathNode),
10221020
opts: &Options{
1023-
DescriptorToPathNodeMapSize: 1,
1021+
DescriptorToPathNodeMapSize: 1,
10241022
DescriptorToPathNodeMaxDepth: 256,
10251023
},
10261024
}, wantErr: false},
@@ -1033,6 +1031,6 @@ func TestDescriptorToPathNode(t *testing.T) {
10331031
println(tt.name)
10341032
spew.Dump(PathNodeToInterface(*tt.args.root, tt.args.opts, false))
10351033
})
1036-
1034+
10371035
}
10381036
}

0 commit comments

Comments
 (0)