Skip to content

Commit

Permalink
Sort map keys before writing in TsMap
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-klotho committed Nov 29, 2023
1 parent 26c714e commit 9455603
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions pkg/infra/iac3/map_marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iac3

import (
"fmt"
"sort"
"strings"
)

Expand All @@ -26,11 +27,16 @@ type (
func (m TsMap) String() string {
buf := strings.Builder{}
buf.WriteRune('{')
keys := len(m)
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
i := 0
for k, v := range m {
for _, k := range keys {
v := m[k]
buf.WriteString(fmt.Sprintf("%s: %v", k, v))
if i < keys-1 {
if i < len(m)-1 {
buf.WriteString(", ")
}
i++
Expand Down
4 changes: 2 additions & 2 deletions pkg/infra/iac3/map_marshaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func TestTsMap_String(t *testing.T) {
{
name: "map with multiple entries",
m: TsMap{"foo": "bar", "baz": "qux"},
want: "{foo: bar, baz: qux}",
want: "{baz: qux, foo: bar}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.m.String(), tt.want)
assert.Equal(t, tt.want, tt.m.String())
})
}
}
Expand Down

0 comments on commit 9455603

Please sign in to comment.