Skip to content

Commit

Permalink
sort map keys to prevent inconsistent map lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerTorres committed Jan 25, 2025
1 parent a43f9d0 commit ef42c32
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/chcol/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"slices"
"strings"
)

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

for key, value := range o.valuesByPath {
sortedPaths := make([]string, 0, len(o.valuesByPath))
for path := range o.valuesByPath {
sortedPaths = append(sortedPaths, path)
}
slices.Sort(sortedPaths)

for _, path := range sortedPaths {
value := o.valuesByPath[path]
if vt, ok := value.(Variant); ok && vt.Nil() {
continue
}

parts := strings.Split(key, ".")
parts := strings.Split(path, ".")
current := result

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

0 comments on commit ef42c32

Please sign in to comment.