Skip to content

Commit 9375560

Browse files
committed
Rework and simplify optional fields linter
1 parent 7b3008b commit 9375560

File tree

9 files changed

+700
-930
lines changed

9 files changed

+700
-930
lines changed

pkg/analysis/optionalfields/analyzer.go

Lines changed: 185 additions & 391 deletions
Large diffs are not rendered by default.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ type A struct {
2323

2424
// NonOmittedString is a string field without omitempty
2525
// +optional
26-
NonOmittedString string `json:"nonOmittedString"` // want "field NonOmittedString is optional and should be a pointer" "field NonOmittedString is optional and should be omitempty"
26+
NonOmittedString string `json:"nonOmittedString"` // want "field NonOmittedString is optional and should be a pointer" "field NonOmittedString is optional and should have the omitempty tag"
2727

2828
// int is an int field.
2929
// +optional
3030
Int int `json:"int,omitempty"` // want "field Int is optional and should be a pointer"
3131

3232
// nonOmittedInt is an int field without omitempty
3333
// +optional
34-
NonOmittedInt int `json:"nonOmittedInt"` // want "field NonOmittedInt is optional and should be a pointer" "field NonOmittedInt is optional and should be omitempty"
34+
NonOmittedInt int `json:"nonOmittedInt"` // want "field NonOmittedInt is optional and should be a pointer" "field NonOmittedInt is optional and should have the omitempty tag"
3535

3636
// struct is a struct field.
3737
// +optional
3838
Struct B `json:"struct,omitempty"` // want "field Struct is optional and should be a pointer"
3939

4040
// nonOmittedStruct is a struct field without omitempty.
4141
// +optional
42-
NonOmittedStruct B `json:"nonOmittedStruct"` // want "field NonOmittedStruct is optional and should be a pointer" "field NonOmittedStruct is optional and should be omitempty"
42+
NonOmittedStruct B `json:"nonOmittedStruct"` // want "field NonOmittedStruct is optional and should be a pointer" "field NonOmittedStruct is optional and should have the omitempty tag"
4343

4444
// structWithMinProperties is a struct field with a minimum number of properties.
4545
// +kubebuilder:validation:MinProperties=1
@@ -60,15 +60,15 @@ type A struct {
6060

6161
// PointerSlice is a pointer slice field.
6262
// +optional
63-
PointerSlice *[]string `json:"pointerSlice,omitempty"` // want "field PointerSlice is a pointer type and should not be a pointer"
63+
PointerSlice *[]string `json:"pointerSlice,omitempty"` // want "field PointerSlice is optional but the underlying type does not need to be a pointer. The pointer should be removed."
6464

6565
// PointerMap is a pointer map field.
6666
// +optional
67-
PointerMap *map[string]string `json:"pointerMap,omitempty"` // want "field PointerMap is a pointer type and should not be a pointer"
67+
PointerMap *map[string]string `json:"pointerMap,omitempty"` // want "field PointerMap is optional but the underlying type does not need to be a pointer. The pointer should be removed."
6868

6969
// PointerPointerString is a double pointer string field.
7070
// +optional
71-
DoublePointerString **string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is a pointer type and should not be a pointer"
71+
DoublePointerString **string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is optional but the underlying type does not need to be a pointer. The pointer should be removed."
7272

7373
// PointerStringAlias is a pointer string alias field.
7474
// +optional
@@ -93,11 +93,11 @@ type A struct {
9393

9494
// PointerSliceAlias is a pointer slice alias field.
9595
// +optional
96-
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"`
96+
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field PointerSliceAlias is optional but the underlying type does not need to be a pointer. The pointer should be removed."
9797

9898
// PointerMapAlias is a pointer map alias field.
9999
// +optional
100-
PointerMapAlias *MapAlias `json:"pointerMapAlias,omitempty"`
100+
PointerMapAlias *MapAlias `json:"pointerMapAlias,omitempty"` // want "field PointerMapAlias is optional but the underlying type does not need to be a pointer. The pointer should be removed."
101101
}
102102

103103
type B struct {

pkg/analysis/optionalfields/testdata/src/a/a.go.golden

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ type A struct {
2323

2424
// NonOmittedString is a string field without omitempty
2525
// +optional
26-
NonOmittedString *string `json:"nonOmittedString,omitempty"` // want "field NonOmittedString is optional and should be a pointer" "field NonOmittedString is optional and should be omitempty"
26+
NonOmittedString *string `json:"nonOmittedString,omitempty"` // want "field NonOmittedString is optional and should be a pointer" "field NonOmittedString is optional and should have the omitempty tag"
2727

2828
// int is an int field.
2929
// +optional
3030
Int *int `json:"int,omitempty"` // want "field Int is optional and should be a pointer"
3131

3232
// nonOmittedInt is an int field without omitempty
3333
// +optional
34-
NonOmittedInt *int `json:"nonOmittedInt,omitempty"` // want "field NonOmittedInt is optional and should be a pointer" "field NonOmittedInt is optional and should be omitempty"
34+
NonOmittedInt *int `json:"nonOmittedInt,omitempty"` // want "field NonOmittedInt is optional and should be a pointer" "field NonOmittedInt is optional and should have the omitempty tag"
3535

3636
// struct is a struct field.
3737
// +optional
3838
Struct *B `json:"struct,omitempty"` // want "field Struct is optional and should be a pointer"
3939

4040
// nonOmittedStruct is a struct field without omitempty.
4141
// +optional
42-
NonOmittedStruct *B `json:"nonOmittedStruct,omitempty"` // want "field NonOmittedStruct is optional and should be a pointer" "field NonOmittedStruct is optional and should be omitempty"
42+
NonOmittedStruct *B `json:"nonOmittedStruct,omitempty"` // want "field NonOmittedStruct is optional and should be a pointer" "field NonOmittedStruct is optional and should have the omitempty tag"
4343

4444
// structWithMinProperties is a struct field with a minimum number of properties.
4545
// +kubebuilder:validation:MinProperties=1
@@ -60,15 +60,15 @@ type A struct {
6060

6161
// PointerSlice is a pointer slice field.
6262
// +optional
63-
PointerSlice []string `json:"pointerSlice,omitempty"` // want "field PointerSlice is a pointer type and should not be a pointer"
63+
PointerSlice []string `json:"pointerSlice,omitempty"` // want "field PointerSlice is optional but the underlying type does not need to be a pointer. The pointer should be removed."
6464

6565
// PointerMap is a pointer map field.
6666
// +optional
67-
PointerMap map[string]string `json:"pointerMap,omitempty"` // want "field PointerMap is a pointer type and should not be a pointer"
67+
PointerMap map[string]string `json:"pointerMap,omitempty"` // want "field PointerMap is optional but the underlying type does not need to be a pointer. The pointer should be removed."
6868

6969
// PointerPointerString is a double pointer string field.
7070
// +optional
71-
DoublePointerString *string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is a pointer type and should not be a pointer"
71+
DoublePointerString *string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is optional but the underlying type does not need to be a pointer. The pointer should be removed."
7272

7373
// PointerStringAlias is a pointer string alias field.
7474
// +optional
@@ -93,11 +93,11 @@ type A struct {
9393

9494
// PointerSliceAlias is a pointer slice alias field.
9595
// +optional
96-
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"`
96+
PointerSliceAlias SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field PointerSliceAlias is optional but the underlying type does not need to be a pointer. The pointer should be removed."
9797

9898
// PointerMapAlias is a pointer map alias field.
9999
// +optional
100-
PointerMapAlias *MapAlias `json:"pointerMapAlias,omitempty"`
100+
PointerMapAlias MapAlias `json:"pointerMapAlias,omitempty"` // want "field PointerMapAlias is optional but the underlying type does not need to be a pointer. The pointer should be removed."
101101
}
102102

103103
type B struct {

0 commit comments

Comments
 (0)