mirror of
https://github.com/mgechev/revive.git
synced 2025-11-25 22:12:38 +02:00
feature: add support of URL struct tags in struct-tag rule (#1239)
This commit is contained in:
@@ -104,6 +104,7 @@ const (
|
||||
keyJSON = "json"
|
||||
keyProtobuf = "protobuf"
|
||||
keyRequired = "required"
|
||||
keyURL = "url"
|
||||
keyXML = "xml"
|
||||
keyYAML = "yaml"
|
||||
)
|
||||
@@ -198,6 +199,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 keyURL:
|
||||
msg, ok := w.checkURLTag(tag.Options)
|
||||
if !ok {
|
||||
w.addFailure(f.Tag, msg)
|
||||
}
|
||||
case keyXML:
|
||||
msg, ok := w.checkXMLTag(tag.Options)
|
||||
if !ok {
|
||||
@@ -329,6 +335,29 @@ func (w lintStructTagRule) checkYAMLTag(options []string) (string, bool) {
|
||||
return "", true
|
||||
}
|
||||
|
||||
func (w lintStructTagRule) checkURLTag(options []string) (string, bool) {
|
||||
var delimiter = ""
|
||||
for _, opt := range options {
|
||||
switch opt {
|
||||
case "int", "omitempty", "numbered", "brackets":
|
||||
case "unix", "unixmilli", "unixnano": // TODO : check that the field is of type time.Time
|
||||
case "comma", "semicolon", "space":
|
||||
if delimiter == "" {
|
||||
delimiter = opt
|
||||
continue
|
||||
}
|
||||
return fmt.Sprintf("can not set both '%s' and '%s' as delimiters in URL tag", opt, delimiter), false
|
||||
default:
|
||||
if w.isUserDefined(keyURL, opt) {
|
||||
continue
|
||||
}
|
||||
return fmt.Sprintf("unknown option '%s' in URL tag", opt), false
|
||||
}
|
||||
}
|
||||
|
||||
return "", true
|
||||
}
|
||||
|
||||
func (lintStructTagRule) typeValueMatch(t ast.Expr, val string) bool {
|
||||
tID, ok := t.(*ast.Ident)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user