Skip to content

Commit

Permalink
Refactor Config.ShouldGenerateInterface
Browse files Browse the repository at this point in the history
PR #720
  • Loading branch information
kbolino committed Oct 13, 2023
1 parent 5d48467 commit e8ebf52
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,46 +263,52 @@ func (c *Config) ExcludePath(path string) bool {
func (c *Config) ShouldGenerateInterface(ctx context.Context, packageName, interfaceName string) (bool, error) {
pkgConfig, err := c.GetPackageConfig(ctx, packageName)
if err != nil {
return false, err
return false, fmt.Errorf("getting package config: %w", err)
}

log := zerolog.Ctx(ctx)
if pkgConfig.All {
if pkgConfig.IncludeRegex != "" {
log.Warn().Msg("interface config has both `all` and `include-regex` set: `include-regex` will be ignored")
}
if pkgConfig.ExcludeRegex != "" {
log.Warn().Msg("interface config has both `all` and `exclude-regex` set: `exclude-regex` will be ignored")
}
return true, nil
}

interfacesSection, err := c.getInterfacesSection(ctx, packageName)
if err != nil {
return false, err
return false, fmt.Errorf("getting interfaces section: %w", err)
}
_, interfaceExists := interfacesSection[interfaceName]

matchedByRegex := false
if pkgConfig.IncludeRegex != "" {
if pkgConfig.All {
log := zerolog.Ctx(ctx)
log.Warn().Msg("interface config has both `all` and `include-regex` set: `include-regex` will be ignored")
} else {
matchedByRegex, err = regexp.MatchString(pkgConfig.IncludeRegex, interfaceName)
if err != nil {
return false, fmt.Errorf("evaluating `include-regex`: %w", err)
}
}
if interfaceExists {
return true, nil
}
excludedByRegex := false
if pkgConfig.ExcludeRegex != "" {
if pkgConfig.All {
log := zerolog.Ctx(ctx)
log.Warn().Msg("interface config has both `all` and `exclude-regex` set: `exclude-regex` will be ignored")
} else if pkgConfig.IncludeRegex == "" {
log := zerolog.Ctx(ctx)

includeRegex := pkgConfig.IncludeRegex
excludeRegex := pkgConfig.ExcludeRegex
if includeRegex == "" {
if excludeRegex != "" {
log.Warn().Msg("interface config has `exclude-regex` set but not `include-regex`: `exclude-regex` will be ignored")
} else {
excludedByRegex, err = regexp.MatchString(pkgConfig.ExcludeRegex, interfaceName)
if err != nil {
return false, fmt.Errorf("evaluating `exclude-regex`: %w", err)
}
if excludedByRegex {
matchedByRegex = false
}
}
return false, nil
}
includedByRegex, err := regexp.MatchString(includeRegex, interfaceName)
if err != nil {
return false, fmt.Errorf("evaluating `include-regex`: %w", err)
}
if !includedByRegex {
return false, nil
}
if excludeRegex == "" {
return true, nil
}
excludedByRegex, err := regexp.MatchString(excludeRegex, interfaceName)
if err != nil {
return false, fmt.Errorf("evaluating `exclude-regex`: %w", err)
}
return pkgConfig.All || interfaceExists || (matchedByRegex && !excludedByRegex), nil
return !excludedByRegex, nil
}

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

0 comments on commit e8ebf52

Please sign in to comment.