Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Aug 2, 2024
1 parent e9756ca commit 3b63c20
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *GeneratorSuite) getInterfaceFromFile(interfacePath, interfaceName strin
)

s.Require().NoError(
s.parser.Load(),
s.parser.Load(context.Background()),
)

iface, err := s.parser.Find(interfaceName)
Expand Down
2 changes: 1 addition & 1 deletion pkg/outputter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ packages:
m.config.Config = confPath.String()

require.NoError(t, parser.ParsePackages(ctx, []string{tt.packagePath}))
require.NoError(t, parser.Load())
require.NoError(t, parser.Load(context.Background()))
for _, intf := range parser.Interfaces() {
t.Logf("generating interface: %s %s", intf.QualifiedName, intf.Name)
require.NoError(t, m.Generate(ctx, intf))
Expand Down
30 changes: 21 additions & 9 deletions pkg/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type fileEntry struct {
interfaces []string
}

func (f *fileEntry) ParseInterfaces() {
nv := NewNodeVisitor()
func (f *fileEntry) ParseInterfaces(ctx context.Context) {
nv := NewNodeVisitor(ctx)
ast.Walk(nv, f.syntax)
f.interfaces = nv.DeclaredInterfaces()
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func (p *Parser) ParsePackages(ctx context.Context, packageNames []string) error
pkg: pkg,
syntax: pkg.Syntax[fileIdx],
}
entry.ParseInterfaces()
entry.ParseInterfaces(ctx)
p.files = append(p.files, &entry)
p.entriesByFileName[file] = &entry
}
Expand Down Expand Up @@ -186,9 +186,9 @@ func (p *Parser) Parse(ctx context.Context, path string) error {
return nil
}

func (p *Parser) Load() error {
func (p *Parser) Load(ctx context.Context) error {
for _, entry := range p.files {
entry.ParseInterfaces()
entry.ParseInterfaces(ctx)
}
return nil
}
Expand Down Expand Up @@ -321,12 +321,15 @@ func (s sortableIFaceList) Less(i, j int) bool {
}

type NodeVisitor struct {
declaredInterfaces []string
declaredInterfaces []string
genericInstantiationInterface map[string]any
ctx context.Context
}

func NewNodeVisitor() *NodeVisitor {
func NewNodeVisitor(ctx context.Context) *NodeVisitor {
return &NodeVisitor{
declaredInterfaces: make([]string, 0),
ctx: ctx,
}
}

Expand All @@ -335,14 +338,23 @@ func (nv *NodeVisitor) DeclaredInterfaces() []string {
}

func (nv *NodeVisitor) Visit(node ast.Node) ast.Visitor {
log := zerolog.Ctx(nv.ctx)

switch n := node.(type) {
case *ast.TypeSpec:
log := log.With().
Str("node-name", n.Name.Name).
Str("node-type", fmt.Sprintf("%T", n.Type)).
Logger()

switch n.Type.(type) {
case *ast.InterfaceType, *ast.FuncType, *ast.IndexExpr:
log.Debug().
Str("node-type", fmt.Sprintf("%T", n.Type)).
Msg("found node with acceptable type for mocking")
nv.declaredInterfaces = append(nv.declaredInterfaces, n.Name.Name)
default:
fmt.Print("PRINTING TYPE\n")
fmt.Printf("%T\n", n.Type)
log.Debug().Msg("Found node with unacceptable type for mocking. Rejecting.")
}
}
return nv
Expand Down
8 changes: 4 additions & 4 deletions pkg/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestFileParse(t *testing.T) {
err := parser.Parse(ctx, testFile)
assert.NoError(t, err)

err = parser.Load()
err = parser.Load(context.Background())
assert.NoError(t, err)

node, err := parser.Find("Requester")
Expand All @@ -38,7 +38,7 @@ func TestBuildTagInFilename(t *testing.T) {
err = parser.Parse(ctx, getFixturePath("buildtag", "filename", "iface_freebsd.go"))
assert.NoError(t, err)

err = parser.Load()
err = parser.Load(context.Background())
assert.NoError(t, err) // Expect "redeclared in this block" if tags aren't respected

nodes := parser.Interfaces()
Expand All @@ -60,7 +60,7 @@ func TestBuildTagInComment(t *testing.T) {
err = parser.Parse(ctx, getFixturePath("buildtag", "comment", "freebsd_iface.go"))
assert.NoError(t, err)

err = parser.Load()
err = parser.Load(context.Background())
assert.NoError(t, err) // Expect "redeclared in this block" if tags aren't respected

nodes := parser.Interfaces()
Expand All @@ -78,7 +78,7 @@ func TestCustomBuildTag(t *testing.T) {
err = parser.Parse(ctx, getFixturePath("buildtag", "comment", "custom2_iface.go"))
assert.NoError(t, err)

err = parser.Load()
err = parser.Load(context.Background())
assert.NoError(t, err) // Expect "redeclared in this block" if tags aren't respected

found := false
Expand Down
2 changes: 1 addition & 1 deletion pkg/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (w *Walker) Walk(ctx context.Context, visitor WalkerVisitor) (generated boo
parser := NewParser(w.BuildTags)
w.doWalk(ctx, parser, w.BaseDir)

err := parser.Load()
err := parser.Load(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error walking: %v\n", err)
os.Exit(1)
Expand Down

0 comments on commit 3b63c20

Please sign in to comment.