Skip to content

Commit be7010b

Browse files
committed
Update serialisation check to format error message
1 parent b446a4e commit be7010b

File tree

85 files changed

+1718
-1718
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1718
-1718
lines changed

pkg/analysis/optionalfields/analyzer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
9393
return nil, kalerrors.ErrCouldNotGetInspector
9494
}
9595

96-
inspect.InspectFields(func(field *ast.Field, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markershelper.Markers, _ string) {
97-
a.checkField(pass, field, markersAccess, jsonTagInfo)
96+
inspect.InspectFields(func(field *ast.Field, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markershelper.Markers, qualifiedFieldName string) {
97+
a.checkField(pass, field, markersAccess, jsonTagInfo, qualifiedFieldName)
9898
})
9999

100100
return nil, nil //nolint:nilnil
101101
}
102102

103-
func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelper.Markers, jsonTags extractjsontags.FieldTagInfo) {
103+
func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelper.Markers, jsonTags extractjsontags.FieldTagInfo, qualifiedFieldName string) {
104104
if field == nil || len(field.Names) == 0 {
105105
return
106106
}
@@ -116,7 +116,7 @@ func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, markersAcce
116116
return
117117
}
118118

119-
a.serializationCheck.Check(pass, field, markersAccess, jsonTags)
119+
a.serializationCheck.Check(pass, field, markersAccess, jsonTags, qualifiedFieldName)
120120
}
121121

