mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
feature: add support for TOML struct tags in struct-tag rule (#1255)
This commit is contained in:
@@ -106,6 +106,7 @@ const (
|
|||||||
keyMapstructure = "mapstructure"
|
keyMapstructure = "mapstructure"
|
||||||
keyProtobuf = "protobuf"
|
keyProtobuf = "protobuf"
|
||||||
keyRequired = "required"
|
keyRequired = "required"
|
||||||
|
keyTOML = "toml"
|
||||||
keyURL = "url"
|
keyURL = "url"
|
||||||
keyValidate = "validate"
|
keyValidate = "validate"
|
||||||
keyXML = "xml"
|
keyXML = "xml"
|
||||||
@@ -212,6 +213,11 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
|
|||||||
if tag.Name != "true" && tag.Name != "false" {
|
if tag.Name != "true" && tag.Name != "false" {
|
||||||
w.addFailure(f.Tag, "required should be 'true' or 'false'")
|
w.addFailure(f.Tag, "required should be 'true' or 'false'")
|
||||||
}
|
}
|
||||||
|
case keyTOML:
|
||||||
|
msg, ok := w.checkTOMLTag(tag.Options)
|
||||||
|
if !ok {
|
||||||
|
w.addFailure(f.Tag, msg)
|
||||||
|
}
|
||||||
case keyURL:
|
case keyURL:
|
||||||
msg, ok := w.checkURLTag(tag.Options)
|
msg, ok := w.checkURLTag(tag.Options)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -435,6 +441,21 @@ func (w lintStructTagRule) checkValidateTag(options []string) (string, bool) {
|
|||||||
return "", true
|
return "", true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w lintStructTagRule) checkTOMLTag(options []string) (string, bool) {
|
||||||
|
for _, opt := range options {
|
||||||
|
switch opt {
|
||||||
|
case "omitempty":
|
||||||
|
default:
|
||||||
|
if w.isUserDefined(keyTOML, opt) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("unknown option '%s' in TOML tag", opt), false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
|
||||||
func (w lintStructTagRule) checkValidateOptionsAlternatives(alternatives []string) (string, bool) {
|
func (w lintStructTagRule) checkValidateOptionsAlternatives(alternatives []string) (string, bool) {
|
||||||
for _, alternative := range alternatives {
|
for _, alternative := range alternatives {
|
||||||
alternative := strings.TrimSpace(alternative)
|
alternative := strings.TrimSpace(alternative)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ func TestStructTagWithUserOptions(t *testing.T) {
|
|||||||
"datastore,myDatastoreOption",
|
"datastore,myDatastoreOption",
|
||||||
"mapstructure,myMapstructureOption",
|
"mapstructure,myMapstructureOption",
|
||||||
"validate,displayName",
|
"validate,displayName",
|
||||||
|
"toml,unknown",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
5
testdata/struct_tag.go
vendored
5
testdata/struct_tag.go
vendored
@@ -158,3 +158,8 @@ type ValidateUser struct {
|
|||||||
BadComplex2 string `validate:"gt=0,dive,eq=1|eq=2,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
BadComplex2 string `validate:"gt=0,dive,eq=1|eq=2,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
||||||
BadComplex3 string `validate:"gt=0,dive,keys,eq=1|eq=2,endkeys,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
BadComplex3 string `validate:"gt=0,dive,keys,eq=1|eq=2,endkeys,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TomlUser struct {
|
||||||
|
Username string `toml:"username,omitempty"`
|
||||||
|
Location string `toml:"location,unknown"` // MATCH /unknown option 'unknown' in TOML tag/
|
||||||
|
}
|
||||||
|
|||||||
5
testdata/struct_tag_user_options.go
vendored
5
testdata/struct_tag_user_options.go
vendored
@@ -41,3 +41,8 @@ type ValidateUser struct {
|
|||||||
BadComplex2 string `validate:"gt=0,dive,eq=1|eq=2,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
BadComplex2 string `validate:"gt=0,dive,eq=1|eq=2,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
||||||
BadComplex3 string `validate:"gt=0,dive,keys,eq=1|eq=2,endkeys,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
BadComplex3 string `validate:"gt=0,dive,keys,eq=1|eq=2,endkeys,endkeys,required"` // MATCH /option 'endkeys' without a previous 'keys' option in validate tag/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TomlUser struct {
|
||||||
|
Username string `toml:"username,omitempty"`
|
||||||
|
Location string `toml:"location,unknown"`
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user