Skip to content

Commit 76ae1d6

Browse files
authored
Merge pull request #1 from hchargois/master
Add Value() to Tag, returning raw value (name+options)
2 parents da3d9ab + 4b694da commit 76ae1d6

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

tags.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,19 @@ func (t *Tag) HasOption(opt string) bool {
265265
return false
266266
}
267267

268-
// String reassembles the tag into a valid tag field representation
269-
func (t *Tag) String() string {
268+
// Value returns the raw value of the tag, i.e. if the tag is
269+
// `json:"foo,omitempty", the Value is "foo,omitempty"
270+
func (t *Tag) Value() string {
270271
options := strings.Join(t.Options, ",")
271272
if options != "" {
272-
return fmt.Sprintf(`%s:"%s,%s"`, t.Key, t.Name, options)
273+
return fmt.Sprintf(`%s,%s`, t.Name, options)
273274
}
274-
return fmt.Sprintf(`%s:"%s"`, t.Key, t.Name)
275+
return t.Name
276+
}
277+
278+
// String reassembles the tag into a valid tag field representation
279+
func (t *Tag) String() string {
280+
return fmt.Sprintf(`%s:"%s"`, t.Key, t.Value())
275281
}
276282

277283
// GoString implements the fmt.GoStringer interface

tags_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,18 @@ func TestTags_Get(t *testing.T) {
188188
t.Fatal(err)
189189
}
190190

191-
want := `json:"foo,omitempty"`
192-
193-
if found.String() != want {
194-
t.Errorf("get\n\twant: %#v\n\tgot : %#v", want, found.String())
195-
}
191+
t.Run("String", func(t *testing.T) {
192+
want := `json:"foo,omitempty"`
193+
if found.String() != want {
194+
t.Errorf("get\n\twant: %#v\n\tgot : %#v", want, found.String())
195+
}
196+
})
197+
t.Run("Value", func(t *testing.T) {
198+
want := `foo,omitempty`
199+
if found.Value() != want {
200+
t.Errorf("get\n\twant: %#v\n\tgot : %#v", want, found.Value())
201+
}
202+
})
196203
}
197204

198205
func TestTags_Set(t *testing.T) {

0 commit comments

Comments
 (0)