Skip to content

Commit

Permalink
feat:(thrift) support putting IDL filename into Descriptor's annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkerQAQ committed Aug 9, 2024
1 parent 1e6bfca commit 8870f42
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions thrift/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,17 @@ func extractNameSpaceToAnnos(ast *parser.Thrift) parser.Annotation {
return ret
}

// FilenameAnnotationKey is used for Option.PutThriftFilenameToAnnotation
const FilenameAnnotationKey = "thrift.filename"

func extractThriftFilePathToAnnos(ast *parser.Thrift) parser.Annotation {
ret := parser.Annotation{
Key: FilenameAnnotationKey,
}
ret.Values = append(ret.Values, ast.GetFilename())
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
12 changes: 12 additions & 0 deletions thrift/idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ type Options struct {
// `namespace go base` will got ["go", "base"]
// NOTICE: at present, only StructDescriptor.Annotations() can get this
PutNameSpaceToAnnotation bool

// PutThriftFilenameToAnnotation indicates to extract the filename of one type
// and put it on the type's annotation. The annotion format is:
// - Key: "thrift.filename" (== FilenameAnnotationKey)
// - Values: pairs of Language and Name. for example:
// `// path := /a/b/c.thrift` will got ["/a/b/c.thrift"]
// NOTICE: at present, only StructDescriptor.Annotations() can get this
PutThriftFilenameToAnnotation bool
}

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

if opts.PutThriftFilenameToAnnotation {
oannos = append(oannos, extractThriftFilePathToAnnos(tree))
}

// inject previous annotations
injectAnnotations((*[]*parser.Annotation)(&st.Annotations), nextAnns)

Expand Down
20 changes: 20 additions & 0 deletions thrift/idl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math"
"path/filepath"
"testing"

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

func TestOptionPutThriftFilenameToAnnotation(t *testing.T) {
path := filepath.Join("..", "testdata", "idl", "example.thrift")
opt := Options{PutThriftFilenameToAnnotation: true}
descriptor, err := opt.NewDescritorFromPath(context.Background(), path)
require.NoError(t, err)
method, err := descriptor.LookupFunctionByMethod("ExampleMethod")
require.NoError(t, err)
req := method.Request().Struct().Fields()[0].Type()
annos := req.Struct().Annotations()
var filename *parser.Annotation
for i, a := range annos {
if a.Key == FilenameAnnotationKey {
filename = &annos[i]
break
}
}
require.Equal(t, filename.Values, []string{path})
}

func TestNewFunctionDescriptorFromContent_absPath(t *testing.T) {
content := `
include "/a/b/main.thrift"
Expand Down

0 comments on commit 8870f42

Please sign in to comment.