122122
func defaultConfig(cfg *OptionalFieldsConfig) {

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,36 @@ type A struct {
1919

2020
// string is a string field.
2121
// +optional
22-
String string `json:"string,omitempty"` // want "field String should be a pointer."
22+
String string `json:"string,omitempty"` // want "field A.String should be a pointer."
2323

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

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

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

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

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

4444
// structWithMinProperties is a struct field with a minimum number of properties.
4545
// +kubebuilder:validation:MinProperties=1
4646
// +optional
47-
StructWithMinProperties B `json:"structWithMinProperties,omitempty"` // want "field StructWithMinProperties should be a pointer."
47+
StructWithMinProperties B `json:"structWithMinProperties,omitempty"` // want "field A.StructWithMinProperties should be a pointer."
4848

4949
// structWithMinPropertiesOnStruct is a struct field with a minimum number of properties on the struct.
5050
// +optional
51-
StructWithMinPropertiesOnStruct D `json:"structWithMinPropertiesOnStruct,omitempty"` // want "field StructWithMinPropertiesOnStruct should be a pointer."
51+
StructWithMinPropertiesOnStruct D `json:"structWithMinPropertiesOnStruct,omitempty"` // want "field A.StructWithMinPropertiesOnStruct should be a pointer."
5252

5353
// slice is a slice field.
5454
// +optional
@@ -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 underlying type does not need to be a pointer. The pointer should be removed."
63+
PointerSlice *[]string `json:"pointerSlice,omitempty"` // want "field A.PointerSlice 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 underlying type does not need to be a pointer. The pointer should be removed."
67+
PointerMap *map[string]string `json:"pointerMap,omitempty"` // want "field A.PointerMap 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 underlying type does not need to be a pointer. The pointer should be removed."
71+
DoublePointerString **string `json:"doublePointerString,omitempty"` // want "field A.DoublePointerString 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,16 +93,16 @@ type A struct {
9393

9494
// PointerSliceAlias is a pointer slice alias field.
9595
// +optional
96-
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field PointerSliceAlias underlying type does not need to be a pointer. The pointer should be removed."
96+
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field A.PointerSliceAlias 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"` // want "field PointerMapAlias underlying type does not need to be a pointer. The pointer should be removed."
100+
PointerMapAlias *MapAlias `json:"pointerMapAlias,omitempty"` // want "field A.PointerMapAlias underlying type does not need to be a pointer. The pointer should be removed."
101101

102102
// StringAliasWithEnum is a string alias field with enum validation.
103103
// With the "Always" pointer preference, optional fields should be pointers regardless of zero value validity.
104104
// +optional
105-
StringAliasWithEnum StringAliasWithEnum `json:"stringAliasWithEnum,omitempty"` // want "field StringAliasWithEnum should be a pointer."
105+
StringAliasWithEnum StringAliasWithEnum `json:"stringAliasWithEnum,omitempty"` // want "field A.StringAliasWithEnum should be a pointer."
106106

107107
// StringAliasWithEnumPointer is a pointer string alias field with enum validation.
108108
// This is correctly a pointer since the zero value is not valid.
@@ -111,7 +111,7 @@ type A struct {
111111

112112
// StringAliasWithEnumNoOmitEmpty is a string alias field with enum validation and no omitempty.
113113
// +optional
114-
StringAliasWithEnumNoOmitEmpty *StringAliasWithEnum `json:"stringAliasWithEnumNoOmitEmpty"` // want "field StringAliasWithEnumNoOmitEmpty should have the omitempty tag."
114+
StringAliasWithEnumNoOmitEmpty *StringAliasWithEnum `json:"stringAliasWithEnumNoOmitEmpty"` // want "field A.StringAliasWithEnumNoOmitEmpty should have the omitempty tag."
115115
}
116116

117117
type B struct {

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,36 @@ type A struct {
1919

2020
// string is a string field.
2121
// +optional
22-
String *string `json:"string,omitempty"` // want "field String should be a pointer."
22+
String *string `json:"string,omitempty"` // want "field A.String should be a pointer."
2323

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

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

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

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

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

4444
// structWithMinProperties is a struct field with a minimum number of properties.
4545
// +kubebuilder:validation:MinProperties=1
4646
// +optional
47-
StructWithMinProperties *B `json:"structWithMinProperties,omitempty"` // want "field StructWithMinProperties should be a pointer."
47+
StructWithMinProperties *B `json:"structWithMinProperties,omitempty"` // want "field A.StructWithMinProperties should be a pointer."
4848

4949
// structWithMinPropertiesOnStruct is a struct field with a minimum number of properties on the struct.
5050
// +optional
51-
StructWithMinPropertiesOnStruct *D `json:"structWithMinPropertiesOnStruct,omitempty"` // want "field StructWithMinPropertiesOnStruct should be a pointer."
51+
StructWithMinPropertiesOnStruct *D `json:"structWithMinPropertiesOnStruct,omitempty"` // want "field A.StructWithMinPropertiesOnStruct should be a pointer."
5252

5353
// slice is a slice field.
5454
// +optional
@@ -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 underlying type does not need to be a pointer. The pointer should be removed."
63+
PointerSlice []string `json:"pointerSlice,omitempty"` // want "field A.PointerSlice 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 underlying type does not need to be a pointer. The pointer should be removed."
67+
PointerMap map[string]string `json:"pointerMap,omitempty"` // want "field A.PointerMap 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 underlying type does not need to be a pointer. The pointer should be removed."
71+
DoublePointerString *string `json:"doublePointerString,omitempty"` // want "field A.DoublePointerString 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,16 +93,16 @@ type A struct {
9393

9494
// PointerSliceAlias is a pointer slice alias field.
9595
// +optional
96-
PointerSliceAlias SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field PointerSliceAlias underlying type does not need to be a pointer. The pointer should be removed."
96+
PointerSliceAlias SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field A.PointerSliceAlias 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"` // want "field PointerMapAlias underlying type does not need to be a pointer. The pointer should be removed."
100+
PointerMapAlias MapAlias `json:"pointerMapAlias,omitempty"` // want "field A.PointerMapAlias underlying type does not need to be a pointer. The pointer should be removed."
101101

102102
// StringAliasWithEnum is a string alias field with enum validation.
103103
// With the "Always" pointer preference, optional fields should be pointers regardless of zero value validity.
104104
// +optional
105-
StringAliasWithEnum *StringAliasWithEnum `json:"stringAliasWithEnum,omitempty"` // want "field StringAliasWithEnum should be a pointer."
105+
StringAliasWithEnum *StringAliasWithEnum `json:"stringAliasWithEnum,omitempty"` // want "field A.StringAliasWithEnum should be a pointer."
106106

107107
// StringAliasWithEnumPointer is a pointer string alias field with enum validation.
108108
// This is correctly a pointer since the zero value is not valid.
@@ -111,7 +111,7 @@ type A struct {
111111

112112
// StringAliasWithEnumNoOmitEmpty is a string alias field with enum validation and no omitempty.
113113
// +optional
114-
StringAliasWithEnumNoOmitEmpty *StringAliasWithEnum `json:"stringAliasWithEnumNoOmitEmpty,omitempty"` // want "field StringAliasWithEnumNoOmitEmpty should have the omitempty tag."
114+
StringAliasWithEnumNoOmitEmpty *StringAliasWithEnum `json:"stringAliasWithEnumNoOmitEmpty,omitempty"` // want "field A.StringAliasWithEnumNoOmitEmpty should have the omitempty tag."
115115
}
116116

117117
type B struct {

0 commit comments

Comments
 (0)