Skip to content

Commit 72c9472

Browse files
authored
Merge pull request #9 from stephens2424/master
ignoring trailing whitespace. fixes #8
2 parents 3878f9f + 3eca0a0 commit 72c9472

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

tags.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type Tag struct {
4242
func 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

tags_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package structtag
33
import (
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\twant: %#v\n\tgot : %#v", ts.exp, got)
183194
}
184195

185-
if ts.tag != tags.String() {
186-
t.Errorf("parse string\n\twant: %#v\n\tgot : %#v", ts.tag, tags.String())
196+
trimmedInput := strings.TrimSpace(ts.tag)
197+
if trimmedInput != tags.String() {
198+
t.Errorf("parse string\n\twant: %#v\n\tgot : %#v", trimmedInput, tags.String())
187199
}
188200
})
189201
}

0 commit comments

Comments
 (0)