Skip to content

Commit 93df6f7

Browse files
authored
Add tagliatelle linter (#1906)
1 parent 0f3f9ef commit 93df6f7

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed

.golangci.example.yml

+14
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,20 @@ linters-settings:
478478
retract-allow-no-explanation: false
479479
# Forbid the use of the `exclude` directives. Default is false.
480480
exclude-forbidden: false
481+
tagliatelle:
482+
# check the struck tag name case
483+
case:
484+
# use the struct field name to check the name of the struct tag
485+
use-field-name: true
486+
rules:
487+
# any struct tag type can be used.
488+
# support string case: `camel`, `pascal`, `kebab`, `snake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`
489+
json: camel
490+
yaml: camel
491+
xml: camel
492+
bson: camel
493+
avro: snake
494+
mapstructure: kebab
481495

482496
# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
483497
# for more info.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
github.com/kunwardeep/paralleltest v1.0.2
4444
github.com/kyoh86/exportloopref v0.1.8
4545
github.com/ldez/gomoddirectives v0.2.1
46+
github.com/ldez/tagliatelle v0.2.0
4647
github.com/maratori/testpackage v1.0.1
4748
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0
4849
github.com/mattn/go-colorable v0.1.8

go.sum

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ type LintersSettings struct {
278278
ImportAs ImportAsSettings
279279
GoModDirectives GoModDirectivesSettings
280280
Promlinter PromlinterSettings
281+
Tagliatelle TagliatelleSettings
281282

282283
Custom map[string]CustomLinterSettings
283284
}
@@ -478,6 +479,13 @@ type GoModDirectivesSettings struct {
478479
RetractAllowNoExplanation bool `mapstructure:"retract-allow-no-explanation"`
479480
}
480481

482+
type TagliatelleSettings struct {
483+
Case struct {
484+
Rules map[string]string
485+
UseFieldName bool `mapstructure:"use-field-name"`
486+
}
487+
}
488+
481489
var defaultLintersSettings = LintersSettings{
482490
Lll: LllSettings{
483491
LineLength: 120,

pkg/golinters/tagliatelle.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package golinters
2+
3+
import (
4+
"github.com/ldez/tagliatelle"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
11+
func NewTagliatelle(settings *config.TagliatelleSettings) *goanalysis.Linter {
12+
cfg := tagliatelle.Config{
13+
Rules: map[string]string{
14+
"json": "camel",
15+
"yaml": "camel",
16+
},
17+
}
18+
19+
if settings != nil {
20+
for k, v := range settings.Case.Rules {
21+
cfg.Rules[k] = v
22+
}
23+
cfg.UseFieldName = settings.Case.UseFieldName
24+
}
25+
26+
a := tagliatelle.New(cfg)
27+
28+
return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, nil).
29+
WithLoadMode(goanalysis.LoadModeSyntax)
30+
}

pkg/lint/lintersdb/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
112112
var cyclopCfg *config.Cyclop
113113
var importAsCfg *config.ImportAsSettings
114114
var goModDirectivesCfg *config.GoModDirectivesSettings
115+
var tagliatelleCfg *config.TagliatelleSettings
115116

116117
if m.cfg != nil {
117118
govetCfg = &m.cfg.LintersSettings.Govet
@@ -126,6 +127,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
126127
cyclopCfg = &m.cfg.LintersSettings.Cyclop
127128
importAsCfg = &m.cfg.LintersSettings.ImportAs
128129
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
130+
tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle
129131
}
130132

131133
const megacheckName = "megacheck"
@@ -483,6 +485,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
483485
WithSince("v1.40.0").
484486
WithPresets(linter.PresetStyle).
485487
WithURL("https://github.com/yeya24/promlinter"),
488+
linter.NewConfig(golinters.NewTagliatelle(tagliatelleCfg)).
489+
WithSince("v1.40.0").
490+
WithPresets(linter.PresetStyle).
491+
WithURL("https://github.com/ldez/tagliatelle"),
492+
486493
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
487494
linter.NewConfig(golinters.NewNoLintLint()).
488495
WithSince("v1.26.0").

test/testdata/tagliatelle.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//args: -Etagliatelle
2+
package testdata
3+
4+
import "time"
5+
6+
type TglFoo struct {
7+
ID string `json:"ID"` // ERROR `json\(camel\): got 'ID' want 'id'`
8+
UserID string `json:"UserID"` // ERROR `json\(camel\): got 'UserID' want 'userId'`
9+
Name string `json:"name"`
10+
Value time.Duration `json:"value,omitempty"`
11+
Bar TglBar `json:"bar"`
12+
Bur `json:"bur"`
13+
}
14+
15+
type TglBar struct {
16+
Name string `json:"-"`
17+
Value string `json:"value"`
18+
CommonServiceFooItem *TglBir `json:"CommonServiceItem,omitempty"` // ERROR `json\(camel\): got 'CommonServiceItem' want 'commonServiceItem'`
19+
}
20+
21+
type TglBir struct {
22+
Name string `json:"-"`
23+
Value string `json:"value"`
24+
ReplaceAllowList []string `mapstructure:"replace-allow-list"`
25+
}
26+
27+
type Bur struct {
28+
Name string
29+
Value string `yaml:"Value"` // ERROR `yaml\(camel\): got 'Value' want 'value'`
30+
More string `json:"-"`
31+
Also string `json:",omitempty"`
32+
ReqPerS string `avro:"req_per_s"`
33+
}

0 commit comments

Comments
 (0)