2013-05-17 14:20:39 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This file contains the test for canonical struct tags.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-11 18:43:33 -07:00
|
|
|
"errors"
|
2013-05-17 14:20:39 -06:00
|
|
|
"go/ast"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2015-01-11 18:43:33 -07:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
2013-05-17 14:20:39 -06:00
|
|
|
)
|
|
|
|
|
2014-06-12 23:04:45 -06:00
|
|
|
func init() {
|
|
|
|
register("structtags",
|
2014-06-13 19:44:31 -06:00
|
|
|
"check that struct field tags have canonical format and apply to exported fields as needed",
|
2014-06-12 23:04:45 -06:00
|
|
|
checkCanonicalFieldTag,
|
|
|
|
field)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkCanonicalFieldTag checks a struct field tag.
|
|
|
|
func checkCanonicalFieldTag(f *File, node ast.Node) {
|
|
|
|
field := node.(*ast.Field)
|
2013-05-17 14:20:39 -06:00
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:43:33 -07:00
|
|
|
if err := validateStructTag(tag); err != nil {
|
|
|
|
f.Badf(field.Pos(), "struct field tag %s not compatible with reflect.StructTag.Get: %s", field.Tag.Value, err)
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
2014-06-13 19:44:31 -06:00
|
|
|
|
|
|
|
// Check for use of json or xml tags with unexported fields.
|
|
|
|
|
|
|
|
// Embedded struct. Nothing to do for now, but that
|
|
|
|
// may change, depending on what happens with issue 7363.
|
|
|
|
if len(field.Names) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Names[0].IsExported() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:43:33 -07:00
|
|
|
st := reflect.StructTag(tag)
|
2014-06-13 19:44:31 -06:00
|
|
|
for _, enc := range [...]string{"json", "xml"} {
|
|
|
|
if st.Get(enc) != "" {
|
|
|
|
f.Badf(field.Pos(), "struct field %s has %s tag but is not exported", field.Names[0].Name, enc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
2015-01-11 18:43:33 -07:00
|
|
|
|
|
|
|
var (
|
|
|
|
errTagSyntax = errors.New("bad syntax for struct tag pair")
|
|
|
|
errTagKeySyntax = errors.New("bad syntax for struct tag key")
|
|
|
|
errTagValueSyntax = errors.New("bad syntax for struct tag value")
|
|
|
|
)
|
|
|
|
|
|
|
|
// validateStructTag parses the struct tag and returns an error if it is not
|
|
|
|
// in the canonical format, which is a space-separated list of key:"value"
|
|
|
|
// settings.
|
|
|
|
func validateStructTag(tag string) error {
|
|
|
|
elems := strings.Split(tag, " ")
|
|
|
|
for _, elem := range elems {
|
|
|
|
if elem == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.SplitN(elem, ":", 2)
|
|
|
|
if len(fields) != 2 {
|
|
|
|
return errTagSyntax
|
|
|
|
}
|
|
|
|
key, value := fields[0], fields[1]
|
|
|
|
if len(key) == 0 || len(value) < 2 {
|
|
|
|
return errTagSyntax
|
|
|
|
}
|
|
|
|
// Key must not contain control characters or quotes.
|
|
|
|
for _, r := range key {
|
|
|
|
if r == '"' || unicode.IsControl(r) {
|
|
|
|
return errTagKeySyntax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if value[0] != '"' || value[len(value)-1] != '"' {
|
|
|
|
return errTagValueSyntax
|
|
|
|
}
|
|
|
|
// Value must be quoted string
|
|
|
|
_, err := strconv.Unquote(value)
|
|
|
|
if err != nil {
|
|
|
|
return errTagValueSyntax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|