Skip to content

Commit 21398c4

Browse files
ianlancetaylordmitshur
authored andcommitted
Revert "go/analysis/passes/structtag: recognize multiple keys per tag"
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 <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 5bd8423 commit 21398c4

File tree

3 files changed

+38
-104
lines changed

3 files changed

+38
-104
lines changed

go/analysis/passes/structtag/structtag.go

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ var (
207207
)
208208

209209
// validateStructTag parses the struct tag and returns an error if it is not
210-
// in the canonical format, as defined by reflect.StructTag.
210+
// in the canonical format, which is a space-separated list of key:"value"
211+
// settings. The value may contain spaces.
211212
func validateStructTag(tag string) error {
212213
// This code is based on the StructTag.Get code in package reflect.
213214

214215
n := 0
215-
var keys []string
216216
for ; tag != ""; n++ {
217217
if n > 0 && tag != "" && tag[0] != ' ' {
218218
// More restrictive than reflect, but catches likely mistakes
@@ -240,27 +240,14 @@ func validateStructTag(tag string) error {
240240
if i == 0 {
241241
return errTagKeySyntax
242242
}
243-
if i+1 >= len(tag) || tag[i] < ' ' || tag[i] == 0x7f {
243+
if i+1 >= len(tag) || tag[i] != ':' {
244244
return errTagSyntax
245245
}
246-
key := tag[:i]
247-
keys = append(keys, key)
248-
tag = tag[i:]
249-
250-
// If we found a space char here - assume that we have a tag with
251-
// multiple keys.
252-
if tag[0] == ' ' {
253-
continue
254-
}
255-
256-
// Spaces were filtered above so we assume that here we have
257-
// only valid tag value started with `:"`.
258-
if tag[0] != ':' || tag[1] != '"' {
246+
if tag[i+1] != '"' {
259247
return errTagValueSyntax
260248
}
261-
262-
// Remove the colon leaving tag at the start of the quoted string.
263-
tag = tag[1:]
249+
key := tag[:i]
250+
tag = tag[i+1:]
264251

265252
// Scan quoted string to find value.
266253
i = 1
@@ -276,56 +263,51 @@ func validateStructTag(tag string) error {
276263
qvalue := tag[:i+1]
277264
tag = tag[i+1:]
278265

279-
wholeValue, err := strconv.Unquote(qvalue)
266+
value, err := strconv.Unquote(qvalue)
280267
if err != nil {
281268
return errTagValueSyntax
282269
}
283270

284-
for _, key := range keys {
285-
if !checkTagSpaces[key] {
286-
continue
287-
}
288-
289-
value := wholeValue
290-
switch key {
291-
case "xml":
292-
// If the first or last character in the XML tag is a space, it is
293-
// suspicious.
294-
if strings.Trim(value, " ") != value {
295-
return errTagValueSpace
296-
}
271+
if !checkTagSpaces[key] {
272+
continue
273+
}
297274

298-
// If there are multiple spaces, they are suspicious.
299-
if strings.Count(value, " ") > 1 {
300-
return errTagValueSpace
301-
}
275+
switch key {
276+
case "xml":
277+
// If the first or last character in the XML tag is a space, it is
278+
// suspicious.
279+
if strings.Trim(value, " ") != value {
280+
return errTagValueSpace
281+
}
302282

303-
// If there is no comma, skip the rest of the checks.
304-
comma := strings.IndexRune(value, ',')
305-
if comma < 0 {
306-
continue
307-
}
283+
// If there are multiple spaces, they are suspicious.
284+
if strings.Count(value, " ") > 1 {
285+
return errTagValueSpace
286+
}
308287

309-
// If the character before a comma is a space, this is suspicious.
310-
if comma > 0 && value[comma-1] == ' ' {
311-
return errTagValueSpace
312-
}
313-
value = value[comma+1:]
314-
case "json":
315-
// JSON allows using spaces in the name, so skip it.
316-
comma := strings.IndexRune(value, ',')
317-
if comma < 0 {
318-
continue
319-
}
320-
value = value[comma+1:]
288+
// If there is no comma, skip the rest of the checks.
289+
comma := strings.IndexRune(value, ',')
290+
if comma < 0 {
291+
continue
321292
}
322293

323-
if strings.IndexByte(value, ' ') >= 0 {
294+
// If the character before a comma is a space, this is suspicious.
295+
if comma > 0 && value[comma-1] == ' ' {
324296
return errTagValueSpace
325297
}
298+
value = value[comma+1:]
299+
case "json":
300+
// JSON allows using spaces in the name, so skip it.
301+
comma := strings.IndexRune(value, ',')
302+
if comma < 0 {
303+
continue
304+
}
305+
value = value[comma+1:]
326306
}
327307

328-
keys = keys[:0]
308+
if strings.IndexByte(value, ' ') >= 0 {
309+
return errTagValueSpace
310+
}
329311
}
330312
return nil
331313
}

go/analysis/passes/structtag/structtag_go16_test.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

go/analysis/passes/structtag/testdata/src/go16/go16.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)