Skip to content

Commit 821fc41

Browse files
committed
XXX: REMOVE: Ugly fix to test sorting
1 parent ae893f9 commit 821fc41

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

pkg/internal/third_party/go-json-experiment/json/arshal_any.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package json
66

7-
import "reflect"
7+
import (
8+
"reflect"
9+
"sort"
10+
)
811

912
// This files contains an optimized marshal and unmarshal implementation
1013
// for the any type. This type is often used when the Go program has
@@ -94,16 +97,22 @@ func marshalObjectAny(mo MarshalOptions, enc *Encoder, obj map[string]any) error
9497
if err := enc.WriteToken(ObjectStart); err != nil {
9598
return err
9699
}
100+
// XXX: Do we need this part? Probably ...
101+
keys := make([]string, 0, len(obj))
102+
for key := range obj {
103+
keys = append(keys, key)
104+
}
105+
sort.Strings(keys)
97106
// A Go map guarantees that each entry has a unique key
98107
// The only possibility of duplicates is due to invalid UTF-8.
99108
if !enc.options.AllowInvalidUTF8 {
100109
enc.tokens.last.disableNamespace()
101110
}
102-
for name, val := range obj {
111+
for _, name := range keys {
103112
if err := enc.WriteToken(String(name)); err != nil {
104113
return err
105114
}
106-
if err := marshalValueAny(mo, enc, val); err != nil {
115+
if err := marshalValueAny(mo, enc, obj[name]); err != nil {
107116
return err
108117
}
109118
}

pkg/internal/third_party/go-json-experiment/json/arshal_default.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"math"
1414
"reflect"
15+
"sort"
1516
"strconv"
1617
"sync"
1718
)
@@ -648,15 +649,20 @@ func makeMapArshaler(t reflect.Type) *arshaler {
648649
// See https://go.dev/cl/400675.
649650
iter := getMapIter(va.Value)
650651
defer putMapIter(iter)
651-
for iter.Next() {
652-
k.SetIterKey(iter)
652+
// XXX: SAD SAD SAD, I have to allocate, cache and sort ...
653+
keys := va.Value.MapKeys()
654+
sort.Slice(keys, func(i, j int) bool {
655+
return keys[i].String() < keys[j].String()
656+
})
657+
for _, key := range keys {
658+
k.Set(key)
653659
if err := marshalKey(mko, enc, k); err != nil {
654660
// TODO: If err is errMissingName, then wrap it as a
655661
// SemanticError since this key type cannot be serialized
656662
// as a JSON string.
657663
return err
658664
}
659-
v.SetIterValue(iter)
665+
v.Set(va.MapIndex(key))
660666
if err := marshalVal(mo, enc, v); err != nil {
661667
return err
662668
}

pkg/internal/third_party/go-json-experiment/json/arshal_inlined.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package json
77
import (
88
"errors"
99
"reflect"
10+
"sort"
1011
)
1112

1213
// This package supports "inlining" a Go struct field, where the contents
@@ -94,8 +95,13 @@ func marshalInlinedFallbackAll(mo MarshalOptions, enc *Encoder, va addressableVa
9495
}
9596
m := v
9697
mv := newAddressableValue(m.Type().Elem())
97-
for iter := m.MapRange(); iter.Next(); {
98-
b, err := appendString(enc.UnusedBuffer(), iter.Key().String(), !enc.options.AllowInvalidUTF8, nil)
98+
99+
keys := m.MapKeys()
100+
sort.Slice(keys, func(i, j int) bool {
101+
return keys[i].String() < keys[j].String()
102+
})
103+
for _, key := range keys {
104+
b, err := appendString(enc.UnusedBuffer(), key.String(), !enc.options.AllowInvalidUTF8, nil)
99105
if err != nil {
100106
return err
101107
}
@@ -110,7 +116,7 @@ func marshalInlinedFallbackAll(mo MarshalOptions, enc *Encoder, va addressableVa
110116
return err
111117
}
112118

113-
mv.Set(iter.Value())
119+
mv.Set(m.MapIndex(key))
114120
marshal := f.fncs.marshal
115121
if mo.Marshalers != nil {
116122
marshal, _ = mo.Marshalers.lookup(marshal, mv.Type())

0 commit comments

Comments
 (0)