1
0
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:
chavacava
2025-03-03 13:19:40 +01:00
committed by GitHub
parent b04d5a71b6
commit 88d5434cee
4 changed files with 32 additions and 0 deletions

View File

@@ -106,6 +106,7 @@ const (
keyMapstructure = "mapstructure"
keyProtobuf = "protobuf"
keyRequired = "required"
keyTOML = "toml"
keyURL = "url"
keyValidate = "validate"
keyXML = "xml"
@@ -212,6 +213,11 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
if tag.Name != "true" && tag.Name != "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:
msg, ok := w.checkURLTag(tag.Options)
if !ok {
@@ -435,6 +441,21 @@ func (w lintStructTagRule) checkValidateTag(options []string) (string, bool) {
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) {
for _, alternative := range alternatives {
alternative := strings.TrimSpace(alternative)