File tree 2 files changed +21
-3
lines changed
2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ type Tag struct {
42
42
func Parse (tag string ) (* Tags , error ) {
43
43
var tags []* Tag
44
44
45
+ hasTag := tag != ""
46
+
45
47
// NOTE(arslan) following code is from reflect and vet package with some
46
48
// modifications to collect all necessary information and extend it with
47
49
// usable methods
@@ -53,7 +55,7 @@ func Parse(tag string) (*Tags, error) {
53
55
}
54
56
tag = tag [i :]
55
57
if tag == "" {
56
- return nil , nil
58
+ break
57
59
}
58
60
59
61
// Scan to colon. A space, a quote or a control character is a syntax
@@ -113,6 +115,10 @@ func Parse(tag string) (*Tags, error) {
113
115
})
114
116
}
115
117
118
+ if hasTag && len (tags ) == 0 {
119
+ return nil , nil
120
+ }
121
+
116
122
return & Tags {
117
123
tags : tags ,
118
124
}, nil
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package structtag
3
3
import (
4
4
"reflect"
5
5
"sort"
6
+ "strings"
6
7
"testing"
7
8
)
8
9
@@ -162,6 +163,16 @@ func TestParse(t *testing.T) {
162
163
},
163
164
},
164
165
},
166
+ {
167
+ name : "tag with trailing space" ,
168
+ tag : `json:"foo" ` ,
169
+ exp : []* Tag {
170
+ {
171
+ Key : "json" ,
172
+ Name : "foo" ,
173
+ },
174
+ },
175
+ },
165
176
}
166
177
167
178
for _ , ts := range test {
@@ -182,8 +193,9 @@ func TestParse(t *testing.T) {
182
193
t .Errorf ("parse\n \t want: %#v\n \t got : %#v" , ts .exp , got )
183
194
}
184
195
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 ())
187
199
}
188
200
})
189
201
}
You can’t perform that action at this time.
0 commit comments