"errors"
"go/ast"
"go/token"
+ "go/types"
"reflect"
"strconv"
"strings"
// checkStructFieldTags checks all the field tags of a struct, including checking for duplicates.
func checkStructFieldTags(f *File, node ast.Node) {
+ styp := f.pkg.types[node.(*ast.StructType)].Type.(*types.Struct)
var seen map[[2]string]token.Pos
- for _, field := range node.(*ast.StructType).Fields.List {
- checkCanonicalFieldTag(f, field, &seen)
+ for i := 0; i < styp.NumFields(); i++ {
+ field := styp.Field(i)
+ tag := styp.Tag(i)
+ checkCanonicalFieldTag(f, field, tag, &seen)
}
}
var checkTagSpaces = map[string]bool{"json": true, "xml": true, "asn1": true}
// checkCanonicalFieldTag checks a single struct field tag.
-func checkCanonicalFieldTag(f *File, field *ast.Field, seen *map[[2]string]token.Pos) {
- if field.Tag == nil {
- return
- }
-
- tag, err := strconv.Unquote(field.Tag.Value)
- if err != nil {
- f.Badf(field.Pos(), "unable to read struct tag %s", field.Tag.Value)
+func checkCanonicalFieldTag(f *File, field *types.Var, tag string, seen *map[[2]string]token.Pos) {
+ if tag == "" {
return
}
if err := validateStructTag(tag); err != nil {
- raw, _ := strconv.Unquote(field.Tag.Value) // field.Tag.Value is known to be a quoted string
- f.Badf(field.Pos(), "struct field tag %#q not compatible with reflect.StructTag.Get: %s", raw, err)
+ f.Badf(field.Pos(), "struct field tag %#q not compatible with reflect.StructTag.Get: %s", tag, err)
}
for _, key := range checkTagDups {
if val == "" || val == "-" || val[0] == ',' {
continue
}
- if key == "xml" && len(field.Names) > 0 && field.Names[0].Name == "XMLName" {
+ if key == "xml" && field.Name() == "XMLName" {
// XMLName defines the XML element name of the struct being
// checked. That name cannot collide with element or attribute
// names defined on other fields of the struct. Vet does not have a
*seen = map[[2]string]token.Pos{}
}
if pos, ok := (*seen)[[2]string{key, val}]; ok {
- var name string
- if len(field.Names) > 0 {
- name = field.Names[0].Name
- } else {
- name = field.Type.(*ast.Ident).Name
- }
- f.Badf(field.Pos(), "struct field %s repeats %s tag %q also at %s", name, key, val, f.loc(pos))
+ f.Badf(field.Pos(), "struct field %s repeats %s tag %q also at %s", field.Name(), key, val, f.loc(pos))
} else {
(*seen)[[2]string{key, val}] = field.Pos()
}
// Embedded struct. Nothing to do for now, but that
// may change, depending on what happens with issue 7363.
- if len(field.Names) == 0 {
+ if field.Anonymous() {
return
}
- if field.Names[0].IsExported() {
+ if field.Exported() {
return
}
for _, enc := range [...]string{"json", "xml"} {
if reflect.StructTag(tag).Get(enc) != "" {
- f.Badf(field.Pos(), "struct field %s has %s tag but is not exported", field.Names[0].Name, enc)
+ f.Badf(field.Pos(), "struct field %s has %s tag but is not exported", field.Name(), enc)
return
}
}
import "encoding/xml"
type StructTagTest struct {
- A int "hello" // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag pair"
+ A int "hello" // ERROR "`hello` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair"
B int "\tx:\"y\"" // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag key"
C int "x:\"y\"\tx:\"y\"" // ERROR "not compatible with reflect.StructTag.Get"
D int "x:`y`" // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag value"
DuplicateOmitXML int `xml:"a,omitempty"` // ERROR "struct field DuplicateOmitXML repeats xml tag .a. also at structtag.go:60"
NonXML int `foo:"a"`
DuplicateNonXML int `foo:"a"`
- Embedded struct {
+ Embedded2 struct {
DuplicateXML int `xml:"a"` // OK because its not in the same struct type
}
AnonymousXML `xml:"a"` // ERROR "struct field AnonymousXML repeats xml tag .a. also at structtag.go:60"