Skip to content

Commit 4a4e30f

Browse files
committed
Address review comments
1 parent d5f8bf2 commit 4a4e30f

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

pkg/analysis/helpers/extractjsontags/analyzer.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"golang.org/x/tools/go/analysis"
2626
"golang.org/x/tools/go/analysis/passes/inspect"
2727
"golang.org/x/tools/go/ast/inspector"
28-
28+
"k8s.io/utils/set"
2929
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
3030
)
3131

@@ -142,15 +142,15 @@ func extractTagInfo(tag *ast.BasicLit) FieldTagInfo {
142142
}
143143

144144
tagName := tagValues[0]
145+
tagSet := set.New[string](tagValues...)
145146

146147
return FieldTagInfo{
147148
Name: tagName,
148-
OmitEmpty: (len(tagValues) == 2 && tagValues[1] == omitEmpty) || (len(tagValues) == 3 && (tagValues[1] == omitEmpty || tagValues[2] == omitEmpty)),
149-
// Considering, omitzero will always coexist with omitemtpy and it can be either second or third tag.
150-
OmitZero: len(tagValues) == 3 && (tagValues[1] == omitZero || tagValues[2] == omitZero),
151-
RawValue: tagValue,
152-
Pos: pos,
153-
End: end,
149+
OmitEmpty: tagSet.Has(omitEmpty),
150+
OmitZero: tagSet.Has(omitZero),
151+
RawValue: tagValue,
152+
Pos: pos,
153+
End: end,
154154
}
155155
}
156156

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,15 @@ type A struct {
171171
// +optional
172172
StructWithMinProperties B `json:"structWithMinProperties,omitempty"` // want "field StructWithMinProperties is optional and should be a pointer"
173173

174-
// StructWithMinPropertiesAndOmitEmptyTag is a struct field with a minimum number of properties.
174+
// structWithMinPropertiesAndOmitEmptyAndOmitZero is a struct field with a minimum number of properties and has both omitzero and omitemtpy tag.
175175
// +kubebuilder:validation:MinProperties=1
176176
// +optional
177-
StructWithMinPropertiesAndOmitEmptyTag B `json:"structWithMinPropertiesAndOmitEmptyTag,omitempty,omitzero"`
177+
StructWithMinPropertiesAndOmitEmptyAndOmitZero B `json:"structWithMinPropertiesAndOmitEmptyAndOmitZero,omitempty,omitzero"`
178+
179+
// structWithOnlyOmitZeroTag is a struct field with a minimum number of properties and has only omitzero tag.
180+
// +kubebuilder:validation:MinProperties=1
181+
// +optional
182+
StructWithOnlyOmitZeroTag B `json:"structWithOnlyOmitZeroTag,omitzero"` // want "field StructWithOnlyOmitZeroTag is optional and should have the omitempty tag"
178183

179184
// structWithMinPropertiesOnStruct is a struct field with a minimum number of properties on the struct.
180185
// +optional

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,15 @@ type A struct {
171171
// +optional
172172
StructWithMinProperties *B `json:"structWithMinProperties,omitempty"` // want "field StructWithMinProperties is optional and should be a pointer"
173173

174-
// StructWithMinPropertiesAndOmitEmptyTag is a struct field with a minimum number of properties.
174+
// structWithMinPropertiesAndOmitEmptyAndOmitZero is a struct field with a minimum number of properties and has both omitzero and omitemtpy tag.
175175
// +kubebuilder:validation:MinProperties=1
176176
// +optional
177-
StructWithMinPropertiesAndOmitEmptyTag B `json:"structWithMinPropertiesAndOmitEmptyTag,omitempty,omitzero"`
177+
StructWithMinPropertiesAndOmitEmptyAndOmitZero B `json:"structWithMinPropertiesAndOmitEmptyAndOmitZero,omitempty,omitzero"`
178+
179+
// structWithOnlyOmitZeroTag is a struct field with a minimum number of properties and has only omitzero tag.
180+
// +kubebuilder:validation:MinProperties=1
181+
// +optional
182+
StructWithOnlyOmitZeroTag B `json:"structWithOnlyOmitZeroTag,omitempty,omitzero"` // want "field StructWithOnlyOmitZeroTag is optional and should have the omitempty tag"
178183

179184
// structWithMinPropertiesOnStruct is a struct field with a minimum number of properties on the struct.
180185
// +optional

0 commit comments

Comments
 (0)