github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/generic/example_test.go (about) 1 package generic 2 3 import ( 4 "fmt" 5 "reflect" 6 7 "github.com/cloudwego/dynamicgo/thrift" 8 ) 9 10 var opts = &Options{ 11 UseNativeSkip: true, 12 } 13 14 func ExampleNewTypedNode() { 15 // make a map<string,list<i32>> node 16 ret := NewTypedNode(thrift.MAP, thrift.LIST, thrift.STRING, PathNode{ 17 Path: NewPathStrKey("1"), 18 Node: NewNodeList([]interface{}{int32(1), int32(2)}), 19 }) 20 21 // print raw data 22 fmt.Printf("buf:%+v\n", ret.Raw()) 23 24 // print interface 25 val, err := ret.Interface(opts) 26 fmt.Printf("val:%#v\n", val) 27 if err != nil { 28 panic(err) 29 } 30 if !reflect.DeepEqual(val, map[string]interface{}{"1": []interface{}{int(1), int(2)}}) { 31 panic("not equal") 32 } 33 34 // make a struct{1:map<string, binary>} node 35 ret = NewTypedNode(thrift.STRUCT, 0, 0, PathNode{ 36 Path: NewPathFieldId(1), 37 Node: NewNodeMap(map[interface{}]interface{}{"1": []byte{1}}, &Options{}), 38 }) 39 // print interface 40 opts.CastStringAsBinary = true 41 opts.MapStructById = true 42 val, err = ret.Interface(opts) 43 fmt.Printf("val:%#v\n", val) 44 if err != nil { 45 panic(err) 46 } 47 if !reflect.DeepEqual(val, map[thrift.FieldID]interface{}{thrift.FieldID(1): map[string]interface{}{"1": []byte{1}}}) { 48 panic("not equal") 49 } 50 } 51 52 func ExampleValue_SetByPath() { 53 // pack root value 54 desc := getExampleDesc() 55 data := getExampleData() 56 v := NewValue(desc, data) 57 58 // pack insert value 59 d := desc.Struct().FieldByKey("Base").Type().Struct().FieldByKey("Extra").Type().Elem() 60 p := thrift.NewBinaryProtocol([]byte{}) 61 exp := "中文" 62 p.WriteString(exp) 63 buf := p.RawBuf() 64 vv := NewValue(d, buf) 65 66 // insert value 67 ps := []Path{NewPathFieldName("Base"), NewPathFieldName("Extra"), NewPathStrKey("b")} 68 exist, err2 := v.SetByPath(vv, ps...) 69 if err2 != nil { 70 panic(err2) 71 } 72 println(exist) // false 73 74 // check inserted value 75 s2 := v.GetByPath(ps...) 76 if s2.Error() != "" { 77 panic(s2.Error()) 78 } 79 f2, _ := s2.String() 80 println(f2) // 中文 81 } 82 83 func ExampleValue_SetMany() { 84 // make root value 85 desc := getExampleDesc() 86 data := getExampleData() 87 d1 := desc.Struct().FieldByKey("Msg").Type() 88 d2 := desc.Struct().FieldByKey("Subfix").Type() 89 v := NewValue(desc, data) 90 91 // make insert values 92 p := thrift.NewBinaryProtocol([]byte{}) 93 e1 := "test1" 94 p.WriteString(e1) 95 v1 := NewValue(d1, p.RawBuf()) 96 p = thrift.NewBinaryProtocol([]byte{}) 97 e2 := float64(-255.0001) 98 p.WriteDouble(e2) 99 v2 := NewValue(d2, p.RawBuf()) 100 v3 := v.GetByPath(NewPathFieldName("Base")) 101 102 // pack insert pathes and values 103 ps := []PathNode{ 104 { 105 Path: NewPathFieldId(1), 106 Node: v1.Node, 107 }, 108 { 109 Path: NewPathFieldId(32767), 110 Node: v2.Node, 111 }, 112 { 113 Path: NewPathFieldId(255), 114 Node: v3.Node, 115 }, 116 } 117 118 // insert values 119 err := v.SetMany(ps, opts) 120 if err != nil { 121 panic(err) 122 } 123 any, err := v.Interface(opts) 124 if err != nil { 125 panic(err) 126 } 127 fmt.Printf("%#v", any) 128 129 // check inserted values 130 ps2 := []PathNode{ 131 { 132 Path: NewPathFieldId(1), 133 }, 134 { 135 Path: NewPathFieldId(32767), 136 }, 137 { 138 Path: NewPathFieldId(255), 139 }, 140 } 141 if err := v.GetMany(ps2, opts); err != nil { 142 panic(err) 143 } 144 any0, err := ps2[2].Node.Interface(opts) 145 if err != nil { 146 panic(err) 147 } 148 fmt.Printf("%#v", any0) 149 } 150 151 func ExampleValue_MarshalTo() { 152 // make full value 153 desc := getExampleDesc() 154 data := getExampleData() 155 v := NewValue(desc, data) 156 157 // print full value 158 full, err := NewNode(thrift.STRUCT, data).Interface(opts) 159 if err != nil { 160 panic(err) 161 } 162 fmt.Printf("%#v", full) 163 164 // get partial descriptor 165 pdesc := getExamplePartialDesc() 166 // cut full value to partial value 167 out, err := v.MarshalTo(pdesc, opts) 168 if err != nil { 169 panic(err) 170 } 171 172 // print partial value 173 partial, err := NewNode(thrift.STRUCT, out).Interface(opts) 174 if err != nil { 175 panic(err) 176 } 177 fmt.Printf("%#v", partial) 178 } 179 180 func ExamplePathNode_Load() { 181 // make root PathNode 182 data := getExampleData() 183 root := PathNode{ 184 Node: NewNode(thrift.STRUCT, data), 185 } 186 187 // load first level children 188 err := root.Load(false, opts) 189 if err != nil { 190 panic(err) 191 } 192 fmt.Printf("%#v", root) 193 194 // load all level children 195 err = root.Load(true, opts) 196 if err != nil { 197 panic(err) 198 } 199 fmt.Printf("%#v", root) 200 201 // reuse PathNode memory 202 reuse := pathNodePool.Get().(*PathNode) 203 root.Node = NewNode(thrift.STRUCT, data) 204 err = root.Load(true, opts) 205 if err != nil { 206 panic(err) 207 } 208 fmt.Printf("%#v", root) 209 reuse.ResetValue() 210 pathNodePool.Put(reuse) 211 212 }