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

Implement Stringer interface for Pairs and KVs #3256

Merged
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
17 changes: 17 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ func (ps Pairs) Values() []string {
return vs
}

func (ps Pairs) String() string {
b := strings.Builder{}
for i, p := range ps {
b.WriteString(p.Name)
b.WriteRune('=')
b.WriteString(p.Value)
if i < len(ps)-1 {
b.WriteString(", ")
}
}
return b.String()
}

// KV is a set of key/value string pairs.
type KV map[string]string

Expand Down Expand Up @@ -239,6 +252,10 @@ func (kv KV) Values() []string {
return kv.SortedPairs().Values()
}

func (kv KV) String() string {
return kv.SortedPairs().String()
}

// Data is the data passed to notification templates and webhook pushes.
//
// End-users should not be exposed to Go's type system, as this will confuse them and prevent
Expand Down
7 changes: 7 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func TestPairValues(t *testing.T) {
require.EqualValues(t, expected, pairs.Values())
}

func TestPairsString(t *testing.T) {
pairs := Pairs{{"name1", "value1"}}
require.Equal(t, "name1=value1", pairs.String())
pairs = append(pairs, Pair{"name2", "value2"})
require.Equal(t, "name1=value1, name2=value2", pairs.String())
}

func TestKVSortedPairs(t *testing.T) {
kv := KV{"d": "dVal", "b": "bVal", "c": "cVal"}

Expand Down