1
0
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:
chavacava
2025-02-17 07:18:30 +01:00
committed by GitHub
parent 8ece20b078
commit 3cf67c5783
4 changed files with 49 additions and 1 deletions

View File

@@ -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 {