Skip to content

Commit

Permalink
feature: add support of URL struct tags in struct-tag rule
Browse files Browse the repository at this point in the history
  • Loading branch information
chavacava committed Feb 15, 2025
1 parent a59a228 commit 9e04a61
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
28 changes: 28 additions & 0 deletions rule/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const (
keyJSON = "json"
keyProtobuf = "protobuf"
keyRequired = "required"
keyURL = "url"
keyXML = "xml"
keyYAML = "yaml"
)
Expand Down Expand Up @@ -196,6 +197,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 {
Expand Down Expand Up @@ -322,6 +328,28 @@ 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 "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 {
Expand Down
2 changes: 1 addition & 1 deletion test/struct_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func TestStructTag(t *testing.T) {

func TestStructTagWithUserOptions(t *testing.T) {
testRule(t, "struct_tag_user_options", &rule.StructTagRule{}, &lint.RuleConfig{
Arguments: []any{"json,inline,outline", "bson,gnu"},
Arguments: []any{"json,inline,outline", "bson,gnu", "url,myURLOption"},
})
}
13 changes: 13 additions & 0 deletions testdata/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,16 @@ type Simple struct {
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}

type RequestQueryOption struct {
Properties []string `url:"properties,comma,omitempty"`
CustomProperties []string `url:"-"`
Associations []string `url:"associations,brackets,omitempty"`
Associations2 []string `url:"associations2,semicolon,omitempty"`
Associations3 []string `url:"associations3,space,brackets,omitempty"`
Associations4 []string `url:"associations4,numbered,omitempty"`
Associations5 []string `url:"associations5,space,semicolon,omitempty"` // MATCH /can not set both 'semicolon' and 'space' as delimiters in URL tag/
PaginateAssociations bool `url:"paginateAssociations,int,omitempty"`
Archived bool `url:"archived,myURLOption"` // MATCH /unknown option 'myURLOption' in URL tag/
IDProperty string `url:"idProperty,omitempty"`
}
6 changes: 6 additions & 0 deletions testdata/struct_tag_user_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ type RangeAllocation struct {
Range string `bson:"range,flow"` // MATCH /unknown option 'flow' in BSON tag/
Data []byte `bson:"data,inline"`
}

type RequestQueryOptions struct {
Properties []string `url:"properties,commmma,omitempty"` // MATCH /unknown option 'commmma' in URL tag/
CustomProperties []string `url:"-"`
Archived bool `url:"archived,myURLOption"`
}

0 comments on commit 9e04a61

Please sign in to comment.