Skip to content

Commit

Permalink
tags: fix escaped quotes
Browse files Browse the repository at this point in the history
fixes: #3
closes: #2
closes: #5
  • Loading branch information
fatih committed Sep 29, 2019
1 parent 2fe4452 commit 3878f9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (t *Tag) Value() string {

// String reassembles the tag into a valid tag field representation
func (t *Tag) String() string {
return fmt.Sprintf(`%s:"%s"`, t.Key, t.Value())
return fmt.Sprintf(`%s:%q`, t.Key, t.Value())
}

// GoString implements the fmt.GoStringer interface
Expand Down
16 changes: 15 additions & 1 deletion tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ func TestParse(t *testing.T) {
},
},
},
{
name: "tag with quoted name",
tag: `json:"foo,bar:\"baz\""`,
exp: []*Tag{
{
Key: "json",
Name: "foo",
Options: []string{`bar:"baz"`},
},
},
},
}

for _, ts := range test {
Expand All @@ -167,10 +178,13 @@ func TestParse(t *testing.T) {
}

got := tags.Tags()

if !reflect.DeepEqual(ts.exp, got) {
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())
}
})
}
}
Expand Down

0 comments on commit 3878f9f

Please sign in to comment.