Skip to content

Commit

Permalink
feat: improve overrides configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jun 26, 2023
1 parent e888512 commit 8f7bd23
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 24 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/ettle/strcase v0.1.1
github.com/golangci/modinfo v0.3.1
github.com/hashicorp/go-immutable-radix/v2 v2.0.0
golang.org/x/tools v0.10.0
)
Expand Down
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/ettle/strcase v0.1.1 h1:htFueZyVeE1XNnMEfbqp5r67qAN/4r6ya1ysq8Q+Zcw=
github.com/ettle/strcase v0.1.1/go.mod h1:hzDLsPC7/lwKyBOywSHEP89nt2pDgdy+No1NBA9o9VY=
github.com/golangci/modinfo v0.3.1 h1:5Id0ESVcISuLTDscJIg5QcX3xOjdwPz4eO3aLHJh+NA=
github.com/golangci/modinfo v0.3.1/go.mod h1:AMKyptKcDrNIz9z/KJyED82pJAboTub2Vgt9vwxZhGE=
github.com/hashicorp/go-immutable-radix/v2 v2.0.0 h1:nq9lQ5I71Heg2lRb2/+szuIWKY3Y73d8YKyXyN91WzU=
github.com/hashicorp/go-immutable-radix/v2 v2.0.0/go.mod h1:hgdqLXA4f6NIjRVisM1TJ9aOJVNRqKZj+xDGF6m7PBw=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
Expand All @@ -10,8 +12,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.0/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyf
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
golang.org/x/exp v0.0.0-20221215174704-0915cd710c24 h1:6w3iSY8IIkp5OQtbYj8NeuKG1jS9d+kYaubXqsoOiQ8=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
Expand All @@ -21,5 +23,5 @@ golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
13 changes: 6 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ linters-settings:
# Overrides the default/root configuration.
# Default: []
overrides:
# The fully qualified package name. (uses `/` only)
# <module_name>/<package_name>
# The package name. (uses `/` only)
# Require
- pkg: example.com/foo/bar
# Default: false or the same as the default/root configuration.
Expand All @@ -169,7 +168,7 @@ linters-settings:
#### Examples
Overrides case rules for the package `example.com/foo/bar`:
Overrides case rules for the package `foo/bar`:

```yaml
linters-settings:
Expand All @@ -186,7 +185,7 @@ linters-settings:
xml: pascal
```

Ignore fields inside the package `example.com/foo/bar`:
Ignore fields inside the package `foo/bar`:

```yaml
linters-settings:
Expand All @@ -197,13 +196,13 @@ linters-settings:
yaml: camel
xml: camel
overrides:
- pkg: example.com/foo/bar
- pkg: foo/bar
ignored-fields:
- Bar
- Foo
```

Ignore the package `example.com/foo/bar`:
Ignore the package `foo/bar`:

```yaml
linters-settings:
Expand All @@ -214,7 +213,7 @@ linters-settings:
yaml: camel
xml: camel
overrides:
- pkg: example.com/foo/bar
- pkg: foo/bar
ignore: true
```

Expand Down
15 changes: 11 additions & 4 deletions tagliatelle.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/ettle/strcase"
"github.com/golangci/modinfo"
iradix "github.com/hashicorp/go-immutable-radix/v2"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
Expand Down Expand Up @@ -44,19 +45,25 @@ func New(config Config) *analysis.Analyzer {
Name: "tagliatelle",
Doc: "Checks the struct tags.",
Run: func(pass *analysis.Pass) (interface{}, error) {
if len(config.Rules) == 0 {
if len(config.Rules) == 0 && len(config.Overrides) == 0 {
return nil, nil
}

return run(pass, config)
},
Requires: []*analysis.Analyzer{
inspect.Analyzer,
modinfo.Analyzer,
},
}
}

func run(pass *analysis.Pass, config Config) (interface{}, error) {
info, err := modinfo.FindModuleFromPass(pass)
if err != nil {
return nil, err
}

isp, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("missing inspect analyser")
Expand All @@ -66,7 +73,7 @@ func run(pass *analysis.Pass, config Config) (interface{}, error) {
(*ast.StructType)(nil),
}

r := createRadixTree(config)
r := createRadixTree(config, info)

isp.Preorder(nodeFilter, func(n ast.Node) {
node, ok := n.(*ast.StructType)
Expand Down Expand Up @@ -252,7 +259,7 @@ func toHeader(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '-')
}

func createRadixTree(config Config) *iradix.Tree[Base] {
func createRadixTree(config Config, info modinfo.ModInfo) *iradix.Tree[Base] {
r := iradix.New[Base]()

defaultRule := Base{
Expand All @@ -277,7 +284,7 @@ func createRadixTree(config Config) *iradix.Tree[Base] {
c.Rules[k] = v
}

r, _, _ = r.Insert([]byte(override.Package), c)
r, _, _ = r.Insert([]byte(path.Join(info.Path, override.Package)), c)
}

return r
Expand Down
22 changes: 12 additions & 10 deletions tagliatelle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

func TestAnalyzer(t *testing.T) {
t.Setenv("MODINFO_DEBUG_DISABLE_ONCE", "true")

testCases := []struct {
desc string
dir string
Expand All @@ -20,8 +22,8 @@ func TestAnalyzer(t *testing.T) {
}{
{
desc: "simple",
dir: "one",
patterns: []string{"one"},
dir: "example.com/one",
patterns: []string{"example.com/one"},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand All @@ -41,8 +43,8 @@ func TestAnalyzer(t *testing.T) {
},
{
desc: "with non-applicable overrides",
dir: "one",
patterns: []string{"one/..."},
dir: "example.com/one",
patterns: []string{"example.com/one/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand Down Expand Up @@ -74,8 +76,8 @@ func TestAnalyzer(t *testing.T) {
},
{
desc: "with applicable overrides",
dir: "two",
patterns: []string{"two/..."},
dir: "example.com/two",
patterns: []string{"example.com/two/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand All @@ -93,7 +95,7 @@ func TestAnalyzer(t *testing.T) {
},
Overrides: []tagliatelle.Overrides{
{
Package: "two/b",
Package: "b",
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "upperSnake",
Expand All @@ -107,8 +109,8 @@ func TestAnalyzer(t *testing.T) {
},
{
desc: "ignore",
dir: "three",
patterns: []string{"three/..."},
dir: "example.com/three",
patterns: []string{"example.com/three/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand All @@ -126,7 +128,7 @@ func TestAnalyzer(t *testing.T) {
},
Overrides: []tagliatelle.Overrides{
{
Package: "three/b",
Package: "b",
Base: tagliatelle.Base{
Ignore: true,
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8f7bd23

Please sign in to comment.