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

feat:(thrift) support putting IDL filename into Descriptor's annotations #71

Merged
merged 1 commit into from
Aug 12, 2024
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
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
Loading