workgroups/vendor/github.com/tommy-muehle/go-mnd/v2/config/config.go
Marvin Preuss 1d4ae27878
All checks were successful
continuous-integration/drone/push Build is passing
ci: drone yaml with reusable anchors
2021-09-24 17:34:17 +02:00

119 lines
2.2 KiB
Go

package config
import (
"regexp"
"strings"
)
type Config struct {
Checks map[string]bool
IgnoredNumbers map[string]struct{}
IgnoredFunctions []*regexp.Regexp
IgnoredFiles []*regexp.Regexp
}
type Option func(config *Config)
func DefaultConfig() *Config {
return &Config{
Checks: map[string]bool{},
IgnoredNumbers: map[string]struct{}{
"0": {},
"0.0": {},
"1": {},
"1.0": {},
},
IgnoredFiles: []*regexp.Regexp{
regexp.MustCompile(`_test.go`),
},
IgnoredFunctions: []*regexp.Regexp{
regexp.MustCompile(`time.Date`),
},
}
}
func WithOptions(options ...Option) *Config {
c := DefaultConfig()
for _, option := range options {
option(c)
}
return c
}
func WithIgnoredFunctions(excludes string) Option {
return func(config *Config) {
if excludes == "" {
return
}
for _, exclude := range strings.Split(excludes, ",") {
config.IgnoredFunctions = append(config.IgnoredFunctions, regexp.MustCompile(exclude))
}
}
}
func WithIgnoredFiles(excludes string) Option {
return func(config *Config) {
if excludes == "" {
return
}
for _, exclude := range strings.Split(excludes, ",") {
config.IgnoredFiles = append(config.IgnoredFiles, regexp.MustCompile(exclude))
}
}
}
func WithIgnoredNumbers(numbers string) Option {
return func(config *Config) {
if numbers == "" {
return
}
for _, number := range strings.Split(numbers, ",") {
config.IgnoredNumbers[config.removeDigitSeparator(number)] = struct{}{}
}
}
}
func WithCustomChecks(checks string) Option {
return func(config *Config) {
if checks == "" {
return
}
for name, _ := range config.Checks {
config.Checks[name] = false
}
for _, name := range strings.Split(checks, ",") {
config.Checks[name] = true
}
}
}
func (c *Config) IsCheckEnabled(name string) bool {
return c.Checks[name]
}
func (c *Config) IsIgnoredNumber(number string) bool {
_, ok := c.IgnoredNumbers[c.removeDigitSeparator(number)]
return ok
}
func (c *Config) IsIgnoredFunction(f string) bool {
for _, pattern := range c.IgnoredFunctions {
if pattern.MatchString(f) {
return true
}
}
return false
}
func (c *Config) removeDigitSeparator(number string) string {
return strings.Replace(number, "_", "", -1)
}