Skip to content

Commit

Permalink
fix: support conversion from string to int and float for json generic…
Browse files Browse the repository at this point in the history
… to be consistent with the original json generic (#1090)
  • Loading branch information
Marina-Sakai committed Aug 30, 2023
1 parent 79bbceb commit f9373f9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/bytedance/sonic v1.9.1
github.com/choleraehyq/pid v0.0.17
github.com/cloudwego/configmanager v0.2.0
github.com/cloudwego/dynamicgo v0.1.2
github.com/cloudwego/dynamicgo v0.1.3
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/frugal v0.1.7
github.com/cloudwego/localsession v0.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cloudwego/configmanager v0.2.0 h1:niVpVg+wQ+npNqnH3dup96SMbR02Pk+tNErubYCJqKo=
github.com/cloudwego/configmanager v0.2.0/go.mod h1:FLIQTjxsZRGjnmDhTttWQTy6f6DghPTatfBVOs2gQLk=
github.com/cloudwego/dynamicgo v0.1.0/go.mod h1:Mdsz0XGsIImi15vxhZaHZpspNChEmBMIiWkUfD6JDKg=
github.com/cloudwego/dynamicgo v0.1.2 h1:t5KMzo/UkT002n3EvGI0Y6+Me73NGDzFI/AQlT1LQME=
github.com/cloudwego/dynamicgo v0.1.2/go.mod h1:AdPqyFN+0+fc3iVSSWojDCnOGPkzH+T0rI65017GCUA=
github.com/cloudwego/dynamicgo v0.1.3 h1:xK2rFS3E7cGbo4CWhqP1HIWeQcVH4Po5YgdNFDC+CfI=
github.com/cloudwego/dynamicgo v0.1.3/go.mod h1:AdPqyFN+0+fc3iVSSWojDCnOGPkzH+T0rI65017GCUA=
github.com/cloudwego/fastpb v0.0.3/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
github.com/cloudwego/fastpb v0.0.4 h1:/ROVVfoFtpfc+1pkQLzGs+azjxUbSOsAqSY4tAAx4mg=
github.com/cloudwego/fastpb v0.0.4/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
Expand Down
4 changes: 3 additions & 1 deletion pkg/generic/json_test/generic_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (

var reqMsg = `{"Msg":"hello","InnerBase":{"Base":{"LogID":"log_id_inner"}},"Base":{"LogID":"log_id"}}`

var reqRegression = `{"Msg":"hello","InnerBase":{"Base":{"LogID":"log_id_inner"}},"Base":{"LogID":"log_id"},"I8":"8","I16":"16","I32":"32","I64":"64","Double":"12.3"}`

var respMsgWithExtra = `{"Msg":"world","required_field":"required_field","extra_field":"extra_field"}`

var errResp = "Test Error"
Expand Down Expand Up @@ -388,6 +390,6 @@ func (g *GenericRegressionImpl) GenericCall(ctx context.Context, method string,
fmt.Printf("Method from Ctx: %s\n", rpcinfo.Invocation().MethodName())
fmt.Printf("Recv: %v\n", buf)
fmt.Printf("Method: %s\n", method)
respMsg := `{"Msg":"world","required_field":"required_field", "num": 64}`
respMsg := `{"Msg":"world","required_field":"required_field","num":64,"I8":"8","I16":"16","I32":"32","I64":"64","Double":"12.3"}`
return respMsg, nil
}
23 changes: 21 additions & 2 deletions pkg/generic/json_test/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,21 +516,40 @@ func testRegression(t *testing.T) {

// normal way
cli := initThriftClient(transport.TTHeader, t, "127.0.0.1:8127", "./idl/example.thrift", nil, nil, false)
resp, err := cli.GenericCall(context.Background(), "ExampleMethod", reqMsg, callopt.WithRPCTimeout(100*time.Second))
resp, err := cli.GenericCall(context.Background(), "ExampleMethod", reqRegression, callopt.WithRPCTimeout(100*time.Second))
test.Assert(t, err == nil, err)
respStr, ok := resp.(string)
test.Assert(t, ok)
test.Assert(t, gjson.Get(respStr, "num").Type == gjson.String)
test.Assert(t, gjson.Get(respStr, "num").String() == "64")
test.Assert(t, gjson.Get(respStr, "I8").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "I8").Int() == int64(8))
test.Assert(t, gjson.Get(respStr, "I16").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "I32").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "I64").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "Double").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "Double").Float() == 12.3)

svr.Stop()

time.Sleep(1 * time.Second)
svr = initThriftServer(t, ":8127", new(GenericRegressionImpl), "./idl/example.thrift", nil, nil, true)

// dynamicgo way
cli = initThriftClient(transport.TTHeader, t, "127.0.0.1:8127", "./idl/example.thrift", nil, nil, true)
resp, err = cli.GenericCall(context.Background(), "ExampleMethod", reqMsg, callopt.WithRPCTimeout(100*time.Second))
resp, err = cli.GenericCall(context.Background(), "ExampleMethod", reqRegression, callopt.WithRPCTimeout(100*time.Second))
test.Assert(t, err == nil, err)
respStr, ok = resp.(string)
test.Assert(t, ok)
test.Assert(t, gjson.Get(respStr, "num").Type == gjson.String)
test.Assert(t, gjson.Get(respStr, "num").String() == "64")
test.Assert(t, gjson.Get(respStr, "I8").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "I8").Int() == int64(8))
test.Assert(t, gjson.Get(respStr, "I16").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "I32").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "I64").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "Double").Type == gjson.Number)
test.Assert(t, gjson.Get(respStr, "Double").Float() == 12.3)

svr.Stop()
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/generic/json_test/idl/example.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ struct ExampleReq {
1: required string Msg,
2: FOO Foo,
3: InnerBase InnerBase,
4: optional i8 I8,
5: optional i16 I16,
6: optional i32 I32,
7: optional i64 I64,
8: optional double Double,
255: base.Base Base,
}
struct ExampleResp {
1: required string Msg,
2: string required_field,
3: optional i64 num (api.js_conv="true"),
4: optional i8 I8,
5: optional i16 I16,
6: optional i32 I32,
7: optional i64 I64,
8: optional double Double,
255: base.BaseResp BaseResp,
}
exception Exception {
Expand Down
1 change: 1 addition & 0 deletions pkg/generic/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
WriteRequireField: true,
WriteDefaultField: true,
EnableValueMapping: true,
String2Int64: true,
}
)

Expand Down

0 comments on commit f9373f9

Please sign in to comment.