github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/proto/generic/example_test.go (about) 1 package generic 2 3 import ( 4 "fmt" 5 "math" 6 "testing" 7 8 "github.com/cloudwego/dynamicgo/proto" 9 "github.com/cloudwego/dynamicgo/proto/binary" 10 "github.com/stretchr/testify/require" 11 ) 12 13 var opts = &Options{ 14 UseNativeSkip: false, 15 MapStructById: true, 16 ClearDirtyValues: true, 17 CastStringAsBinary: false, 18 } 19 20 func ExampleValue_GetByPath(t *testing.T) { 21 desc := getExample2Desc() 22 data := getExample2Data() 23 v := NewRootValue(desc, data) 24 25 ps := []Path{NewPathFieldName("InnerBase2"), NewPathFieldName("Base"), NewPathFieldName("Extra"), NewPathStrKey("1b")} 26 s := v.GetByPath(ps...) 27 if s.Error() != "" { 28 panic(s.Error()) 29 } 30 str, _ := s.String() 31 fmt.Println(str) // "aaa" 32 } 33 34 func ExampleValue_GetMany() { 35 desc := getExample2Desc() 36 data := getExample2Data() 37 v := NewRootValue(desc, data) 38 39 ps := []PathNode{ 40 {Path: NewPathFieldId(1)}, 41 {Path: NewPathFieldId(3)}, 42 {Path: NewPathFieldId(32767)}, 43 } 44 45 err := v.GetMany(ps, opts) 46 if err != nil { 47 panic(err) 48 } 49 } 50 51 func ExampleValue_SetByPath() { 52 desc := getExample2Desc() 53 data := getExample2Data() 54 v := NewRootValue(desc, data) 55 56 p := binary.NewBinaryProtol([]byte{}) 57 exp := "中文" 58 p.WriteString(exp) 59 buf := p.RawBuf() 60 vv := NewNode(proto.STRING, buf) 61 62 ps := []Path{NewPathFieldName("InnerBase2"), NewPathFieldName("Base"), NewPathFieldName("Extra"), NewPathStrKey("b")} 63 exist, err2 := v.SetByPath(vv, ps...) 64 if err2 != nil { 65 panic(err2) 66 } 67 fmt.Println(exist) // false 68 69 // check inserted value 70 s2 := v.GetByPath(ps...) 71 if s2.Error() != "" { 72 panic(s2.Error()) 73 } 74 f2, _ := s2.String() 75 fmt.Println(f2) // 中文 76 } 77 78 func ExampleValue_SetMany(t *testing.T) { 79 desc := getExample2Desc() 80 data := getExample2Data() 81 root := NewRootValue(desc, data) 82 83 84 exp1 := int32(-1024) 85 n1 := NewNodeInt32(exp1) 86 address := []int{} 87 pathes := []Path{} 88 err := root.SetMany([]PathNode{ 89 { 90 Path: NewPathFieldId(2), 91 Node: n1, 92 }, 93 }, opts, &root, address, pathes...) 94 95 require.Nil(t, err) 96 v1 := root.GetByPath(NewPathFieldName("A")) 97 act1, err := v1.Int() 98 fmt.Println(act1) // -1024 99 100 101 expmin := int32(math.MinInt32) 102 expmax := int32(math.MaxInt32) 103 nmin := NewNodeInt32(expmin) 104 nmax := NewNodeInt32(expmax) 105 PathExampleListInt32 = []Path{NewPathFieldName("InnerBase2"), NewPathFieldId(proto.FieldNumber(8))} 106 107 vv, listInt2root := root.GetByPathWithAddress(PathExampleListInt32...) 108 l2, err := vv.Len() 109 fmt.Println(l2) // 6 110 if err != nil { 111 panic(err) 112 } 113 114 115 path2root := []Path{NewPathFieldName("InnerBase2"), NewPathFieldId(proto.FieldNumber(8)), NewPathIndex(1024)} 116 address2root := append(listInt2root, 0) 117 118 err = vv.SetMany([]PathNode{ 119 { 120 Path: NewPathIndex(1024), 121 Node: nmin, 122 }, 123 { 124 Path: NewPathIndex(1024), 125 Node: nmax, 126 }, 127 }, opts, &root, address2root, path2root...) 128 129 if err != nil { 130 panic(err) 131 } 132 133 v21 := vv.GetByPath(NewPathIndex(6)) 134 act21, err := v21.Int() 135 if err != nil { 136 panic(err) 137 } 138 139 fmt.Println(act21) // -2147483648 140 141 v22 := vv.GetByPath(NewPathIndex(7)) 142 act22, err := v22.Int() 143 if err != nil { 144 panic(err) 145 } 146 147 fmt.Println(act22) // 2147483647 148 149 ll2, err := vv.Len() 150 if err != nil { 151 panic(err) 152 } 153 154 fmt.Println(ll2) // 8 155 } 156 157 func ExampleValue_MarshalTo(t *testing.T) { 158 desc := getExample2Desc() 159 data := getExample2Data() 160 v := NewRootValue(desc, data) 161 162 partial := getExamplePartialDesc() 163 164 out, err := v.MarshalTo(partial, opts) 165 if err != nil { 166 panic(err) 167 } 168 169 partMsg, err := NewRootValue(partial, out).Interface(opts) 170 if err != nil { 171 panic(err) 172 } 173 fmt.Printf("%#v", partMsg) 174 } 175 176 177 func ExamplePathNode_Load(* testing.T) { 178 desc := getExample2Desc() 179 data := getExample2Data() 180 181 root := PathNode{ 182 Node: NewNode(proto.MESSAGE, data), 183 } 184 185 // load first level children 186 err := root.Load(false, opts, desc) 187 if err != nil { 188 panic(err) 189 } 190 fmt.Printf("%#v", root) 191 192 // load all level children 193 err = root.Load(true, opts, desc) 194 if err != nil { 195 panic(err) 196 } 197 fmt.Printf("%#v", root) 198 199 200 // reuse PathNode memory 201 reuse := pathNodePool.Get().(*PathNode) 202 reuse.Node = NewNode(proto.MESSAGE, data) 203 err = root.Load(true, opts, desc) 204 if err != nil { 205 panic(err) 206 } 207 fmt.Printf("%#v", reuse) 208 reuse.ResetValue() 209 pathNodePool.Put(reuse) 210 } 211 212 func ExampleValue_MarshalAll() { 213 desc := getExample2Desc() 214 data := getExample2Data() 215 v := NewRootValue(desc, data) 216 p := PathNode{ 217 Node: v.Node, 218 } 219 220 if err := p.Load(true, opts, desc); err != nil { 221 panic(err) 222 } 223 224 out, err := p.Marshal(opts) 225 if err != nil { 226 panic(err) 227 } 228 229 msg, err := NewRootValue(desc, out).Interface(opts) 230 if err != nil { 231 panic(err) 232 } 233 fmt.Printf("%#v", msg) 234 } 235 236 func ExamplePathNode_MarshalMany(t *testing.T) { 237 desc := getExample2Desc() 238 data := getExample2Data() 239 240 v := NewRootValue(desc, data) 241 242 ps := []PathNode{ 243 {Path: NewPathFieldId(1)}, 244 // {Path: NewPathFieldId(3)}, 245 {Path: NewPathFieldId(32767)}, 246 } 247 248 err := v.GetMany(ps, opts) 249 if err != nil { 250 panic(err) 251 } 252 253 n := PathNode{ 254 Path: NewPathFieldId(1), // just used path type for message flag, id is not used 255 Node: v.Node, 256 Next: ps, 257 } 258 259 buf, err := n.Marshal(opts) 260 261 msg, err := NewRootValue(desc, buf).Interface(opts) 262 if err != nil { 263 panic(err) 264 } 265 fmt.Printf("%#v", msg) 266 267 }