github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/testdata/example_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 testdata 18 19 import ( 20 ejson "encoding/json" 21 "io/ioutil" 22 "os" 23 "runtime" 24 "runtime/debug" 25 "testing" 26 "time" 27 28 "github.com/cloudwego/dynamicgo/testdata/kitex_gen/base" 29 "github.com/cloudwego/dynamicgo/testdata/kitex_gen/example2" 30 "github.com/cloudwego/dynamicgo/testdata/sample" 31 "github.com/cloudwego/dynamicgo/thrift/annotation" 32 jsoniter "github.com/json-iterator/go" 33 "github.com/stretchr/testify/require" 34 ) 35 36 var ( 37 genData = os.Getenv("DYNAMICGO_GEN_TESTDATA") != "" 38 debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == "" 39 ) 40 41 func TestMain(m *testing.M) { 42 go func() { 43 if !debugAsyncGC { 44 return 45 } 46 println("Begin GC looping...") 47 for { 48 runtime.GC() 49 debug.FreeOSMemory() 50 } 51 }() 52 time.Sleep(time.Millisecond) 53 annotation.InitAGWAnnos() 54 m.Run() 55 } 56 57 // const ExampleReqJSON = "{\"Msg\":\"中文\",\"Base\":{\"LogID\":\"a\",\"Caller\":\"b\",\"Addr\":\"c\",\"Client\":\"d\"},\"Subfix\":-0.000000000001}" 58 59 // const ExampleReqJSON2 = `{"A":null,"Msg":"hello","InnerBase":{"Bool":true,"Byte":127,"Int16":-32768,"Int32":2147483647,"Int64":-9223372036854775808,"Double":1.7976931348623157e308,"String":"你好","ListInt32":[-1,0,1],"MapStringString":{"c":"C","a":"A","b":"B"},"SetInt32":[-1,0,1],"Foo":1,"Base":{"LogID":"log_id_inner","Caller":"","Addr":"","Client":""}},"Base":{"LogID":"log_id","Caller":"","Addr":"","Client":""}}` 60 61 // func TestThriftEncoderExample2(t *testing.T) { 62 // svc, err := thrift.NewDescritorFromPath("../testdata/idl/example2.thrift") 63 // if err != nil { 64 // t.Fatal(err) 65 // } 66 // req := svc.Functions()["ExampleMethod"].Request().Struct().FieldByKey("req").Type() 67 68 // t.Run("load", func(t *testing.T) { 69 // // stru: converted from j2t 70 // root, err := json.NewSearcher(ExampleReqJSON2).GetByPath() 71 // if err != nil { 72 // t.Fatal(err) 73 // } 74 // err = root.LoadAll() 75 // if err != nil { 76 // t.Fatal(err) 77 // } 78 // enc := &json.ThriftEncoder{Proto: meta.ThriftBinary, Options: json.NewDefaultOptions()} 79 // out, err := enc.Encode(req, root) 80 // if err != nil { 81 // t.Fatal(err) 82 // } 83 // spew.Dump(out) 84 // stru := example2.NewExampleReq() 85 // ret, err := stru.FastRead(out) 86 // if err != nil { 87 // t.Fatal(err) 88 // } 89 // println(ret) 90 91 // // exp: unmarshaled from json 92 // exp := example2.NewExampleReq() 93 // if err := sonic.UnmarshalString(ExampleReqJSON2, exp); err != nil { 94 // t.Fatal(err) 95 // } 96 // assert.Equal(t, exp, stru) 97 98 // b := make([]byte, exp.BLength()) 99 // if ret := exp.FastWriteNocopy(b, nil); ret < 0 { 100 // t.Fatal(ret) 101 // } 102 // // spew.Dump(b) 103 // // ioutil.WriteFile("./data/example2.bin", b, 0644) 104 // assert.Equal(t, len(b), len(out)) 105 // // assert.Equal(t, b, out) 106 // }) 107 108 // t.Run("raw", func(t *testing.T) { 109 // // stru: converted from j2t 110 // root, err := json.NewSearcher(ExampleReqJSON2).GetByPath() 111 // if err != nil { 112 // t.Fatal(err) 113 // } 114 // enc := &json.ThriftEncoder{Proto: meta.ThriftBinary, Options: json.NewDefaultOptions()} 115 // out, err := enc.Encode(req, root) 116 // if err != nil { 117 // t.Fatal(err) 118 // } 119 // spew.Dump(out) 120 // stru := example2.NewExampleReq() 121 // ret, err := stru.FastRead(out) 122 // if err != nil { 123 // t.Fatal(err) 124 // } 125 // println(ret) 126 127 // // exp: unmarshaled from json 128 // exp := example2.NewExampleReq() 129 // if err := sonic.UnmarshalString(ExampleReqJSON2, exp); err != nil { 130 // t.Fatal(err) 131 // } 132 // //FIXME: since native.j2t_fsm_exec() does not support handling null optional field, we set it to nil manually 133 // stru.SetA(nil) 134 // assert.Equal(t, exp, stru) 135 136 // b := make([]byte, exp.BLength()) 137 // if ret := exp.FastWriteNocopy(b, nil); ret < 0 { 138 // t.Fatal(ret) 139 // } 140 // // spew.Dump(b) 141 // // assert.Equal(t, len(b), len(out)) 142 // // assert.Equal(t, b, out) 143 // }) 144 // } 145 146 // func TestThriftEncoderExample0(t *testing.T) { 147 // svc, err := thrift.NewDescritorFromPath("../testdata/idl/example.thrift") 148 // if err != nil { 149 // t.Fatal(err) 150 // } 151 // req := svc.Functions()["ExampleMethod"].Request().Struct().FieldByKey("req").Type() 152 153 // root, err := json.NewSearcher(ExampleReqJSON).GetByPath() 154 // if err != nil { 155 // t.Fatal(err) 156 // } 157 // err = root.LoadAll() 158 // if err != nil { 159 // t.Fatal(err) 160 // } 161 162 // enc := &json.ThriftEncoder{Proto: meta.ThriftBinary, Options: json.NewDefaultOptions()} 163 // out, err := enc.Encode(req, root) 164 // if err != nil { 165 // t.Fatal(err) 166 // } 167 // spew.Dump(out) 168 169 // stru := example.NewExampleReq() 170 // _, err = stru.FastRead(out) 171 // if err != nil { 172 // t.Fatal(err) 173 // } 174 175 // struc := example.NewExampleReq() 176 // if err := sonic.UnmarshalString(ExampleReqJSON, struc); err != nil { 177 // t.Fatal(err) 178 // } 179 // assert.Equal(t, struc, stru) 180 181 // b := make([]byte, struc.BLength()) 182 // if ret := struc.FastWriteNocopy(b, nil); ret < 0 { 183 // t.Fatal(ret) 184 // } 185 // spew.Dump(b) 186 // assert.Equal(t, len(b), len(out)) 187 188 // enc.WriteDefault = false 189 // out, err = enc.Encode(req, root) 190 // if err != nil { 191 // t.Fatal(err) 192 // } 193 // spew.Dump(out) 194 // stru = example.NewExampleReq() 195 // _, err = stru.FastRead(out) 196 // if err != nil { 197 // t.Fatal(err) 198 // } 199 // } 200 201 func TestGenExample(t *testing.T) { 202 obj := example2.NewExampleReq() 203 msg := "中文" 204 obj.Msg = &msg 205 obj.Subfix = -0.000000000001 206 obj.Base = base.NewBase() 207 obj.Base.LogID = "a" 208 obj.Base.Caller = "b" 209 obj.Base.Addr = "c" 210 obj.Base.Client = "d" 211 out := make([]byte, obj.BLength()) 212 ret := obj.FastWriteNocopy(out, nil) 213 if ret <= 0 { 214 t.Fatal(ret) 215 } 216 if !genData { 217 return 218 } 219 require.Nil(t, ioutil.WriteFile("./data/example.bin", out, 0644)) 220 out, err := ejson.Marshal(obj) 221 require.Nil(t, err) 222 require.Nil(t, ioutil.WriteFile("./data/example.json", out, 0644)) 223 } 224 225 func TestGenExample2(t *testing.T) { 226 obj := sample.GetExample3Req() 227 out := make([]byte, obj.BLength()) 228 ret := obj.FastWriteNocopy(out, nil) 229 if ret <= 0 { 230 t.Fatal(ret) 231 } 232 if !genData { 233 return 234 } 235 require.Nil(t, ioutil.WriteFile("./data/example2.bin", out, 0644)) 236 // out, err := ejson.Marshal(obj) 237 // require.Nil(t, err) 238 // require.Nil(t, ioutil.WriteFile("./data/example2.json", out, 0644)) 239 } 240 241 func TestGenExample2Super(t *testing.T) { 242 obj := sample.GetExample2ReqSuper() 243 out := make([]byte, obj.BLength()) 244 ret := obj.FastWriteNocopy(out, nil) 245 if ret <= 0 { 246 t.Fatal(ret) 247 } 248 if !genData { 249 return 250 } 251 require.Nil(t, ioutil.WriteFile("./data/example2super.bin", out, 0644)) 252 // out, err := ejson.Marshal(obj) 253 // require.Nil(t, err) 254 // require.Nil(t, ioutil.WriteFile("./data/example2super.json", out, 0644)) 255 } 256 257 func TestGenExample3Req(t *testing.T) { 258 obj := sample.GetExample3Req() 259 out := make([]byte, obj.BLength()) 260 ret := obj.FastWriteNocopy(out, nil) 261 if ret <= 0 { 262 t.Fatal(ret) 263 } 264 if !genData { 265 return 266 } 267 require.Nil(t, ioutil.WriteFile("./data/example3req.bin", out, 0644)) 268 out, err := jsoniter.Marshal(obj) 269 require.NoError(t, err) 270 require.Nil(t, ioutil.WriteFile("./data/example3req.json", out, 0644)) 271 } 272 273 func TestGenExample3Resp(t *testing.T) { 274 obj := sample.GetExample3Resp() 275 out := make([]byte, obj.BLength()) 276 ret := obj.FastWriteNocopy(out, nil) 277 if ret <= 0 { 278 t.Fatal(ret) 279 } 280 if !genData { 281 return 282 } 283 require.Nil(t, ioutil.WriteFile("./data/example3resp.bin", out, 0644)) 284 out, err := ejson.Marshal(obj) 285 require.Nil(t, err) 286 require.Nil(t, ioutil.WriteFile("./data/example3resp.json", out, 0644)) 287 }