Skip to content

Commit

Permalink
Teach ast.Resolve() about 'include' paths (#34)
Browse files Browse the repository at this point in the history
We previously assumed a 1-to-1 relationship between type reference names
and .thrift files in the search path, but the 'include' header allows
for an additional degree of indirection.

For example, we could `include "subdir/file.thrift"`, which allows for
type references named `file.TypeName`, but the previously logic would
fail to load `file.thrift` unless `subdir/` was explicitly added as a
search directory.

The revised approach implemented here first searches all of the
program's 'include' headers to resolve the given include path (e.g.
`subdir/file.thrift`) and then attempts to parse _that_ path relative
to one of the configured search directories.
  • Loading branch information
jparise authored Nov 19, 2021
1 parent 11e4a4b commit e3d3c25
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package thriftcheck

import (
"fmt"
"path/filepath"
"reflect"
"strings"

Expand Down Expand Up @@ -58,7 +59,22 @@ func Resolve(ref ast.TypeReference, program *ast.Program, dirs []string) (ast.No

if strings.Contains(name, ".") {
parts := strings.SplitN(name, ".", 2)
program, _, err := ParseFile(parts[0]+".thrift", dirs)
fname := parts[0] + ".thrift"

var ipath string
for _, header := range program.Headers {
if include, ok := header.(*ast.Include); ok {
if _, file := filepath.Split(include.Path); file == fname {
ipath = include.Path
break
}
}
}
if ipath == "" {
return nil, fmt.Errorf("missing \"include\" for type reference %q", ref.Name)
}

program, _, err := ParseFile(ipath, dirs)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e3d3c25

Please sign in to comment.