Skip to content

feature: add support of datastore struct tags in struct-tag rule #1240

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

Merged
merged 1 commit into from
Feb 18, 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
39 changes: 30 additions & 9 deletions rule/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,16 @@ func (w lintStructTagRule) Visit(node ast.Node) ast.Visitor {
}

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

func (w lintStructTagRule) checkTagNameIfNeed(tag *structtag.Tag) (string, bool) {
Expand Down Expand Up @@ -181,6 +182,11 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
if !ok {
w.addFailure(f.Tag, msg)
}
case keyDatastore:
msg, ok := w.checkDatastoreTag(tag.Options)
if !ok {
w.addFailure(f.Tag, msg)
}
case keyDefault:
if !w.typeValueMatch(f.Type, tag.Name) {
w.addFailure(f.Tag, "field's type and default value's type mismatch")
Expand Down Expand Up @@ -358,6 +364,21 @@ func (w lintStructTagRule) checkURLTag(options []string) (string, bool) {
return "", true
}

func (w lintStructTagRule) checkDatastoreTag(options []string) (string, bool) {
for _, opt := range options {
switch opt {
case "flatten", "noindex", "omitempty":
default:
if w.isUserDefined(keyDatastore, opt) {
continue
}
return fmt.Sprintf("unknown option '%s' in Datastore tag", opt), false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking

Suggested change
return fmt.Sprintf("unknown option '%s' in Datastore tag", opt), false
return fmt.Sprintf("unknown option '%s' in datastore tag", opt), false

Or a variation of this, with quotes maybe.

I'm unsure google datastore tag should be considered as an acronym or a brand.

JSON, BSON, XML were acronym. But look at the way "protobuf" tag is reported.

Copy link
Collaborator Author

@chavacava chavacava Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also hesitated with the name :)
The web site says "Datastore"

}
}

return "", true
}

func (lintStructTagRule) typeValueMatch(t ast.Expr, val string) bool {
tID, ok := t.(*ast.Ident)
if !ok {
Expand Down
7 changes: 6 additions & 1 deletion test/struct_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ 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", "url,myURLOption"},
Arguments: []any{
"json,inline,outline",
"bson,gnu",
"url,myURLOption",
"datastore,myDatastoreOption",
},
})
}

Expand Down
5 changes: 5 additions & 0 deletions testdata/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,8 @@ type RequestQueryOption struct {
Archived bool `url:"archived,myURLOption"` // MATCH /unknown option 'myURLOption' in URL tag/
IDProperty string `url:"idProperty,omitempty"`
}

type Fields struct {
Field string `datastore:",noindex,flatten,omitempty"`
OtherField string `datastore:",unknownOption"` // MATCH /unknown option 'unknownOption' in Datastore 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 @@ -19,3 +19,8 @@ type RequestQueryOptions struct {
CustomProperties []string `url:"-"`
Archived bool `url:"archived,myURLOption"`
}

type Fields struct {
Field string `datastore:",noindex,flatten,omitempty,myDatastoreOption"`
OtherField string `datastore:",unknownOption"` // MATCH /unknown option 'unknownOption' in Datastore tag/
}
Loading