From 4b694dae447b9a8be4a39666c9437b6dcc510850 Mon Sep 17 00:00:00 2001 From: Hugo Chargois Date: Mon, 30 Jul 2018 17:05:32 +0200 Subject: [PATCH] Add Value() to Tag, returning raw value (name+options) --- tags.go | 14 ++++++++++---- tags_test.go | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/tags.go b/tags.go index 915d576..be28a98 100644 --- a/tags.go +++ b/tags.go @@ -265,13 +265,19 @@ func (t *Tag) HasOption(opt string) bool { return false } -// String reassembles the tag into a valid tag field representation -func (t *Tag) String() string { +// Value returns the raw value of the tag, i.e. if the tag is +// `json:"foo,omitempty", the Value is "foo,omitempty" +func (t *Tag) Value() string { options := strings.Join(t.Options, ",") if options != "" { - return fmt.Sprintf(`%s:"%s,%s"`, t.Key, t.Name, options) + return fmt.Sprintf(`%s,%s`, t.Name, options) } - return fmt.Sprintf(`%s:"%s"`, t.Key, t.Name) + return t.Name +} + +// String reassembles the tag into a valid tag field representation +func (t *Tag) String() string { + return fmt.Sprintf(`%s:"%s"`, t.Key, t.Value()) } // GoString implements the fmt.GoStringer interface diff --git a/tags_test.go b/tags_test.go index 06bc0b6..318061f 100644 --- a/tags_test.go +++ b/tags_test.go @@ -188,11 +188,18 @@ func TestTags_Get(t *testing.T) { t.Fatal(err) } - want := `json:"foo,omitempty"` - - if found.String() != want { - t.Errorf("get\n\twant: %#v\n\tgot : %#v", want, found.String()) - } + t.Run("String", func(t *testing.T) { + want := `json:"foo,omitempty"` + if found.String() != want { + t.Errorf("get\n\twant: %#v\n\tgot : %#v", want, found.String()) + } + }) + t.Run("Value", func(t *testing.T) { + want := `foo,omitempty` + if found.Value() != want { + t.Errorf("get\n\twant: %#v\n\tgot : %#v", want, found.Value()) + } + }) } func TestTags_Set(t *testing.T) {