1
0
mirror of https://github.com/mgechev/revive.git synced 2025-02-07 13:31:42 +02:00

fix 208 struct-tag linter false positive (#209)

This commit is contained in:
SalvadorC 2019-08-05 18:21:20 +02:00 committed by Minko Gechev
parent 4790dddbd1
commit 9649b1c2a1
2 changed files with 10 additions and 3 deletions

View File

@ -20,7 +20,9 @@ type decodeAndValidateRequest struct {
OptionalStruct *optionalStruct `json:"optionalStruct,omitempty"`
OptionalQuery string `json:"-" querystring:"queryfoo"`
optionalQuery string `json:"-" querystring:"queryfoo"` // MATCH /tag on not-exported field optionalQuery/
// No-reg test for bug https://github.com/mgechev/revive/issues/208
Tiret string `json:"-,"`
BadTiret string `json:"other,"` // MATCH /option can not be empty in JSON tag/
}
type RangeAllocation struct {

View File

@ -86,7 +86,7 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
w.addFailure(f.Tag, "field's type and default value's type mismatch")
}
case "json":
msg, ok := w.checkJSONTag(tag.Options)
msg, ok := w.checkJSONTag(tag.Name, tag.Options)
if !ok {
w.addFailure(f.Tag, msg)
}
@ -161,10 +161,15 @@ func (w lintStructTagRule) checkBSONTag(options []string) (string, bool) {
return "", true
}
func (w lintStructTagRule) checkJSONTag(options []string) (string, bool) {
func (w lintStructTagRule) checkJSONTag(name string, options []string) (string, bool) {
for _, opt := range options {
switch opt {
case "omitempty", "string":
case "":
// special case for JSON key "-"
if name != "-" {
return "option can not be empty in JSON tag", false
}
default:
return fmt.Sprintf("unknown option '%s' in JSON tag", opt), false
}