Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add suport of mapstructure struct tags in struct-tag rule #1241

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/
}
Loading