Skip to content

Commit ef42c32

Browse files
committed
sort map keys to prevent inconsistent map lookup
1 parent a43f9d0 commit ef42c32

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/chcol/json.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"database/sql/driver"
2222
"encoding/json"
2323
"fmt"
24+
"slices"
2425
"strings"
2526
)
2627

@@ -53,12 +54,19 @@ func (o *JSON) ValueAtPath(path string) (any, bool) {
5354
func (o *JSON) NestedMap() map[string]any {
5455
result := make(map[string]any)
5556

56-
for key, value := range o.valuesByPath {
57+
sortedPaths := make([]string, 0, len(o.valuesByPath))
58+
for path := range o.valuesByPath {
59+
sortedPaths = append(sortedPaths, path)
60+
}
61+
slices.Sort(sortedPaths)
62+
63+
for _, path := range sortedPaths {
64+
value := o.valuesByPath[path]
5765
if vt, ok := value.(Variant); ok && vt.Nil() {
5866
continue
5967
}
6068

61-
parts := strings.Split(key, ".")
69+
parts := strings.Split(path, ".")
6270
current := result
6371

6472
for i := 0; i < len(parts)-1; i++ {

0 commit comments

Comments
 (0)