Skip to content

Commit e5265c8

Browse files
authored
ability to convert a mapping (back) to KEY=VALUE strings (#445)
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 45494a8 commit e5265c8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

types/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,16 @@ func NewMapping(values []string) Mapping {
491491
return mapping
492492
}
493493

494+
// convert values into a set of KEY=VALUE strings
495+
func (m Mapping) Values() []string {
496+
values := make([]string, 0, len(m))
497+
for k, v := range m {
498+
values = append(values, fmt.Sprintf("%s=%s", k, v))
499+
}
500+
sort.Strings(values)
501+
return values
502+
}
503+
494504
// ToMappingWithEquals converts Mapping into a MappingWithEquals with pointer references
495505
func (m Mapping) ToMappingWithEquals() MappingWithEquals {
496506
mapping := MappingWithEquals{}

types/types_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,14 @@ func equalTrimSpace(x interface{}, y interface{}) is.Comparison {
368368
}
369369
return is.DeepEqual(trim(x), trim(y))
370370
}
371+
372+
func TestMappingValues(t *testing.T) {
373+
values := []string{"BAR=QIX", "FOO=BAR", "QIX=ZOT"}
374+
mapping := NewMapping(values)
375+
assert.DeepEqual(t, mapping, Mapping{
376+
"FOO": "BAR",
377+
"BAR": "QIX",
378+
"QIX": "ZOT",
379+
})
380+
assert.DeepEqual(t, mapping.Values(), values)
381+
}

0 commit comments

Comments
 (0)