Skip to content

Commit

Permalink
feature: add suport of mapstructure struct tags in struct-tag rule (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chavacava authored Feb 19, 2025
1 parent a4ee892 commit 4f34235
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
41 changes: 31 additions & 10 deletions rule/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,17 @@ func (w lintStructTagRule) Visit(node ast.Node) ast.Visitor {
}

const (
keyASN1 = "asn1"
keyBSON = "bson"
keyDatastore = "datastore"
keyDefault = "default"
keyJSON = "json"
keyProtobuf = "protobuf"
keyRequired = "required"
keyURL = "url"
keyXML = "xml"
keyYAML = "yaml"
keyASN1 = "asn1"
keyBSON = "bson"
keyDatastore = "datastore"
keyDefault = "default"
keyJSON = "json"
keyMapstructure = "mapstructure"
keyProtobuf = "protobuf"
keyRequired = "required"
keyURL = "url"
keyXML = "xml"
keyYAML = "yaml"
)

func (w lintStructTagRule) checkTagNameIfNeed(tag *structtag.Tag) (string, bool) {
Expand Down Expand Up @@ -196,6 +197,11 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
if !ok {
w.addFailure(f.Tag, msg)
}
case keyMapstructure:
msg, ok := w.checkMapstructureTag(tag.Options)
if !ok {
w.addFailure(f.Tag, msg)
}
case keyProtobuf:
msg, ok := w.checkProtobufTag(tag)
if !ok {
Expand Down Expand Up @@ -379,6 +385,21 @@ func (w lintStructTagRule) checkDatastoreTag(options []string) (string, bool) {
return "", true
}

func (w lintStructTagRule) checkMapstructureTag(options []string) (string, bool) {
for _, opt := range options {
switch opt {
case "omitempty", "reminder", "squash":
default:
if w.isUserDefined(keyMapstructure, opt) {
continue
}
return fmt.Sprintf("unknown option '%s' in Mapstructure tag", opt), false
}
}

return "", true
}

func (lintStructTagRule) typeValueMatch(t ast.Expr, val string) bool {
tID, ok := t.(*ast.Ident)
if !ok {
Expand Down
1 change: 1 addition & 0 deletions test/struct_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestStructTagWithUserOptions(t *testing.T) {
"bson,gnu",
"url,myURLOption",
"datastore,myDatastoreOption",
"mapstructure,myMapstructureOption",
},
})
}
Expand Down
5 changes: 5 additions & 0 deletions testdata/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,8 @@ type Fields struct {
Field string `datastore:",noindex,flatten,omitempty"`
OtherField string `datastore:",unknownOption"` // MATCH /unknown option 'unknownOption' in Datastore tag/
}

type MapStruct struct {
Field1 string `mapstructure:",squash,reminder,omitempty"`
OtherField string `mapstructure:",unknownOption"` // MATCH /unknown option 'unknownOption' in Mapstructure tag/
}
5 changes: 5 additions & 0 deletions testdata/struct_tag_user_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ type Fields struct {
Field string `datastore:",noindex,flatten,omitempty,myDatastoreOption"`
OtherField string `datastore:",unknownOption"` // MATCH /unknown option 'unknownOption' in Datastore tag/
}

type MapStruct struct {
Field1 string `mapstructure:",squash,reminder,omitempty,myMapstructureOption"`
OtherField string `mapstructure:",unknownOption"` // MATCH /unknown option 'unknownOption' in Mapstructure tag/
}

0 comments on commit 4f34235

Please sign in to comment.