Skip to content

Commit 5e077dd

Browse files
authored
feat: allow to override configuration by package (#21)
1 parent 9fa7b3f commit 5e077dd

File tree

21 files changed

+889
-41
lines changed

21 files changed

+889
-41
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ linters-settings:
3030
disable:
3131
- fieldalignment
3232
gocyclo:
33-
min-complexity: 15
33+
min-complexity: 20
3434
goconst:
3535
min-len: 5
3636
min-occurrences: 3

cmd/tagliatelle/tagliatelle.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ import (
77

88
func main() {
99
cfg := tagliatelle.Config{
10-
Rules: map[string]string{
11-
"json": "camel",
12-
"yaml": "camel",
13-
"xml": "camel",
14-
"bson": "camel",
15-
"avro": "snake",
16-
"header": "header",
17-
"envconfig": "upperSnake",
18-
"env": "upperSnake",
10+
Base: tagliatelle.Base{
11+
Rules: map[string]string{
12+
"json": "camel",
13+
"yaml": "camel",
14+
"xml": "camel",
15+
"bson": "camel",
16+
"avro": "snake",
17+
"header": "header",
18+
"envconfig": "upperSnake",
19+
"env": "upperSnake",
20+
},
21+
UseFieldName: true,
1922
},
20-
UseFieldName: true,
2123
}
2224

2325
singlechecker.Main(tagliatelle.New(cfg))

copygo122_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build go1.22 && !go1.23
2+
3+
// Copyright 2016 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package tagliatelle_test
8+
9+
import (
10+
"io"
11+
"io/fs"
12+
"os"
13+
"path/filepath"
14+
)
15+
16+
// CopyFS IMPORTANT:
17+
// This is a modified copy of os.CopyFS.
18+
// os.CopyFS is available in go1.23.
19+
// TODO(ldez) remove this file when bump to go1.23.
20+
//
21+
//nolint:all // code from Go.
22+
func CopyFS(dir string, fsys fs.FS) error {
23+
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
24+
if err != nil {
25+
return err
26+
}
27+
28+
fpath := path
29+
30+
newPath := filepath.Join(dir, fpath)
31+
if d.IsDir() {
32+
return os.MkdirAll(newPath, 0777)
33+
}
34+
35+
// TODO(panjf2000): handle symlinks with the help of fs.ReadLinkFS
36+
// once https://go.dev/issue/49580 is done.
37+
// we also need filepathlite.IsLocal from https://go.dev/cl/564295.
38+
if !d.Type().IsRegular() {
39+
return &os.PathError{Op: "CopyFS", Path: path, Err: os.ErrInvalid}
40+
}
41+
42+
r, err := fsys.Open(path)
43+
if err != nil {
44+
return err
45+
}
46+
defer r.Close()
47+
info, err := r.Stat()
48+
if err != nil {
49+
return err
50+
}
51+
w, err := os.OpenFile(newPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0666|info.Mode()&0777)
52+
if err != nil {
53+
return err
54+
}
55+
56+
if _, err := io.Copy(w, r); err != nil {
57+
w.Close()
58+
return &os.PathError{Op: "Copy", Path: newPath, Err: err}
59+
}
60+
return w.Close()
61+
})
62+
}

copygo123_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build go1.23
2+
3+
package tagliatelle_test
4+
5+
import (
6+
"io/fs"
7+
"os"
8+
)
9+
10+
// CopyFS temporary workaround.
11+
// TODO(ldez) remove this file when bump to go1.23.
12+
func CopyFS(dir string, fsys fs.FS) error {
13+
return os.CopyFS(dir, fsys)
14+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ go 1.22.0
44

55
require (
66
github.com/ettle/strcase v0.2.0
7+
github.com/hashicorp/go-immutable-radix/v2 v2.1.0
78
golang.org/x/tools v0.27.0
89
)
910

1011
require (
12+
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
1113
golang.org/x/mod v0.22.0 // indirect
1214
golang.org/x/sync v0.9.0 // indirect
1315
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ github.com/ettle/strcase v0.2.0 h1:fGNiVF21fHXpX1niBgk0aROov1LagYsOwV/xqKDKR/Q=
22
github.com/ettle/strcase v0.2.0/go.mod h1:DajmHElDSaX76ITe3/VHVyMin4LWSJN5Z909Wp+ED1A=
33
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
44
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5+
github.com/hashicorp/go-immutable-radix/v2 v2.1.0 h1:CUW5RYIcysz+D3B+l1mDeXrQ7fUvGGCwJfdASSzbrfo=
6+
github.com/hashicorp/go-immutable-radix/v2 v2.1.0/go.mod h1:hgdqLXA4f6NIjRVisM1TJ9aOJVNRqKZj+xDGF6m7PBw=
7+
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
8+
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
9+
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
10+
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
11+
golang.org/x/exp v0.0.0-20221215174704-0915cd710c24 h1:6w3iSY8IIkp5OQtbYj8NeuKG1jS9d+kYaubXqsoOiQ8=
12+
golang.org/x/exp v0.0.0-20221215174704-0915cd710c24/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
513
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
614
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
715
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=

readme.md

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,24 @@ Define the rules, you want via your [golangci-lint](https://golangci-lint.run) c
114114
```yaml
115115
linters-settings:
116116
tagliatelle:
117-
# Check the struck tag name case.
117+
# Check the struct tag name case.
118118
case:
119-
# Use the struct field name to check the name of the struct tag.
120-
# Default: false
121-
use-field-name: true
119+
# Define the association between tag name and case.
120+
# Any struct tag name can be used.
121+
# Support string cases:
122+
# - `camel`
123+
# - `pascal`
124+
# - `kebab`
125+
# - `snake`
126+
# - `upperSnake`
127+
# - `goCamel`
128+
# - `goPascal`
129+
# - `goKebab`
130+
# - `goSnake`
131+
# - `upper`
132+
# - `lower`
133+
# - `header`
122134
rules:
123-
# Any struct tag type can be used.
124-
# Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header`.
125135
json: camel
126136
yaml: camel
127137
xml: camel
@@ -131,6 +141,87 @@ linters-settings:
131141
mapstructure: kebab
132142
env: upperSnake
133143
envconfig: upperSnake
144+
whatever: snake
145+
# Use the struct field name to check the name of the struct tag.
146+
# Default: false
147+
use-field-name: true
148+
# The field names to ignore.
149+
# Default: []
150+
ignored-fields:
151+
- Bar
152+
- Foo
153+
# Overrides the default/root configuration.
154+
# Default: []
155+
overrides:
156+
-
157+
# The package path. (uses `/` only as separator)
158+
# Required
159+
pkg: foo/bar
160+
# Default: empty or the same as the default/root configuration.
161+
rules:
162+
json: snake
163+
xml: pascal
164+
# Default: false or the same as the default/root configuration.
165+
use-field-name: true
166+
# The field names to ignore.
167+
# Default: [] or the same as the default/root configuration.
168+
ignored-fields:
169+
- Bar
170+
- Foo
171+
# Ignore the package (it takes the precedence over all other configuration).
172+
# Default: false
173+
ignore: true
174+
```
175+
176+
#### Examples
177+
178+
Overrides case rules for the package `foo/bar`:
179+
180+
```yaml
181+
linters-settings:
182+
tagliatelle:
183+
case:
184+
rules:
185+
json: camel
186+
yaml: camel
187+
xml: camel
188+
overrides:
189+
- pkg: foo/bar
190+
rules:
191+
json: snake
192+
xml: pascal
193+
```
194+
195+
Ignore fields inside the package `foo/bar`:
196+
197+
```yaml
198+
linters-settings:
199+
tagliatelle:
200+
case:
201+
rules:
202+
json: camel
203+
yaml: camel
204+
xml: camel
205+
overrides:
206+
- pkg: foo/bar
207+
ignored-fields:
208+
- Bar
209+
- Foo
210+
```
211+
212+
Ignore the package `foo/bar`:
213+
214+
```yaml
215+
linters-settings:
216+
tagliatelle:
217+
case:
218+
rules:
219+
json: camel
220+
yaml: camel
221+
xml: camel
222+
overrides:
223+
- pkg: foo/bar
224+
ignore: true
134225
```
135226

136227
More information here https://golangci-lint.run/usage/linters/#tagliatelle
@@ -171,9 +262,6 @@ linters-settings:
171262
tagliatelle:
172263
# Check the struck tag name case.
173264
case:
174-
# Use the struct field name to check the name of the struct tag.
175-
# Default: false
176-
use-field-name: true
177265
rules:
178266
# Any struct tag type can be used.
179267
# Support string case: `camel`, `pascal`, `kebab`, `snake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`
@@ -182,4 +270,7 @@ linters-settings:
182270
xml: camel
183271
toml: camel
184272
whatever: kebab
273+
# Use the struct field name to check the name of the struct tag.
274+
# Default: false
275+
use-field-name: true
185276
```

0 commit comments

Comments
 (0)