Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(generic): fix a bug where default values are not set with dynamicgo #1572

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/generic/http_test/idl/binary_echo.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct BinaryWrapper {
1: binary msg (api.body = "msg")
2: bool got_base64 (api.body = "got_base64")
3: required i64 num (api.body = "num", api.js_conv="")
4: optional string str (api.query = "str", go.tag = "json:\"STR\"")
4: optional string str = "echo" (api.query = "str", go.tag = "json:\"STR\"")
}

service ExampleService {
Expand Down
4 changes: 2 additions & 2 deletions pkg/generic/thriftidl_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewThriftFileProviderWithDynamicgoWithOption(path string, opts []ThriftIDLP
return nil, err
}
handleGoTagForDynamicGo(tOpts.goTag)
dOpts := dthrift.Options{EnableThriftBase: true, ParseServiceMode: dParseMode}
dOpts := dthrift.Options{EnableThriftBase: true, ParseServiceMode: dParseMode, UseDefaultValue: true}
dsvc, err := dOpts.NewDescritorFromPath(context.Background(), path, includeDirs...)
if err != nil {
// fall back to the original way (without dynamicgo)
Expand Down Expand Up @@ -480,7 +480,7 @@ func newDynamicGoDscFromContent(svc *descriptor.ServiceDescriptor, path, content
if err != nil {
return err
}
dOpts := dthrift.Options{EnableThriftBase: true, ParseServiceMode: dParseMode}
dOpts := dthrift.Options{EnableThriftBase: true, ParseServiceMode: dParseMode, UseDefaultValue: true}
dsvc, err := dOpts.NewDescritorFromContent(context.Background(), path, content, includes, isAbsIncludePath)
if err != nil {
klog.CtxWarnf(context.Background(), "KITEX: failed to get dynamicgo service descriptor, fall back to the original way, error=%s", err)
Expand Down
42 changes: 41 additions & 1 deletion pkg/generic/thriftidl_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var testServiceContent = `
struct ExampleReq {
1: required string Msg (go.tag = "json:\"message\""),
2: FOO Foo,
4: optional i8 I8,
4: optional i8 I8 = 8,
5: optional i16 I16,
6: optional i32 I32,
7: optional i64 I64,
Expand Down Expand Up @@ -510,3 +510,43 @@ func TestContentWithAbsIncludePathProviderParseModeWithDynamicGo(t *testing.T) {
test.Assert(t, tree.Name == "CombinedServices")
test.Assert(t, tree.DynamicGoDsc.Name() == "CombinedServices")
}

func TestDefaultValue(t *testing.T) {
// file provider
path := "http_test/idl/binary_echo.thrift"
p, err := NewThriftFileProviderWithDynamicGo(path)
test.Assert(t, err == nil)
pd, ok := p.(GetProviderOption)
test.Assert(t, ok)
test.Assert(t, pd.Option().DynamicGoEnabled)
tree := <-p.Provide()
test.Assert(t, tree != nil)
test.Assert(t, tree.DynamicGoDsc != nil)
test.Assert(t, tree.Functions["BinaryEcho"].Request.Struct.FieldsByID[1].Type.Struct.FieldsByID[4].DefaultValue == "echo")
test.Assert(t, tree.DynamicGoDsc.Functions()["BinaryEcho"].Request().Struct().FieldById(1).Type().Struct().FieldById(4).DefaultValue().JSONValue() == `"echo"`)
p.Close()

// content provider
p, err = NewThriftContentProviderWithDynamicGo(testServiceContent, nil)
test.Assert(t, err == nil)
tree = <-p.Provide()
test.Assert(t, tree != nil)
test.Assert(t, tree.Name == "Example2Service")
test.Assert(t, tree.Functions["Example2Method"].Request.Struct.FieldsByID[1].Type.Struct.FieldsByID[4].DefaultValue == uint8(8))
test.Assert(t, tree.DynamicGoDsc.Name() == "Example2Service")
test.Assert(t, tree.DynamicGoDsc.Functions()["Example2Method"].Request().Struct().FieldById(1).Type().Struct().FieldById(4).DefaultValue().JSONValue() == "8")
p.Close()

// content with abs include path provider
path = "json_test/idl/example_multi_service.thrift"
includes := map[string]string{path: testServiceContent}
p, err = NewThriftContentWithAbsIncludePathProviderWithDynamicGo(path, includes)
test.Assert(t, err == nil, err)
tree = <-p.Provide()
test.Assert(t, tree != nil)
test.Assert(t, tree.Name == "Example2Service")
test.Assert(t, tree.DynamicGoDsc.Name() == "Example2Service")
test.Assert(t, tree.DynamicGoDsc.Name() == "Example2Service")
test.Assert(t, tree.DynamicGoDsc.Functions()["Example2Method"].Request().Struct().FieldById(1).Type().Struct().FieldById(4).DefaultValue().JSONValue() == "8")
p.Close()
}