Skip to content

Commit

Permalink
fix(thrift): support both relative and absolute check for idl include…
Browse files Browse the repository at this point in the history
…s parse to make it compatible with generation tool (#90)

* fix: support both relative and absolute check

* fix: relative check first

* optimize: reduce memory allocations
  • Loading branch information
Marina-Sakai authored Mar 2, 2025
1 parent fefc46e commit 55eb3a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
37 changes: 23 additions & 14 deletions thrift/idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,34 @@ func parseIDLContent(path, content string, includes map[string]string, isAbsIncl
func refIncludes(tree *parser.Thrift, path string, done map[string]*parser.Thrift, includes map[string]*parser.Thrift, isAbsIncludePath bool) error {
done[path] = tree
for _, i := range tree.Includes {
p := i.Path
ps := make([]string, 0, 2)
if isAbsIncludePath {
p = absPath(tree.Filename, i.Path)
p := absPath(tree.Filename, i.Path)
if p != i.Path {
ps = append(ps, p)
}
}
ps = append(ps, i.Path)

// check cycle reference
if t := done[p]; t != nil {
i.Reference = t
continue
}
for _, p := range ps {
// check cycle reference
if t := done[p]; t != nil {
i.Reference = t
continue
}

ref, ok := includes[p]
if !ok {
return fmt.Errorf("miss include path: %s for file: %s", p, tree.Filename)
}
if err := refIncludes(ref, p, done, includes, isAbsIncludePath); err != nil {
return err
ref, ok := includes[p]
if !ok {
if !isAbsIncludePath {
return fmt.Errorf("miss include path: %s for file: %s", p, tree.Filename)
}
continue
}
if err := refIncludes(ref, p, done, includes, isAbsIncludePath); err != nil {
return err
}
i.Reference = ref
}
i.Reference = ref
}
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions thrift/idl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func TestThriftContentWithAbsIncludePath(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("%#v\n", p)

delete(includes, "a/b/x.thrift")
includes["x.thrift"] = "namespace go kitex.test.server"
p, err = NewDescritorFromContent(context.Background(), path, content, includes, true)
if err != nil {
t.Fatal(err)
}
fmt.Printf("%#v\n", p)
}

func TestBitmap(t *testing.T) {
Expand Down

0 comments on commit 55eb3a2

Please sign in to comment.