Skip to content

Commit

Permalink
Finish refactoring to use helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Aug 29, 2024
1 parent c23bbe1 commit 4ed77bd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
13 changes: 3 additions & 10 deletions tools/analyzers/protomarshalcheck/protomarshalcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ import (
"regexp"
"strings"

"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}

func Analyzer() *analysis.Analyzer {
flagSet := flag.NewFlagSet("protomarshalcheck", flag.ExitOnError)
skipPkg := flagSet.String("skip-pkg", "", "package(s) to skip for linting")
Expand All @@ -31,7 +24,7 @@ func Analyzer() *analysis.Analyzer {
Run: func(pass *analysis.Pass) (any, error) {
// Check for a skipped package.
if len(*skipPkg) > 0 {
skipped := sliceMap(strings.Split(*skipPkg, ","), strings.TrimSpace)
skipped := lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
Expand All @@ -42,7 +35,7 @@ func Analyzer() *analysis.Analyzer {
// Check for a skipped file.
skipFilePatterns := make([]string, 0)
if len(*skipFiles) > 0 {
skipFilePatterns = sliceMap(strings.Split(*skipFiles, ","), strings.TrimSpace)
skipFilePatterns = lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
}
for _, pattern := range skipFilePatterns {
_, err := regexp.Compile(pattern)
Expand Down
15 changes: 3 additions & 12 deletions tools/analyzers/zerologmarshalcheck/zerologmarshalcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ import (
"slices"
"strings"

"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}

func Analyzer() *analysis.Analyzer {
flagSet := flag.NewFlagSet("zerologmarshalcheck", flag.ExitOnError)
skipPkg := flagSet.String("skip-pkg", "", "package(s) to skip for linting")
Expand All @@ -35,7 +28,7 @@ func Analyzer() *analysis.Analyzer {

// Check for a skipped package.
if len(*skipPkg) > 0 {
skipped := sliceMap(strings.Split(*skipPkg, ","), strings.TrimSpace)
skipped := lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
Expand All @@ -46,7 +39,7 @@ func Analyzer() *analysis.Analyzer {
// Check for a skipped file.
skipFilePatterns := make([]string, 0)
if len(*skipFiles) > 0 {
skipFilePatterns = sliceMap(strings.Split(*skipFiles, ","), strings.TrimSpace)
skipFilePatterns = lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
}
for _, pattern := range skipFilePatterns {
_, err := regexp.Compile(pattern)
Expand Down Expand Up @@ -165,8 +158,6 @@ func Analyzer() *analysis.Analyzer {
default:
return true
}

return false
})

return nil, nil
Expand Down

0 comments on commit 4ed77bd

Please sign in to comment.