Skip to content

Commit b26eec1

Browse files
committed
fix: ensure deterministic map iteration during variable interpolation
Refactored to use slices.Sorted(maps.Keys()) as suggested by reviewer. This is more idiomatic Go than manual slice creation and sorting. Fixes: docker/compose#13712 Signed-off-by: Md Yunus <admin@yunuscollege.eu.org>
1 parent 038312c commit b26eec1

1 file changed

Lines changed: 4 additions & 11 deletions

File tree

interpolation/interpolation.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package interpolation
1818

1919
import (
20-
"sort"
2120
"errors"
2221
"fmt"
22+
"maps"
2323
"os"
24+
"slices"
2425

2526
"github.com/compose-spec/compose-go/v2/template"
2627
"github.com/compose-spec/compose-go/v2/tree"
@@ -59,11 +60,7 @@ func Interpolate(config map[string]interface{}, opts Options) (map[string]interf
5960

6061
out := map[string]interface{}{}
6162

62-
keys := make([]string, 0, len(config))
63-
for key := range config {
64-
keys = append(keys, key)
65-
}
66-
sort.Strings(keys)
63+
keys := slices.Sorted(maps.Keys(config))
6764
for _, key := range keys {
6865
value := config[key]
6966
interpolatedValue, err := recursiveInterpolate(value, tree.NewPath(key), opts)
@@ -95,11 +92,7 @@ func recursiveInterpolate(value interface{}, path tree.Path, opts Options) (inte
9592

9693
case map[string]interface{}:
9794
out := map[string]interface{}{}
98-
keys := make([]string, 0, len(value))
99-
for key := range value {
100-
keys = append(keys, key)
101-
}
102-
sort.Strings(keys)
95+
keys := slices.Sorted(maps.Keys(value))
10396
for _, key := range keys {
10497
elem := value[key]
10598
interpolatedElem, err := recursiveInterpolate(elem, path.Next(key), opts)

0 commit comments

Comments
 (0)