Skip to content

Commit 009f2b2

Browse files
committed
Add Warn for omitempty
1 parent 7a959a1 commit 009f2b2

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

pkg/analysis/optionalfields/analyzer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,10 @@ func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, markersAcce
127127
// (such as to be able to marshal an empty object with all possible options shown)
128128
// and when this is desired, there is an option to ignore the omitempty tag.
129129
func (a *analyzer) checkFieldOmitEmpty(pass *analysis.Pass, field *ast.Field, fieldName string, jsonTags extractjsontags.FieldTagInfo) {
130-
if a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicyIgnore {
131-
return
132-
}
133-
134-
if !jsonTags.OmitEmpty {
130+
switch {
131+
case jsonTags.OmitEmpty, a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicyIgnore:
132+
// Nothing to do, either we have omitempty, or we are ignoring it.
133+
case a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicySuggestFix:
135134
pass.Report(analysis.Diagnostic{
136135
Pos: field.Pos(),
137136
Message: fmt.Sprintf("field %s is optional and should be omitempty", fieldName),
@@ -147,6 +146,8 @@ func (a *analyzer) checkFieldOmitEmpty(pass *analysis.Pass, field *ast.Field, fi
147146
},
148147
},
149148
})
149+
case a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicyWarn:
150+
pass.Reportf(field.Pos(), "field %s is optional and should be omitempty", fieldName)
150151
}
151152
}
152153

pkg/config/linters_config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ type OptionalFieldsOmitEmpty struct {
181181
// policy determines whether the linter should require omitempty for all optional fields.
182182
// Valid values are "SuggestFix" and "Ignore".
183183
// When set to "SuggestFix", the linter will suggest adding the `omitempty` tag when an optional field does not have it.
184+
// When set to "Warn", the linter will emit a warning if the field does not have the `omitempty` tag.
184185
// When set to "Ignore", and optional field missing the `omitempty` tag will be ignored.
185186
// Note, when set to "Ignore", and a field does not have the `omitempty` tag, this may affect whether the field should be a pointer or not.
186187
Policy OptionalFieldsOmitEmptyPolicy `json:"policy"`
@@ -215,6 +216,9 @@ const (
215216
// OptionalFieldsOmitEmptyPolicySuggestFix indicates that the linter will emit a warning if the field does not have omitempty, and suggest a fix.
216217
OptionalFieldsOmitEmptyPolicySuggestFix OptionalFieldsOmitEmptyPolicy = "SuggestFix"
217218

219+
// OptionalFieldsOmitEmptyPolicyWarn indicates that the linter will emit a warning if the field does not have omitempty.
220+
OptionalFieldsOmitEmptyPolicyWarn OptionalFieldsOmitEmptyPolicy = "Warn"
221+
218222
// OptionalFieldsOmitEmptyPolicyIgnore indicates that the linter will ignore any field missing the omitempty tag.
219223
OptionalFieldsOmitEmptyPolicyIgnore OptionalFieldsOmitEmptyPolicy = "Ignore"
220224
)

pkg/validation/linters_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ func validateOptionFieldsOmitEmpty(oec config.OptionalFieldsOmitEmpty, fldPath *
126126
fieldErrors := field.ErrorList{}
127127

128128
switch oec.Policy {
129-
case "", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicySuggestFix:
129+
case "", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicyWarn, config.OptionalFieldsOmitEmptyPolicySuggestFix:
130130
default:
131-
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("policy"), oec.Policy, fmt.Sprintf("invalid value, must be one of %q, %q or omitted", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicySuggestFix)))
131+
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("policy"), oec.Policy, fmt.Sprintf("invalid value, must be one of %q, %q, %q or omitted", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicyWarn, config.OptionalFieldsOmitEmptyPolicySuggestFix)))
132132
}
133133

134134
return fieldErrors

0 commit comments

Comments
 (0)