@@ -36,7 +36,35 @@ import (
3636)
3737
3838// sourceMode generates mocks via source file.
39- func sourceMode (source string ) (* model.Package , error ) {
39+ //
40+ // ifaces is a list of interface names to generate mocks for.
41+ // If nil or empty, all interfaces in the source file are used.
42+ func sourceMode (source string , ifaces []string ) (* model.Package , error ) {
43+ var wantIface func (name string ) bool
44+ if len (ifaces ) == 0 {
45+ wantIface = func (name string ) bool { return true }
46+ } else {
47+ wantIfaces := make (map [string ]struct {})
48+ for _ , n := range ifaces {
49+ wantIfaces [n ] = struct {}{}
50+ }
51+ wantIface = func (name string ) bool {
52+ _ , ok := wantIfaces [name ]
53+ return ok
54+ }
55+ }
56+
57+ if * excludeInterfaces != "" {
58+ oldWantIface := wantIface
59+ excludeNamesSet := parseExcludeInterfaces (* excludeInterfaces )
60+ wantIface = func (name string ) bool {
61+ if _ , ok := excludeNamesSet [name ]; ok {
62+ return false
63+ }
64+ return oldWantIface (name )
65+ }
66+ }
67+
4068 srcDir , err := filepath .Abs (filepath .Dir (source ))
4169 if err != nil {
4270 return nil , fmt .Errorf ("failed getting source directory: %v" , err )
@@ -59,6 +87,7 @@ func sourceMode(source string) (*model.Package, error) {
5987 importedInterfaces : newInterfaceCache (),
6088 auxInterfaces : newInterfaceCache (),
6189 srcDir : srcDir ,
90+ wantIface : wantIface ,
6291 }
6392
6493 // Handle -imports.
@@ -75,10 +104,6 @@ func sourceMode(source string) (*model.Package, error) {
75104 }
76105 }
77106
78- if * excludeInterfaces != "" {
79- p .excludeNamesSet = parseExcludeInterfaces (* excludeInterfaces )
80- }
81-
82107 // Handle -aux_files.
83108 if err := p .parseAuxFiles (* auxFiles ); err != nil {
84109 return nil , err
@@ -167,7 +192,7 @@ type fileParser struct {
167192 auxFiles []* ast.File
168193 auxInterfaces * interfaceCache
169194 srcDir string
170- excludeNamesSet map [ string ] struct {}
195+ wantIface func ( name string ) bool
171196}
172197
173198func (p * fileParser ) errorf (pos token.Pos , format string , args ... any ) error {
@@ -228,7 +253,7 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag
228253
229254 var is []* model.Interface
230255 for ni := range iterInterfaces (file ) {
231- if _ , ok := p . excludeNamesSet [ ni .name .String ()]; ok {
256+ if p . wantIface != nil && ! p . wantIface ( ni .name .String ()) {
232257 continue
233258 }
234259 i , err := p .parseInterface (ni .name .String (), importPath , ni )
0 commit comments