Skip to content

Commit

Permalink
Merge pull request #9 from stephens2424/master
Browse files Browse the repository at this point in the history
ignoring trailing whitespace. fixes #8
  • Loading branch information
fatih authored Dec 6, 2019
2 parents 3878f9f + 3eca0a0 commit 72c9472
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Tag struct {
func Parse(tag string) (*Tags, error) {
var tags []*Tag

hasTag := tag != ""

// NOTE(arslan) following code is from reflect and vet package with some
// modifications to collect all necessary information and extend it with
// usable methods
Expand All @@ -53,7 +55,7 @@ func Parse(tag string) (*Tags, error) {
}
tag = tag[i:]
if tag == "" {
return nil, nil
break
}

// Scan to colon. A space, a quote or a control character is a syntax
Expand Down Expand Up @@ -113,6 +115,10 @@ func Parse(tag string) (*Tags, error) {
})
}

if hasTag && len(tags) == 0 {
return nil, nil
}

return &Tags{
tags: tags,
}, nil
Expand Down
16 changes: 14 additions & 2 deletions tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package structtag
import (
"reflect"
"sort"
"strings"
"testing"
)

Expand Down Expand Up @@ -162,6 +163,16 @@ func TestParse(t *testing.T) {
},
},
},
{
name: "tag with trailing space",
tag: `json:"foo" `,
exp: []*Tag{
{
Key: "json",
Name: "foo",
},
},
},
}

for _, ts := range test {
Expand All @@ -182,8 +193,9 @@ func TestParse(t *testing.T) {
t.Errorf("parse\n\twant: %#v\n\tgot : %#v", ts.exp, got)
}

if ts.tag != tags.String() {
t.Errorf("parse string\n\twant: %#v\n\tgot : %#v", ts.tag, tags.String())
trimmedInput := strings.TrimSpace(ts.tag)
if trimmedInput != tags.String() {
t.Errorf("parse string\n\twant: %#v\n\tgot : %#v", trimmedInput, tags.String())
}
})
}
Expand Down

0 comments on commit 72c9472

Please sign in to comment.