1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00

feature: add support of datastore struct tags in struct-tag rume (#1240)

This commit is contained in:
chavacava
2025-02-18 13:09:31 +01:00
committed by GitHub
parent 3cf67c5783
commit a4ee892836
4 changed files with 46 additions and 10 deletions

View File

@@ -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) {
@@ -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")
@@ -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
}
}
return "", true
}
func (lintStructTagRule) typeValueMatch(t ast.Expr, val string) bool {
tID, ok := t.(*ast.Ident)
if !ok {

View File

@@ -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",
},
})
}

View File

@@ -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/
}

View File

@@ -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/
}