1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-10 03:17:11 +02:00

struct-tag warns on private fields with tags (#131)

This commit is contained in:
SalvadorC 2019-06-01 10:34:43 +02:00 committed by Minko Gechev
parent 22b849f286
commit c967fd68ea
2 changed files with 11 additions and 2 deletions

View File

@ -1,5 +1,7 @@
package fixtures
import "time"
type decodeAndValidateRequest struct {
// BEAWRE : the flag of URLParam should match the const string URLParam
URLParam string `json:"-" path:"url_param" validate:"numeric"`
@ -17,6 +19,8 @@ type decodeAndValidateRequest struct {
MandatoryStruct4 mandatoryStruct `json:"mandatoryStruct" required:"false"`
OptionalStruct *optionalStruct `json:"optionalStruct,omitempty"`
OptionalQuery string `json:"-" querystring:"queryfoo"`
optionalQuery string `json:"-" querystring:"queryfoo"` // MATCH /tag on not-exported field optionalQuery/
}
type RangeAllocation struct {

View File

@ -2,11 +2,12 @@ package rule
import (
"fmt"
"github.com/fatih/structtag"
"github.com/mgechev/revive/lint"
"go/ast"
"strconv"
"strings"
"github.com/fatih/structtag"
"github.com/mgechev/revive/lint"
)
// 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.
// precondition: the field has a tag
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, "`"))
if err != nil || tags == nil {
w.addFailure(f.Tag, "malformed tag")