Skip to content

Commit

Permalink
feat:(thrift) support putting IDL name-space into Descriptor's annota…
Browse files Browse the repository at this point in the history
…tions (#49)

* feat:(thrift) support putting IDL name-space into annotations

* commnent

* update sonic
  • Loading branch information
AsterDY committed Apr 26, 2024
1 parent d2b8419 commit db74400
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ 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=
github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY=
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=
Expand Down
17 changes: 17 additions & 0 deletions thrift/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions thrift/idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions thrift/idl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit db74400

Please sign in to comment.