Skip to content

Commit 89b82de

Browse files
Check jsontags
1 parent f53dcc5 commit 89b82de

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

pkg/analysis/nophase/analyzer.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import (
55
"go/ast"
66
"strings"
77

8+
"github.com/JoelSpeed/kal/pkg/analysis/helpers/extractjsontags"
89
"golang.org/x/tools/go/analysis"
910
"golang.org/x/tools/go/analysis/passes/inspect"
1011
"golang.org/x/tools/go/ast/inspector"
1112
)
1213

1314
const name = "nophase"
1415

15-
var errCouldNotGetInspector = errors.New("could not get inspector")
16+
var (
17+
errCouldNotGetInspector = errors.New("could not get inspector")
18+
errCouldNotGetJSONTags = errors.New("could not get json tags")
19+
)
1620

1721
// Analyzer is the analyzer for the nophase package.
1822
// It checks that no struct fields named 'phase', or that contain phase as a
@@ -21,7 +25,7 @@ var Analyzer = &analysis.Analyzer{
2125
Name: name,
2226
Doc: "phase fields are deprecated and conditions should be preferred, avoid phase like enum fields",
2327
Run: run,
24-
Requires: []*analysis.Analyzer{inspect.Analyzer},
28+
Requires: []*analysis.Analyzer{inspect.Analyzer, extractjsontags.Analyzer},
2529
}
2630

2731
func run(pass *analysis.Pass) (interface{}, error) {
@@ -30,6 +34,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
3034
return nil, errCouldNotGetInspector
3135
}
3236

37+
jsonTags, ok := pass.ResultOf[extractjsontags.Analyzer].(extractjsontags.StructFieldTags)
38+
if !ok {
39+
return nil, errCouldNotGetJSONTags
40+
}
41+
3342
// Filter to structs so that we can iterate over fields in a struct.
3443
nodeFilter := []ast.Node{
3544
(*ast.StructType)(nil),
@@ -56,11 +65,25 @@ func run(pass *analysis.Pass) (interface{}, error) {
5665

5766
fieldName := field.Names[0].Name
5867

68+
// First check if the struct field name contains 'phase'
5969
if strings.Contains(strings.ToLower(fieldName), "phase") {
6070
pass.Reportf(field.Pos(),
6171
"field %s: phase fields are deprecated and conditions should be preferred, avoid phase like enum fields",
6272
fieldName,
6373
)
74+
75+
continue
76+
}
77+
78+
// Then check if the json serialization of the field contains 'phase'
79+
tagInfo := jsonTags.FieldTags(sTyp, fieldName)
80+
81+
if strings.Contains(strings.ToLower(tagInfo.Name), "phase") {
82+
pass.Reportf(field.Pos(),
83+
"field %s: phase fields are deprecated and conditions should be preferred, avoid phase like enum fields",
84+
fieldName,
85+
)
86+
6487
}
6588
}
6689
})

pkg/analysis/nophase/testdata/src/a/a.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ type NoSubPhaseTestStruct struct {
1111
FooPhase *string `json:"fooPhase,omitempty"` // want "field FooPhase: phase fields are deprecated and conditions should be preferred, avoid phase like enum fields"
1212

1313
}
14+
15+
type SerializedPhaseTeststruct struct {
16+
// +optional
17+
FooField *string `json:"fooPhase,omitempty"` // want "field FooField: phase fields are deprecated and conditions should be preferred, avoid phase like enum fields"
18+
19+
}

pkg/analysis/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func NewRegistry() Registry {
5252
initializers: []AnalyzerInitializer{
5353
commentstart.Initializer(),
5454
jsontags.Initializer(),
55+
nophase.Initializer(),
5556
optionalorrequired.Initializer(),
5657
requiredfields.Initializer(),
57-
nophase.Initializer(),
5858
},
5959
}
6060
}

pkg/analysis/registry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ var _ = Describe("Registry", func() {
1818
Expect(r.DefaultLinters().UnsortedList()).To(ConsistOf(
1919
"commentstart",
2020
"jsontags",
21+
"nophase",
2122
"optionalorrequired",
2223
"requiredfields",
23-
"nophase",
2424
))
2525
})
2626
})
@@ -31,9 +31,9 @@ var _ = Describe("Registry", func() {
3131
Expect(r.AllLinters().UnsortedList()).To(ConsistOf(
3232
"commentstart",
3333
"jsontags",
34+
"nophase",
3435
"optionalorrequired",
3536
"requiredfields",
36-
"nophase",
3737
))
3838
})
3939
})

0 commit comments

Comments
 (0)