-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tagliatelle.go
218 lines (184 loc) · 4.36 KB
/
tagliatelle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Package tagliatelle a linter that handle struct tags.
package tagliatelle
import (
"encoding/json"
"errors"
"fmt"
"go/ast"
"reflect"
"strings"
"github.com/ettle/strcase"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Config the tagliatelle configuration.
type Config struct {
Rules map[string]string
UseFieldName bool
}
// New creates an analyzer.
func New(config Config) *analysis.Analyzer {
return &analysis.Analyzer{
Name: "tagliatelle",
Doc: "Checks the struct tags.",
Run: func(pass *analysis.Pass) (interface{}, error) {
if len(config.Rules) == 0 {
return nil, nil
}
return run(pass, config)
},
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
}
func run(pass *analysis.Pass, config Config) (interface{}, error) {
isp, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("missing inspect analyser")
}
nodeFilter := []ast.Node{
(*ast.StructType)(nil),
}
isp.Preorder(nodeFilter, func(n ast.Node) {
node, ok := n.(*ast.StructType)
if !ok {
return
}
for _, field := range node.Fields.List {
analyze(pass, config, node, field)
}
})
return nil, nil
}
func analyze(pass *analysis.Pass, config Config, n *ast.StructType, field *ast.Field) {
if n.Fields == nil || n.Fields.NumFields() < 1 {
// skip empty structs
return
}
if field.Tag == nil {
// skip when no struct tag
return
}
fieldName, err := getFieldName(field)
if err != nil {
pass.Reportf(n.Pos(), "unable to get field name: %v", err)
return
}
for key, convName := range config.Rules {
if convName == "" {
continue
}
value, flags, ok := lookupTagValue(field.Tag, key)
if !ok {
// skip when no struct tag for the key
continue
}
if value == "-" {
// skip when skipped :)
continue
}
// TODO(ldez): need to be rethink.
// This is an exception because of a bug.
// https://github.com/ldez/tagliatelle/issues/8
// For now, tagliatelle should try to remain neutral in terms of format.
if hasTagFlag(flags, "inline") {
// skip for inline children (no name to lint)
continue
}
if value == "" {
value = fieldName
}
converter, err := getConverter(convName)
if err != nil {
pass.Reportf(n.Pos(), "%s(%s): %v", key, convName, err)
continue
}
expected := value
if config.UseFieldName {
expected = fieldName
}
if value != converter(expected) {
pass.Reportf(field.Tag.Pos(), "%s(%s): got '%s' want '%s'", key, convName, value, converter(expected))
}
}
}
func getFieldName(field *ast.Field) (string, error) {
var name string
for _, n := range field.Names {
if n.Name != "" {
name = n.Name
}
}
if name != "" {
return name, nil
}
return getTypeName(field.Type)
}
func getTypeName(exp ast.Expr) (string, error) {
switch typ := exp.(type) {
case *ast.Ident:
return typ.Name, nil
case *ast.StarExpr:
return getTypeName(typ.X)
case *ast.SelectorExpr:
return getTypeName(typ.Sel)
default:
bytes, _ := json.Marshal(exp)
return "", fmt.Errorf("unexpected error: type %T: %s", typ, string(bytes))
}
}
func lookupTagValue(tag *ast.BasicLit, key string) (name string, flags []string, ok bool) {
raw := strings.Trim(tag.Value, "`")
value, ok := reflect.StructTag(raw).Lookup(key)
if !ok {
return value, nil, ok
}
values := strings.Split(value, ",")
if len(values) < 1 {
return "", nil, true
}
return values[0], values[1:], true
}
func hasTagFlag(flags []string, query string) bool {
for _, flag := range flags {
if flag == query {
return true
}
}
return false
}
func getConverter(c string) (func(s string) string, error) {
switch c {
case "camel":
return strcase.ToCamel, nil
case "pascal":
return strcase.ToPascal, nil
case "kebab":
return strcase.ToKebab, nil
case "snake":
return strcase.ToSnake, nil
case "goCamel":
return strcase.ToGoCamel, nil
case "goPascal":
return strcase.ToGoPascal, nil
case "goKebab":
return strcase.ToGoKebab, nil
case "goSnake":
return strcase.ToGoSnake, nil
case "header":
return toHeader, nil
case "upper":
return strings.ToUpper, nil
case "upperSnake":
return strcase.ToSNAKE, nil
case "lower":
return strings.ToLower, nil
default:
return nil, fmt.Errorf("unsupported case: %s", c)
}
}
func toHeader(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '-')
}