From db744009c5fea1f6725607b553fa7449c879a007 Mon Sep 17 00:00:00 2001 From: Yi Duan Date: Fri, 26 Apr 2024 19:04:32 +0800 Subject: [PATCH] feat:(thrift) support putting IDL name-space into Descriptor's annotations (#49) * feat:(thrift) support putting IDL name-space into annotations * commnent * update sonic --- go.mod | 6 +++--- go.sum | 9 ++++++--- thrift/annotation.go | 17 +++++++++++++++++ thrift/idl.go | 11 +++++++++++ thrift/idl_test.go | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 1fc79dbf..abe77aae 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,9 @@ go 1.16 require ( github.com/apache/thrift v0.13.0 - github.com/bytedance/sonic v1.11.5 - github.com/bytedance/sonic/loader v0.1.0 - github.com/cloudwego/base64x v0.1.3 + github.com/bytedance/sonic v1.11.6 + github.com/bytedance/sonic/loader v0.1.1 + github.com/cloudwego/base64x v0.1.4 github.com/cloudwego/fastpb v0.0.4 github.com/cloudwego/kitex v0.9.3-rc github.com/cloudwego/thriftgo v0.3.6 diff --git a/go.sum b/go.sum index 097096fa..dddaebd7 100644 --- a/go.sum +++ b/go.sum @@ -16,10 +16,12 @@ github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7/go.mod h1:2ZlV9BaU github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b h1:R6PWoQtxEMpWJPHnpci+9LgFxCS7iJCfOGBvCgZeTKI= github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= github.com/bytedance/mockey v1.2.7/go.mod h1:bNrUnI1u7+pAc0TYDgPATM+wF2yzHxmNH+iDXg4AOCU= -github.com/bytedance/sonic v1.11.5 h1:G00FYjjqll5iQ1PYXynbg/hyzqBqavH8Mo9/oTopd9k= github.com/bytedance/sonic v1.11.5/go.mod h1:X2PC2giUdj/Cv2lliWFLk6c/DUQok5rViJSemeB0wDw= -github.com/bytedance/sonic/loader v0.1.0 h1:skjHJ2Bi9ibbq3Dwzh1w42MQ7wZJrXmEZr/uqUn3f0Q= +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9BqwBO3pm9H/+HY= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/choleraehyq/pid v0.0.18 h1:O7LLxPoOyt3YtonlCC8BmNrF9P6Hc8B509UOqlPSVhw= github.com/choleraehyq/pid v0.0.18/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= @@ -27,8 +29,9 @@ github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4M github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cloudwego/base64x v0.1.3 h1:b5J/l8xolB7dyDTTmhJP2oTs5LdrjyrUFuNxdfq5hAg= github.com/cloudwego/base64x v0.1.3/go.mod h1:1+1K5BUHIQzyapgpF7LwvOGAEDicKtt1umPV+aN8pi8= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/configmanager v0.2.1/go.mod h1:0oD/BWaTuznBOawVeTmTl3LE99RWaw7rX2jECowmY58= github.com/cloudwego/dynamicgo v0.2.2-dep3/go.mod h1:oFLzd9SEUtU7XbSc7AT9e5xoAV1OJ1mVpudtUOiD7PQ= github.com/cloudwego/dynamicgo v0.2.2/go.mod h1:k840iCFH9ng9PBqr6jIoOyZxdk58EPEccrbfOk4ni1s= diff --git a/thrift/annotation.go b/thrift/annotation.go index 840235e1..46221923 100644 --- a/thrift/annotation.go +++ b/thrift/annotation.go @@ -423,6 +423,23 @@ func copyAnnotationValues(anns []*parser.Annotation) []parser.Annotation { return ret } +// NameSpaceAnnotationKey is used for Option.PutNameSpaceToAnnotation +const NameSpaceAnnotationKey = "thrift.name_space" + +func extractNameSpaceToAnnos(ast *parser.Thrift) parser.Annotation { + ret := parser.Annotation{ + Key: NameSpaceAnnotationKey, + } + for _, s := range ast.Namespaces { + if s == nil { + continue + } + ret.Values = append(ret.Values, s.Language) + ret.Values = append(ret.Values, s.Name) + } + return ret +} + // injectAnnotation injects next annotation by appending. // NOTICE: the next annotation will be appended to the end of the current annotation. func injectAnnotations(origin *[]*parser.Annotation, next []parser.Annotation) error { diff --git a/thrift/idl.go b/thrift/idl.go index 5ce5d18d..d406f141 100644 --- a/thrift/idl.go +++ b/thrift/idl.go @@ -75,6 +75,14 @@ type Options struct { // 1. Its type is 'base.Base' (for request base) or 'base.BaseResp' (for response base); // 2. it is on the top layer of the root struct of one function. EnableThriftBase bool + + // PutNameSpaceToAnnotation indicates to extract the name-space of one type + // and put it on the type's annotation. The annotion format is: + // - Key: "thrift.name_space" (== NameSpaceAnnotationKey) + // - Values: pairs of Language and Name. for example: + // `namespace go base` will got ["go", "base"] + // NOTICE: at present, only StructDescriptor.Annotations() can get this + PutNameSpaceToAnnotation bool } // NewDefaultOptions creates a default Options. @@ -547,6 +555,9 @@ func parseType(ctx context.Context, t *parser.Type, tree *parser.Thrift, cache c // copy original annotations oannos := copyAnnotationValues(st.Annotations) + if opts.PutNameSpaceToAnnotation { + oannos = append(oannos, extractNameSpaceToAnnos(tree)) + } // inject previous annotations injectAnnotations((*[]*parser.Annotation)(&st.Annotations), nextAnns) diff --git a/thrift/idl_test.go b/thrift/idl_test.go index 7ae5dd7a..9066f3ac 100644 --- a/thrift/idl_test.go +++ b/thrift/idl_test.go @@ -271,6 +271,38 @@ func TestOptionSetOptionalBitmap(t *testing.T) { require.Equal(t, true, req.Struct().Requires().IsSet(3)) } +func TestOptionPutNameSpaceToAnnotation(t *testing.T) { + content := ` + namespace py py.base + namespace go go.base + + struct Base { + 1: string DefaultField, + 2: optional string OptionalField, + 3: required string RequiredField, + } + + service InboxService { + Base ExampleMethod(1: Base req) + } + ` + p, err := GetDescFromContent(content, "ExampleMethod", &Options{ + PutNameSpaceToAnnotation: true, + }) + require.NoError(t, err) + req := p.Request().Struct().Fields()[0].Type() + annos := req.Struct().Annotations() + var ns *parser.Annotation + for i, a := range annos { + if a.Key == NameSpaceAnnotationKey { + ns = &annos[i] + break + } + } + require.NotNil(t, ns) + require.Equal(t, ns.Values, []string{"py", "py.base", "go", "go.base"}) +} + func TestNewFunctionDescriptorFromContent_absPath(t *testing.T) { content := ` include "/a/b/main.thrift"