Skip to content

Commit

Permalink
added ignoring interfaces via -exclude flag (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
tulzke committed Sep 7, 2023
1 parent 665220d commit 78bb101
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mockgen/internal/tests/exclude/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package exclude

//go:generate mockgen -source=interfaces.go -destination=mock.go -package=ignore -exclude=IgnoreMe,IgnoreMe2

type IgnoreMe interface {
A() bool
}

type IgnoreMe2 interface {
~int
}

type GenerateMockForMe interface {
B() int
}
52 changes: 52 additions & 0 deletions mockgen/internal/tests/exclude/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
exclude = flag.String("exclude", "", "Comma-separated names of interfaces to be excluded")

debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
showVersion = flag.Bool("version", false, "Print version.")
Expand Down Expand Up @@ -200,6 +201,20 @@ func parseMockNames(names string) map[string]string {
return mocksMap
}

func parseExclude(names string) map[string]struct{} {
splitNames := strings.Split(names, ",")
if len(splitNames) == 0 {
return nil
}

namesSet := make(map[string]struct{}, len(splitNames))
for _, name := range splitNames {
namesSet[name] = struct{}{}
}

return namesSet
}

func usage() {
_, _ = io.WriteString(os.Stderr, usageText)
flag.PrintDefaults()
Expand Down
8 changes: 8 additions & 0 deletions mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func sourceMode(source string) (*model.Package, error) {
}
}

if *exclude != "" {
p.excludeNamesSet = parseExclude(*exclude)
}

// Handle -aux_files.
if err := p.parseAuxFiles(*auxFiles); err != nil {
return nil, err
Expand Down Expand Up @@ -163,6 +167,7 @@ type fileParser struct {
auxFiles []*ast.File
auxInterfaces *interfaceCache
srcDir string
excludeNamesSet map[string]struct{}
}

func (p *fileParser) errorf(pos token.Pos, format string, args ...any) error {
Expand Down Expand Up @@ -223,6 +228,9 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag

var is []*model.Interface
for ni := range iterInterfaces(file) {
if _, ok := p.excludeNamesSet[ni.name.String()]; ok {
continue
}
i, err := p.parseInterface(ni.name.String(), importPath, ni)
if err != nil {
return nil, err
Expand Down

0 comments on commit 78bb101

Please sign in to comment.