From c174e46ae91e966ab05254f739fa1692cf451a3f Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Sat, 9 Sep 2017 10:49:04 -0500 Subject: [PATCH] cmd/vet: don't warn on expected space in XML tag The change in https://golang.org/cl/43295 added warning about spaces in struct tags. However, in XML tags it is expected that there will be a space between the namespace and the local name. Change-Id: Ic31c3bdae30797f406f25c737b83bbe2de1ed1db Reviewed-on: https://go-review.googlesource.com/62570 Run-TryBot: Rob Pike TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/cmd/vet/all/whitelist/all.txt | 25 ------------------------- src/cmd/vet/structtag.go | 26 +++++++++++++++++++++++++- src/cmd/vet/testdata/structtag.go | 24 +++++++++++++++--------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/cmd/vet/all/whitelist/all.txt b/src/cmd/vet/all/whitelist/all.txt index e52e3b7343..c28035f8bb 100644 --- a/src/cmd/vet/all/whitelist/all.txt +++ b/src/cmd/vet/all/whitelist/all.txt @@ -54,28 +54,3 @@ cmd/link/link_test.go: struct field tag "\n\tLondon. Michaelmas term lately over cmd/link/link_test.go: struct field tag "\n\tIt was grand to see how the wind awoke, and bent the trees, and drove the rain before it like a cloud of smoke; and to hear the solemn thunder, and to see the lightning; and while thinking with awe of the tremendous powers by which our little lives are encompassed, to consider how beneficent they are, and how upon the smallest flower and leaf there was already a freshness poured from all this seeming rage, which seemed to make creation new again." not compatible with reflect.StructTag.Get: bad syntax for struct tag key cmd/link/link_test.go: struct field tag "\n\tJarndyce and Jarndyce drones on. This scarecrow of a suit has, over the course of time, become so complicated, that no man alive knows what it means. The parties to it understand it least; but it has been observed that no two Chancery lawyers can talk about it for five minutes, without coming to a total disagreement as to all the premises. Innumerable children have been born into the cause; innumerable young people have married into it; innumerable old people have died out of it. Scores of persons have deliriously found themselves made parties in Jarndyce and Jarndyce, without knowing how or why; whole families have inherited legendary hatreds with the suit. The little plaintiff or defendant, who was promised a new rocking-horse when Jarndyce and Jarndyce should be settled, has grown up, possessed himself of a real horse, and trotted away into the other world. Fair wards of court have faded into mothers and grandmothers; a long procession of Chancellors has come in and gone out; the legion of bills in the suit have been transformed into mere bills of mortality; there are not three Jarndyces left upon the earth perhaps, since old Tom Jarndyce in despair blew his brains out at a coffee-house in Chancery Lane; but Jarndyce and Jarndyce still drags its dreary length before the Court, perennially hopeless." not compatible with reflect.StructTag.Get: bad syntax for struct tag key cmd/link/link_test.go: struct field tag "\n\tThe one great principle of the English law is, to make business for itself. There is no other principle distinctly, certainly, and consistently maintained through all its narrow turnings. Viewed by this light it becomes a coherent scheme, and not the monstrous maze the laity are apt to think it. Let them but once clearly perceive that its grand principle is to make business for itself at their expense, and surely they will cease to grumble." not compatible with reflect.StructTag.Get: bad syntax for struct tag key - -// Tests of how xml handles spaces in struct tags. -encoding/xml/marshal_test.go: struct field tag `xml:"ns foo"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"testns outer"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"outerns test"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"outerns test"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space top"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>d"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>d"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space x>b"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>b"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/2005/Atom feed"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/TR/html4/ table"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://www.w3schools.com/furniture table"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/TR/html4/ table,attr"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://www.w3schools.com/furniture table,attr"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/XML/1998/namespace lang,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/xml/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/xmlfoo/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/json/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value -encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/2/json/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value \ No newline at end of file diff --git a/src/cmd/vet/structtag.go b/src/cmd/vet/structtag.go index eeef0ec110..cfa816694e 100644 --- a/src/cmd/vet/structtag.go +++ b/src/cmd/vet/structtag.go @@ -185,7 +185,31 @@ func validateStructTag(tag string) error { continue } - if key == "json" { + 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 are multiple spaces, they are suspicious. + if strings.Count(value, " ") > 1 { + return errTagValueSpace + } + + // If there is no comma, skip the rest of the checks. + comma := strings.IndexRune(value, ',') + if comma < 0 { + continue + } + + // 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 { diff --git a/src/cmd/vet/testdata/structtag.go b/src/cmd/vet/testdata/structtag.go index cdd29bb94a..6eec2e3fec 100644 --- a/src/cmd/vet/testdata/structtag.go +++ b/src/cmd/vet/testdata/structtag.go @@ -84,13 +84,19 @@ type DuplicateJSONFields struct { type UnexpectedSpacetest struct { A int `json:"a,omitempty"` B int `json:"b, omitempty"` // ERROR "suspicious space found in struct tag value" - C int `json:"d ,omitempty"` - D int `json:"e,omitempty, string"` // ERROR "suspicious space found in struct tag value" - E int `xml:" f"` // ERROR "suspicious space found in struct tag value" - F int `xml:"g "` // ERROR "suspicious space found in struct tag value" - G int `xml:"h ,omitempty"` // ERROR "suspicious space found in struct tag value" - H int `xml:" i"` // ERROR "suspicious space found in struct tag value" - I int `xml:"j "` // ERROR "suspicious space found in struct tag value" - J int `xml:"k ,omitempty"` // ERROR "suspicious space found in struct tag value" - K int `foo:" doesn't care "` + C int `json:"c ,omitempty"` + D int `json:"d,omitempty, string"` // ERROR "suspicious space found in struct tag value" + E int `xml:"e local"` + F int `xml:"f "` // ERROR "suspicious space found in struct tag value" + G int `xml:" g"` // ERROR "suspicious space found in struct tag value" + H int `xml:"h ,omitempty"` // ERROR "suspicious space found in struct tag value" + I int `xml:"i, omitempty"` // ERROR "suspicious space found in struct tag value" + J int `xml:"j local ,omitempty"` // ERROR "suspicious space found in struct tag value" + K int `xml:"k local, omitempty"` // ERROR "suspicious space found in struct tag value" + L int `xml:" l local,omitempty"` // ERROR "suspicious space found in struct tag value" + M int `xml:"m local,omitempty"` // ERROR "suspicious space found in struct tag value" + N int `xml:" "` // ERROR "suspicious space found in struct tag value" + O int `xml:""` + P int `xml:","` + Q int `foo:" doesn't care "` }