Skip to content

Commit c3f8934

Browse files
committed
feature: impl_interfaces 参数支持正则传参, 默认忽略所有接口
1 parent eac3d23 commit c3f8934

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

generator.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path"
1111
"path/filepath"
12+
"regexp"
1213
"sort"
1314
"strconv"
1415
"strings"
@@ -23,8 +24,9 @@ type generator struct {
2324
indent string
2425
mockNames map[string]string // may be empty
2526
mockInterfaces map[string]bool // may be empty
26-
filename string // may be empty
27-
srcPackage, srcInterfaces string // may be empty
27+
mockInterfaceRegexps []*regexp.Regexp
28+
filename string // may be empty
29+
srcPackage, srcInterfaces string // may be empty
2830
copyrightHeader string
2931

3032
packageMap map[string]string // map from import path to package name
@@ -164,8 +166,18 @@ func (g *generator) generate(pkg *model.Package, dstPkg *model.Package, outputPk
164166
}
165167

166168
for _, intf := range pkg.Interfaces {
167-
if len(g.mockInterfaces) != 0 && !g.mockInterfaces[intf.Name] {
168-
continue
169+
if !g.mockInterfaces[intf.Name] {
170+
match := false
171+
for _, v := range g.mockInterfaceRegexps {
172+
if v.Match([]byte(intf.Name)) {
173+
match = true
174+
break
175+
}
176+
}
177+
178+
if !match {
179+
continue
180+
}
169181
}
170182

171183
sn, exist := namesMap[g.mockName(intf.Name)]

mockgen.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"os"
3131
"os/exec"
3232
"path/filepath"
33+
"regexp"
3334
"strings"
3435
"unicode"
3536

@@ -128,8 +129,9 @@ func main() {
128129
}
129130

130131
g := &generator{
131-
mockNames: make(map[string]string),
132-
mockInterfaces: make(map[string]bool),
132+
mockNames: make(map[string]string),
133+
mockInterfaces: make(map[string]bool),
134+
mockInterfaceRegexps: make([]*regexp.Regexp, 0),
133135
}
134136
if *destination != "" {
135137
g.dstFileName = *destination
@@ -147,7 +149,16 @@ func main() {
147149
if *implInterfaces != "" {
148150
for _, v := range strings.Split(*implInterfaces, ",") {
149151
v := strings.TrimSpace(v)
150-
g.mockInterfaces[v] = true
152+
153+
if v[0] == '^' {
154+
exp, err := regexp.Compile(v)
155+
if err != nil {
156+
log.Fatalf("Failed reading impl_interfaces arguments: %v", err)
157+
}
158+
g.mockInterfaceRegexps = append(g.mockInterfaceRegexps, exp)
159+
} else {
160+
g.mockInterfaces[v] = true
161+
}
151162
}
152163
}
153164
if *copyrightFile != "" {

0 commit comments

Comments
 (0)