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:
parent
4790dddbd1
commit
9649b1c2a1
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user