Skip to content

Commit e8ebf52

Browse files
committedOct 13, 2023
Refactor Config.ShouldGenerateInterface
PR #720
1 parent 5d48467 commit e8ebf52

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed
 

‎pkg/config/config.go

+36-30
Original file line numberDiff line numberDiff line change
@@ -263,46 +263,52 @@ func (c *Config) ExcludePath(path string) bool {
263263
func (c *Config) ShouldGenerateInterface(ctx context.Context, packageName, interfaceName string) (bool, error) {
264264
pkgConfig, err := c.GetPackageConfig(ctx, packageName)
265265
if err != nil {
266-
return false, err
266+
return false, fmt.Errorf("getting package config: %w", err)
267+
}
268+
269+
log := zerolog.Ctx(ctx)
270+
if pkgConfig.All {
271+
if pkgConfig.IncludeRegex != "" {
272+
log.Warn().Msg("interface config has both `all` and `include-regex` set: `include-regex` will be ignored")
273+
}
274+
if pkgConfig.ExcludeRegex != "" {
275+
log.Warn().Msg("interface config has both `all` and `exclude-regex` set: `exclude-regex` will be ignored")
276+
}
277+
return true, nil
267278
}
268279

269280
interfacesSection, err := c.getInterfacesSection(ctx, packageName)
270281
if err != nil {
271-
return false, err
282+
return false, fmt.Errorf("getting interfaces section: %w", err)
272283
}
273284
_, interfaceExists := interfacesSection[interfaceName]
274-
275-
matchedByRegex := false
276-
if pkgConfig.IncludeRegex != "" {
277-
if pkgConfig.All {
278-
log := zerolog.Ctx(ctx)
279-
log.Warn().Msg("interface config has both `all` and `include-regex` set: `include-regex` will be ignored")
280-
} else {
281-
matchedByRegex, err = regexp.MatchString(pkgConfig.IncludeRegex, interfaceName)
282-
if err != nil {
283-
return false, fmt.Errorf("evaluating `include-regex`: %w", err)
284-
}
285-
}
285+
if interfaceExists {
286+
return true, nil
286287
}
287-
excludedByRegex := false
288-
if pkgConfig.ExcludeRegex != "" {
289-
if pkgConfig.All {
290-
log := zerolog.Ctx(ctx)
291-
log.Warn().Msg("interface config has both `all` and `exclude-regex` set: `exclude-regex` will be ignored")
292-
} else if pkgConfig.IncludeRegex == "" {
293-
log := zerolog.Ctx(ctx)
288+
289+
includeRegex := pkgConfig.IncludeRegex
290+
excludeRegex := pkgConfig.ExcludeRegex
291+
if includeRegex == "" {
292+
if excludeRegex != "" {
294293
log.Warn().Msg("interface config has `exclude-regex` set but not `include-regex`: `exclude-regex` will be ignored")
295-
} else {
296-
excludedByRegex, err = regexp.MatchString(pkgConfig.ExcludeRegex, interfaceName)
297-
if err != nil {
298-
return false, fmt.Errorf("evaluating `exclude-regex`: %w", err)
299-
}
300-
if excludedByRegex {
301-
matchedByRegex = false
302-
}
303294
}
295+
return false, nil
296+
}
297+
includedByRegex, err := regexp.MatchString(includeRegex, interfaceName)
298+
if err != nil {
299+
return false, fmt.Errorf("evaluating `include-regex`: %w", err)
300+
}
301+
if !includedByRegex {
302+
return false, nil
303+
}
304+
if excludeRegex == "" {
305+
return true, nil
306+
}
307+
excludedByRegex, err := regexp.MatchString(excludeRegex, interfaceName)
308+
if err != nil {
309+
return false, fmt.Errorf("evaluating `exclude-regex`: %w", err)
304310
}
305-
return pkgConfig.All || interfaceExists || (matchedByRegex && !excludedByRegex), nil
311+
return !excludedByRegex, nil
306312
}
307313

308314
func (c *Config) getInterfacesSection(ctx context.Context, packageName string) (map[string]any, error) {

0 commit comments

Comments
 (0)
Please sign in to comment.