Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignoring trailing whitespace. fixes #8 #9

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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