golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
-golang.org/x/tools v0.0.0-20201211025543-abf6a1d87e11 h1:9j/upNXDRpADUw2RpUfJ7E7GHtfhDih62kX6JM8vs2c=
-golang.org/x/tools v0.0.0-20201211025543-abf6a1d87e11/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
+golang.org/x/tools v0.0.0-20210107193943-4ed967dd8eff h1:6EkB024TP1fu6cmQqeCNw685zYDVt5g8N1BXh755SQM=
+golang.org/x/tools v0.0.0-20210107193943-4ed967dd8eff/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
)
// validateStructTag parses the struct tag and returns an error if it is not
-// in the canonical format, as defined by reflect.StructTag.
+// in the canonical format, which is a space-separated list of key:"value"
+// settings. The value may contain spaces.
func validateStructTag(tag string) error {
// This code is based on the StructTag.Get code in package reflect.
n := 0
- var keys []string
for ; tag != ""; n++ {
if n > 0 && tag != "" && tag[0] != ' ' {
// More restrictive than reflect, but catches likely mistakes
if i == 0 {
return errTagKeySyntax
}
- if i+1 >= len(tag) || tag[i] < ' ' || tag[i] == 0x7f {
+ if i+1 >= len(tag) || tag[i] != ':' {
return errTagSyntax
}
- key := tag[:i]
- keys = append(keys, key)
- tag = tag[i:]
-
- // If we found a space char here - assume that we have a tag with
- // multiple keys.
- if tag[0] == ' ' {
- continue
- }
-
- // Spaces were filtered above so we assume that here we have
- // only valid tag value started with `:"`.
- if tag[0] != ':' || tag[1] != '"' {
+ if tag[i+1] != '"' {
return errTagValueSyntax
}
-
- // Remove the colon leaving tag at the start of the quoted string.
- tag = tag[1:]
+ key := tag[:i]
+ tag = tag[i+1:]
// Scan quoted string to find value.
i = 1
qvalue := tag[:i+1]
tag = tag[i+1:]
- wholeValue, err := strconv.Unquote(qvalue)
+ value, err := strconv.Unquote(qvalue)
if err != nil {
return errTagValueSyntax
}
- for _, key := range keys {
- if !checkTagSpaces[key] {
- continue
- }
-
- value := wholeValue
- switch key {
- case "xml":
- // If the first or last character in the XML tag is a space, it is
- // suspicious.
- if strings.Trim(value, " ") != value {
- return errTagValueSpace
- }
+ if !checkTagSpaces[key] {
+ continue
+ }
- // If there are multiple spaces, they are suspicious.
- if strings.Count(value, " ") > 1 {
- return errTagValueSpace
- }
+ switch key {
+ case "xml":
+ // If the first or last character in the XML tag is a space, it is
+ // suspicious.
+ if strings.Trim(value, " ") != value {
+ return errTagValueSpace
+ }
- // If there is no comma, skip the rest of the checks.
- comma := strings.IndexRune(value, ',')
- if comma < 0 {
- continue
- }
+ // If there are multiple spaces, they are suspicious.
+ if strings.Count(value, " ") > 1 {
+ return errTagValueSpace
+ }
- // If the character before a comma is a space, this is suspicious.
- if comma > 0 && value[comma-1] == ' ' {
- return errTagValueSpace
- }
- value = value[comma+1:]
- case "json":
- // JSON allows using spaces in the name, so skip it.
- comma := strings.IndexRune(value, ',')
- if comma < 0 {
- continue
- }
- value = value[comma+1:]
+ // If there is no comma, skip the rest of the checks.
+ comma := strings.IndexRune(value, ',')
+ if comma < 0 {
+ continue
}
- if strings.IndexByte(value, ' ') >= 0 {
+ // If the character before a comma is a space, this is suspicious.
+ if comma > 0 && value[comma-1] == ' ' {
return errTagValueSpace
}
+ value = value[comma+1:]
+ case "json":
+ // JSON allows using spaces in the name, so skip it.
+ comma := strings.IndexRune(value, ',')
+ if comma < 0 {
+ continue
+ }
+ value = value[comma+1:]
}
- keys = keys[:0]
+ if strings.IndexByte(value, ' ') >= 0 {
+ return errTagValueSpace
+ }
}
return nil
}