mirror of
https://github.com/mgechev/revive.git
synced 2025-11-25 22:12:38 +02:00
feature: add support of datastore struct tags in struct-tag rume (#1240)
This commit is contained in:
@@ -98,15 +98,16 @@ func (w lintStructTagRule) Visit(node ast.Node) ast.Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
keyASN1 = "asn1"
|
keyASN1 = "asn1"
|
||||||
keyBSON = "bson"
|
keyBSON = "bson"
|
||||||
keyDefault = "default"
|
keyDatastore = "datastore"
|
||||||
keyJSON = "json"
|
keyDefault = "default"
|
||||||
keyProtobuf = "protobuf"
|
keyJSON = "json"
|
||||||
keyRequired = "required"
|
keyProtobuf = "protobuf"
|
||||||
keyURL = "url"
|
keyRequired = "required"
|
||||||
keyXML = "xml"
|
keyURL = "url"
|
||||||
keyYAML = "yaml"
|
keyXML = "xml"
|
||||||
|
keyYAML = "yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w lintStructTagRule) checkTagNameIfNeed(tag *structtag.Tag) (string, bool) {
|
func (w lintStructTagRule) checkTagNameIfNeed(tag *structtag.Tag) (string, bool) {
|
||||||
@@ -181,6 +182,11 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
w.addFailure(f.Tag, msg)
|
w.addFailure(f.Tag, msg)
|
||||||
}
|
}
|
||||||
|
case keyDatastore:
|
||||||
|
msg, ok := w.checkDatastoreTag(tag.Options)
|
||||||
|
if !ok {
|
||||||
|
w.addFailure(f.Tag, msg)
|
||||||
|
}
|
||||||
case keyDefault:
|
case keyDefault:
|
||||||
if !w.typeValueMatch(f.Type, tag.Name) {
|
if !w.typeValueMatch(f.Type, tag.Name) {
|
||||||
w.addFailure(f.Tag, "field's type and default value's type mismatch")
|
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
|
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 {
|
func (lintStructTagRule) typeValueMatch(t ast.Expr, val string) bool {
|
||||||
tID, ok := t.(*ast.Ident)
|
tID, ok := t.(*ast.Ident)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -13,7 +13,12 @@ func TestStructTag(t *testing.T) {
|
|||||||
|
|
||||||
func TestStructTagWithUserOptions(t *testing.T) {
|
func TestStructTagWithUserOptions(t *testing.T) {
|
||||||
testRule(t, "struct_tag_user_options", &rule.StructTagRule{}, &lint.RuleConfig{
|
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",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
testdata/struct_tag.go
vendored
5
testdata/struct_tag.go
vendored
@@ -136,3 +136,8 @@ type RequestQueryOption struct {
|
|||||||
Archived bool `url:"archived,myURLOption"` // MATCH /unknown option 'myURLOption' in URL tag/
|
Archived bool `url:"archived,myURLOption"` // MATCH /unknown option 'myURLOption' in URL tag/
|
||||||
IDProperty string `url:"idProperty,omitempty"`
|
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
testdata/struct_tag_user_options.go
vendored
5
testdata/struct_tag_user_options.go
vendored
@@ -19,3 +19,8 @@ type RequestQueryOptions struct {
|
|||||||
CustomProperties []string `url:"-"`
|
CustomProperties []string `url:"-"`
|
||||||
Archived bool `url:"archived,myURLOption"`
|
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/
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user