Skip to content

Commit

Permalink
Revert "go/analysis/passes/structtag: recognize multiple keys per tag"
Browse files Browse the repository at this point in the history
This reverts CL 277092.

Proposal golang/go#40281 was initially accepted, then declined.  Stop accepting
the new, now once again invalid, format.

For golang/go#40281
For golang/go#43083
For golang/go#43226

Change-Id: Ida97263048f3a048f904a844f577d8353e3a1afa
Reviewed-on: https://go-review.googlesource.com/c/tools/+/281973
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
  • Loading branch information
ianlancetaylor committed Jan 7, 2021
1 parent a85a8ce commit 4ed967d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 104 deletions.
94 changes: 38 additions & 56 deletions go/analysis/passes/structtag/structtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ var (
)

// 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
Expand Down Expand Up @@ -240,27 +240,14 @@ func validateStructTag(tag string) error {
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
Expand All @@ -276,56 +263,51 @@ func validateStructTag(tag string) error {
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
}
21 changes: 0 additions & 21 deletions go/analysis/passes/structtag/structtag_go16_test.go

This file was deleted.

27 changes: 0 additions & 27 deletions go/analysis/passes/structtag/testdata/src/go16/go16.go

This file was deleted.

0 comments on commit 4ed967d

Please sign in to comment.