mirror of
https://github.com/mgechev/revive.git
synced 2025-07-05 00:28:53 +02:00
struct-tag warns on private fields with tags (#131)
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
package fixtures
|
package fixtures
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type decodeAndValidateRequest struct {
|
type decodeAndValidateRequest struct {
|
||||||
// BEAWRE : the flag of URLParam should match the const string URLParam
|
// BEAWRE : the flag of URLParam should match the const string URLParam
|
||||||
URLParam string `json:"-" path:"url_param" validate:"numeric"`
|
URLParam string `json:"-" path:"url_param" validate:"numeric"`
|
||||||
@ -17,6 +19,8 @@ type decodeAndValidateRequest struct {
|
|||||||
MandatoryStruct4 mandatoryStruct `json:"mandatoryStruct" required:"false"`
|
MandatoryStruct4 mandatoryStruct `json:"mandatoryStruct" required:"false"`
|
||||||
OptionalStruct *optionalStruct `json:"optionalStruct,omitempty"`
|
OptionalStruct *optionalStruct `json:"optionalStruct,omitempty"`
|
||||||
OptionalQuery string `json:"-" querystring:"queryfoo"`
|
OptionalQuery string `json:"-" querystring:"queryfoo"`
|
||||||
|
optionalQuery string `json:"-" querystring:"queryfoo"` // MATCH /tag on not-exported field optionalQuery/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RangeAllocation struct {
|
type RangeAllocation struct {
|
||||||
|
@ -2,11 +2,12 @@ package rule
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fatih/structtag"
|
|
||||||
"github.com/mgechev/revive/lint"
|
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/structtag"
|
||||||
|
"github.com/mgechev/revive/lint"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StructTagRule lints struct tags.
|
// StructTagRule lints struct tags.
|
||||||
@ -58,6 +59,10 @@ func (w lintStructTagRule) Visit(node ast.Node) ast.Visitor {
|
|||||||
// checkTaggedField checks the tag of the given field.
|
// checkTaggedField checks the tag of the given field.
|
||||||
// precondition: the field has a tag
|
// precondition: the field has a tag
|
||||||
func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
|
func (w lintStructTagRule) checkTaggedField(f *ast.Field) {
|
||||||
|
if len(f.Names) > 0 && !f.Names[0].IsExported() {
|
||||||
|
w.addFailure(f, "tag on not-exported field "+f.Names[0].Name)
|
||||||
|
}
|
||||||
|
|
||||||
tags, err := structtag.Parse(strings.Trim(f.Tag.Value, "`"))
|
tags, err := structtag.Parse(strings.Trim(f.Tag.Value, "`"))
|
||||||
if err != nil || tags == nil {
|
if err != nil || tags == nil {
|
||||||
w.addFailure(f.Tag, "malformed tag")
|
w.addFailure(f.Tag, "malformed tag")
|
||||||
|
Reference in New Issue
Block a user