Skip to content

Commit 0d978bd

Browse files
engalarclaude
andcommitted
fix: sort translation map iteration in all serializers
Map iteration order is non-deterministic in Go. Sort language keys before serializing Translations in writer_microflow.go, writer_enumeration.go, and writer_pages.go to ensure idempotent output. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 544e14a commit 0d978bd

3 files changed

Lines changed: 33 additions & 16 deletions

File tree

sdk/mpr/writer_enumeration.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package mpr
44

55
import (
66
"fmt"
7+
"sort"
78

89
"github.com/mendixlabs/mxcli/model"
910

@@ -89,15 +90,20 @@ func (w *Writer) serializeEnumeration(enum *model.Enumeration) ([]byte, error) {
8990
}
9091
captionID := generateUUID()
9192

92-
// Build translation items
93+
// Build translation items (sorted for deterministic output)
9394
translationItems := bson.A{int32(3)}
9495
if v.Caption != nil {
95-
for langCode, text := range v.Caption.Translations {
96+
langs := make([]string, 0, len(v.Caption.Translations))
97+
for lang := range v.Caption.Translations {
98+
langs = append(langs, lang)
99+
}
100+
sort.Strings(langs)
101+
for _, langCode := range langs {
96102
translationItems = append(translationItems, bson.M{
97103
"$ID": idToBsonBinary(generateUUID()),
98104
"$Type": "Texts$Translation",
99105
"LanguageCode": langCode,
100-
"Text": text,
106+
"Text": v.Caption.Translations[langCode],
101107
})
102108
}
103109
}

sdk/mpr/writer_microflow.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package mpr
44

55
import (
66
"fmt"
7+
"sort"
78

89
"github.com/mendixlabs/mxcli/model"
910
"github.com/mendixlabs/mxcli/sdk/microflows"
@@ -637,12 +638,18 @@ func serializeTextTemplate(text *model.Text, params []string) bson.D {
637638
if len(text.Translations) > 0 {
638639
var transArray bson.A
639640
transArray = append(transArray, int32(3)) // items marker (3 = has items)
640-
for lang, value := range text.Translations {
641+
// Sort language keys for deterministic output
642+
langs := make([]string, 0, len(text.Translations))
643+
for lang := range text.Translations {
644+
langs = append(langs, lang)
645+
}
646+
sort.Strings(langs)
647+
for _, lang := range langs {
641648
transArray = append(transArray, bson.D{
642649
{Key: "$ID", Value: idToBsonBinary(generateUUID())},
643650
{Key: "$Type", Value: "Texts$Translation"},
644651
{Key: "LanguageCode", Value: lang},
645-
{Key: "Text", Value: value},
652+
{Key: "Text", Value: text.Translations[lang]},
646653
})
647654
}
648655
textDoc = append(textDoc, bson.E{Key: "Items", Value: transArray})

sdk/mpr/writer_pages.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package mpr
44

55
import (
66
"fmt"
7+
"sort"
78

89
"github.com/mendixlabs/mxcli/model"
910
"github.com/mendixlabs/mxcli/sdk/pages"
@@ -218,18 +219,21 @@ func (w *Writer) serializePage(page *pages.Page) ([]byte, error) {
218219
// Items go directly after the version marker, NOT nested in another array
219220
if page.Title != nil {
220221
titleItems := bson.A{int32(3)} // Start with empty marker
221-
hasTranslations := false
222-
for langCode, text := range page.Title.Translations {
223-
if !hasTranslations {
224-
titleItems = bson.A{int32(2)} // First item: change to version 2
225-
hasTranslations = true
222+
if len(page.Title.Translations) > 0 {
223+
titleItems = bson.A{int32(2)} // version 2 for non-empty
224+
langs := make([]string, 0, len(page.Title.Translations))
225+
for lang := range page.Title.Translations {
226+
langs = append(langs, lang)
227+
}
228+
sort.Strings(langs)
229+
for _, langCode := range langs {
230+
titleItems = append(titleItems, bson.D{
231+
{Key: "$ID", Value: idToBsonBinary(generateUUID())},
232+
{Key: "$Type", Value: "Texts$Translation"},
233+
{Key: "LanguageCode", Value: langCode},
234+
{Key: "Text", Value: page.Title.Translations[langCode]},
235+
})
226236
}
227-
titleItems = append(titleItems, bson.D{
228-
{Key: "$ID", Value: idToBsonBinary(generateUUID())},
229-
{Key: "$Type", Value: "Texts$Translation"},
230-
{Key: "LanguageCode", Value: langCode},
231-
{Key: "Text", Value: text},
232-
})
233237
}
234238
titleDoc := bson.D{
235239
{Key: "$ID", Value: idToBsonBinary(string(page.Title.ID))},

0 commit comments

Comments
 (0)