github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/generic/iter_test.go (about) 1 /** 2 * Copyright 2023 CloudWeGo Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package generic 18 19 import ( 20 "testing" 21 22 "github.com/cloudwego/dynamicgo/testdata/sample" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestForeach(t *testing.T) { 27 var iterOpts = &Options{ 28 IterateStructByName: true, 29 } 30 31 desc := getExampleDesc() 32 data := getExampleData() 33 root := NewValue(desc, data) 34 35 var exp = PathNode{ 36 Path: NewPathFieldName("root"), 37 Node: root.Node, 38 Next: []PathNode{}, 39 } 40 require.Nil(t, root.Children(&exp.Next, true, iterOpts)) 41 42 t.Run("Node", func(t *testing.T) { 43 var handler func(path Path, node Node) bool 44 var tree = PathNode{ 45 Path: NewPathFieldName("root"), 46 Node: root.Node, 47 } 48 var cur = &tree 49 handler = func(path Path, node Node) bool { 50 cur.Next = append(cur.Next, PathNode{ 51 Path: path, 52 Node: node, 53 }) 54 if node.Type().IsComplex() { 55 old := cur 56 cur = &cur.Next[len(cur.Next)-1] 57 require.Nil(t, node.Foreach(handler, iterOpts)) 58 cur = old 59 } 60 return true 61 } 62 require.NoError(t, root.Node.Foreach(handler, iterOpts)) 63 require.Equal(t, exp, tree) 64 65 cout := 0 66 handler3 := func(path Path, node Node) bool { 67 cout += 1 68 return false 69 } 70 vv := root.GetByPath(NewPathFieldName("InnerBase")) 71 require.Nil(t, vv.Check()) 72 require.NoError(t, vv.Node.Foreach(handler3, iterOpts)) 73 require.Equal(t, 1, cout) 74 75 cout = 0 76 vv = root.GetByPath(PathExampleListInt32...) 77 require.Nil(t, vv.Check()) 78 require.NoError(t, vv.Node.Foreach(handler3, iterOpts)) 79 require.Equal(t, 1, cout) 80 81 cout = 0 82 vv = root.GetByPath(PathExampleMapInt32String...) 83 require.Nil(t, vv.Check()) 84 require.NoError(t, vv.Node.Foreach(handler3, iterOpts)) 85 require.Equal(t, 1, cout) 86 87 cout = 0 88 vv = root.GetByPath(PathExampleMapStringString...) 89 require.Nil(t, vv.Check()) 90 require.NoError(t, vv.Node.Foreach(handler3, iterOpts)) 91 require.Equal(t, 1, cout) 92 }) 93 94 t.Run("Value", func(t *testing.T) { 95 handler := func(path Path, val Value) bool { 96 vv, err := val.Interface(iterOpts) 97 require.NoError(t, err) 98 t.Logf("Path: %v, Value: %v", path, vv) 99 return true 100 } 101 require.NoError(t, root.Foreach(handler, iterOpts)) 102 103 handler2 := func(path Path, val Value) bool { 104 require.True(t, path.Type() == PathFieldName) 105 t.Logf("handler2: %v", path) 106 return true 107 } 108 vv := root.GetByPath(NewPathFieldName("InnerBase")) 109 require.Nil(t, vv.Check()) 110 require.NoError(t, vv.Foreach(handler2, iterOpts)) 111 112 cout := 0 113 handler3 := func(path Path, val Value) bool { 114 cout += 1 115 return false 116 } 117 cout = 0 118 vv = root.GetByPath(PathExampleListInt32...) 119 require.Nil(t, vv.Check()) 120 require.NoError(t, vv.Foreach(handler3, iterOpts)) 121 require.Equal(t, 1, cout) 122 123 cout = 0 124 vv = root.GetByPath(PathExampleMapInt32String...) 125 require.Nil(t, vv.Check()) 126 require.NoError(t, vv.Foreach(handler3, iterOpts)) 127 require.Equal(t, 1, cout) 128 129 cout = 0 130 vv = root.GetByPath(PathExampleMapStringString...) 131 require.Nil(t, vv.Check()) 132 require.NoError(t, vv.Foreach(handler3, iterOpts)) 133 require.Equal(t, 1, cout) 134 }) 135 136 } 137 138 func TestForeachKV(t *testing.T) { 139 desc := getExampleDesc() 140 data := getExampleData() 141 root := NewValue(desc, data) 142 opts := &Options{} 143 144 t.Run("Node", func(t *testing.T) { 145 v := root.GetByPath(PathExampleListInt32...) 146 require.Nil(t, v.Check()) 147 err := v.Node.ForeachKV(func(key, val Node) bool { return true }, opts) 148 require.Error(t, err) 149 v = root.GetByPath(PathExampleMapInt32String...) 150 require.Nil(t, v.Check()) 151 err = v.Node.ForeachKV(func(key, val Node) bool { 152 k, err := key.Int() 153 require.NoError(t, err) 154 v, err := val.String() 155 require.NoError(t, err) 156 exp := sample.Example2Obj.InnerBase.MapInt32String[int32(k)] 157 require.Equal(t, exp, v) 158 return true 159 }, opts) 160 require.NoError(t, err) 161 }) 162 163 t.Run("Value", func(t *testing.T) { 164 v := root.GetByPath(PathExampleListInt32...) 165 require.Nil(t, v.Check()) 166 err := v.Node.ForeachKV(func(key, val Node) bool { return true }, opts) 167 require.Error(t, err) 168 v = root.GetByPath(PathExampleMapInt32String...) 169 require.Nil(t, v.Check()) 170 err = v.ForeachKV(func(key, val Value) bool { 171 k, err := key.Int() 172 require.NoError(t, err) 173 v, err := val.String() 174 require.NoError(t, err) 175 exp := sample.Example2Obj.InnerBase.MapInt32String[int32(k)] 176 require.Equal(t, exp, v) 177 return true 178 }, opts) 179 require.NoError(t, err) 180 }) 181 182 }