Skip to content

Commit

Permalink
normalize numbers updating maps and arrays destructively
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jun 25, 2020
1 parent 3e40794 commit 7d442ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
19 changes: 6 additions & 13 deletions normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"math"
"math/big"
"reflect"
"strings"
)

Expand Down Expand Up @@ -66,17 +65,15 @@ func normalizeNumbers(v interface{}) interface{} {
case float32:
return float64(v)
case map[string]interface{}:
u := make(map[string]interface{}, len(v))
for k, v := range v {
u[k] = normalizeNumbers(v)
for k, x := range v {
v[k] = normalizeNumbers(x)
}
return u
return v
case []interface{}:
u := make([]interface{}, len(v))
for i, v := range v {
u[i] = normalizeNumbers(v)
for i, x := range v {
v[i] = normalizeNumbers(x)
}
return u
return v
default:
return v
}
Expand Down Expand Up @@ -140,7 +137,3 @@ func deleteEmpty(v interface{}) interface{} {
return v
}
}

func deepEqual(x, y interface{}) bool {
return reflect.DeepEqual(normalizeNumbers(x), normalizeNumbers(y))
}
3 changes: 2 additions & 1 deletion operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gojq
import (
"math"
"math/big"
"reflect"
"strings"
)

Expand Down Expand Up @@ -369,7 +370,7 @@ func funcOpSub(_, l, r interface{}) interface{} {
for _, v := range l {
var found bool
for _, w := range r {
if deepEqual(v, w) {
if reflect.DeepEqual(normalizeNumbers(v), normalizeNumbers(w)) {
found = true
break
}
Expand Down

0 comments on commit 7d442ff

Please sign in to comment.