Skip to content

Commit cb44ce6

Browse files
authored
feat:(thrift) support putting IDL filename into Descriptor's annotations (#71)
Co-authored-by: zhangshengkun <zhangshengkun@bytedance.com>
1 parent 0edfd20 commit cb44ce6

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

thrift/annotation.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,17 @@ func extractNameSpaceToAnnos(ast *parser.Thrift) parser.Annotation {
440440
return ret
441441
}
442442

443+
// FilenameAnnotationKey is used for Option.PutThriftFilenameToAnnotation
444+
const FilenameAnnotationKey = "thrift.filename"
445+
446+
func extractThriftFilePathToAnnos(ast *parser.Thrift) parser.Annotation {
447+
ret := parser.Annotation{
448+
Key: FilenameAnnotationKey,
449+
}
450+
ret.Values = append(ret.Values, ast.GetFilename())
451+
return ret
452+
}
453+
443454
// injectAnnotation injects next annotation by appending.
444455
// NOTICE: the next annotation will be appended to the end of the current annotation.
445456
func injectAnnotations(origin *[]*parser.Annotation, next []parser.Annotation) error {

thrift/idl.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ type Options struct {
8383
// `namespace go base` will got ["go", "base"]
8484
// NOTICE: at present, only StructDescriptor.Annotations() can get this
8585
PutNameSpaceToAnnotation bool
86+
87+
// PutThriftFilenameToAnnotation indicates to extract the filename of one type
88+
// and put it on the type's annotation. The annotion format is:
89+
// - Key: "thrift.filename" (== FilenameAnnotationKey)
90+
// - Values: pairs of Language and Name. for example:
91+
// `// path := /a/b/c.thrift` will got ["/a/b/c.thrift"]
92+
// NOTICE: at present, only StructDescriptor.Annotations() can get this
93+
PutThriftFilenameToAnnotation bool
8694
}
8795

8896
// NewDefaultOptions creates a default Options.
@@ -559,6 +567,10 @@ func parseType(ctx context.Context, t *parser.Type, tree *parser.Thrift, cache c
559567
oannos = append(oannos, extractNameSpaceToAnnos(tree))
560568
}
561569

570+
if opts.PutThriftFilenameToAnnotation {
571+
oannos = append(oannos, extractThriftFilePathToAnnos(tree))
572+
}
573+
562574
// inject previous annotations
563575
injectAnnotations((*[]*parser.Annotation)(&st.Annotations), nextAnns)
564576

thrift/idl_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"math"
23+
"path/filepath"
2324
"testing"
2425

2526
"github.com/cloudwego/thriftgo/parser"
@@ -303,6 +304,25 @@ func TestOptionPutNameSpaceToAnnotation(t *testing.T) {
303304
require.Equal(t, ns.Values, []string{"py", "py.base", "go", "go.base"})
304305
}
305306

307+
func TestOptionPutThriftFilenameToAnnotation(t *testing.T) {
308+
path := filepath.Join("..", "testdata", "idl", "example.thrift")
309+
opt := Options{PutThriftFilenameToAnnotation: true}
310+
descriptor, err := opt.NewDescritorFromPath(context.Background(), path)
311+
require.NoError(t, err)
312+
method, err := descriptor.LookupFunctionByMethod("ExampleMethod")
313+
require.NoError(t, err)
314+
req := method.Request().Struct().Fields()[0].Type()
315+
annos := req.Struct().Annotations()
316+
var filename *parser.Annotation
317+
for i, a := range annos {
318+
if a.Key == FilenameAnnotationKey {
319+
filename = &annos[i]
320+
break
321+
}
322+
}
323+
require.Equal(t, filename.Values, []string{path})
324+
}
325+
306326
func TestNewFunctionDescriptorFromContent_absPath(t *testing.T) {
307327
content := `
308328
include "/a/b/main.thrift"

0 commit comments

Comments
 (0)