Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit c83805b

Browse files
authored
fix: iterate over BlueMap in deterministic order (#57)
This is so that outputs are deterministic, which is important for e.g. to check in CI that the 'go generate' output has not changed.
1 parent b072474 commit c83805b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

codegen/blueprint.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"sort"
78
"text/template"
89

910
"github.com/ipld/edelweiss/util/indent"
@@ -48,9 +49,19 @@ type T struct {
4849

4950
type BlueMap map[string]Blueprint
5051

52+
// SortedKeys returns a deterministically-ordered set of keys
53+
func (x BlueMap) SortedKeys() []string {
54+
var keys []string
55+
for k := range x {
56+
keys = append(keys, k)
57+
}
58+
sort.Stable(sort.StringSlice(keys))
59+
return keys
60+
}
61+
5162
func (x BlueMap) Write(ctx GoFileContext, w io.Writer) error {
52-
for _, b := range x {
53-
if err := b.Write(ctx, w); err != nil {
63+
for _, k := range x.SortedKeys() {
64+
if err := x[k].Write(ctx, w); err != nil {
5465
return nil
5566
}
5667
}
@@ -107,7 +118,8 @@ func flattenBlueprint(ctx GoFileContext, b Blueprint) (Blueprint, error) {
107118

108119
func flattenBlueMap(ctx GoFileContext, bm BlueMap) (BlueMap, error) {
109120
r := BlueMap{}
110-
for k, v := range bm {
121+
for _, k := range bm.SortedKeys() {
122+
v := bm[k]
111123
f, err := flattenBlueprint(ctx, v)
112124
if err != nil {
113125
return nil, err

0 commit comments

Comments
 (0)