Skip to content

Commit

Permalink
Add Value() to Tag, returning raw value (name+options)
Browse files Browse the repository at this point in the history
  • Loading branch information
hchargois committed Jul 30, 2018
1 parent da3d9ab commit 4b694da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 10 additions & 4 deletions tags.go
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions tags_test.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 4b694da

Please sign in to comment.