File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed
Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ type Tag struct {
4242func Parse (tag string ) (* Tags , error ) {
4343 var tags []* Tag
4444
45+ hasTag := tag != ""
46+
4547 // NOTE(arslan) following code is from reflect and vet package with some
4648 // modifications to collect all necessary information and extend it with
4749 // usable methods
@@ -53,7 +55,7 @@ func Parse(tag string) (*Tags, error) {
5355 }
5456 tag = tag [i :]
5557 if tag == "" {
56- return nil , nil
58+ break
5759 }
5860
5961 // Scan to colon. A space, a quote or a control character is a syntax
@@ -113,6 +115,10 @@ func Parse(tag string) (*Tags, error) {
113115 })
114116 }
115117
118+ if hasTag && len (tags ) == 0 {
119+ return nil , nil
120+ }
121+
116122 return & Tags {
117123 tags : tags ,
118124 }, nil
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package structtag
33import (
44 "reflect"
55 "sort"
6+ "strings"
67 "testing"
78)
89
@@ -162,6 +163,16 @@ func TestParse(t *testing.T) {
162163 },
163164 },
164165 },
166+ {
167+ name : "tag with trailing space" ,
168+ tag : `json:"foo" ` ,
169+ exp : []* Tag {
170+ {
171+ Key : "json" ,
172+ Name : "foo" ,
173+ },
174+ },
175+ },
165176 }
166177
167178 for _ , ts := range test {
@@ -182,8 +193,9 @@ func TestParse(t *testing.T) {
182193 t .Errorf ("parse\n \t want: %#v\n \t got : %#v" , ts .exp , got )
183194 }
184195
185- if ts .tag != tags .String () {
186- t .Errorf ("parse string\n \t want: %#v\n \t got : %#v" , ts .tag , tags .String ())
196+ trimmedInput := strings .TrimSpace (ts .tag )
197+ if trimmedInput != tags .String () {
198+ t .Errorf ("parse string\n \t want: %#v\n \t got : %#v" , trimmedInput , tags .String ())
187199 }
188200 })
189201 }
You can’t perform that action at this time.
0 commit comments