2021-10-15 19:19:59 +03:00
|
|
|
// Some of these functions are based on gojq func.go functions
|
|
|
|
// TODO: maybe should be exported from gojq fq branch instead?
|
|
|
|
// The MIT License (MIT)
|
|
|
|
// Copyright (c) 2019-2021 itchyny
|
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
package gojqex
|
2021-10-15 19:19:59 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/wader/gojq"
|
|
|
|
)
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func IsNull(x any) bool {
|
2021-10-15 19:19:59 +03:00
|
|
|
switch x := x.(type) {
|
|
|
|
case nil:
|
|
|
|
return true
|
|
|
|
case gojq.JQValue:
|
|
|
|
return IsNull(x.JQValueToGoJQ())
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func ToGoJQValue(v any) (any, bool) {
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := v.(type) {
|
|
|
|
case nil:
|
|
|
|
return vv, true
|
|
|
|
case bool:
|
|
|
|
return vv, true
|
|
|
|
case int:
|
|
|
|
return vv, true
|
|
|
|
case int64:
|
2022-05-28 21:25:20 +03:00
|
|
|
if vv >= math.MinInt && vv <= math.MaxInt {
|
|
|
|
return int(vv), true
|
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
return big.NewInt(vv), true
|
|
|
|
case uint64:
|
2022-05-28 21:25:20 +03:00
|
|
|
if vv <= math.MaxInt {
|
|
|
|
return int(vv), true
|
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
return new(big.Int).SetUint64(vv), true
|
2021-12-09 19:15:21 +03:00
|
|
|
case float32:
|
|
|
|
return float64(vv), true
|
2021-11-03 19:19:33 +03:00
|
|
|
case float64:
|
|
|
|
return vv, true
|
|
|
|
case *big.Int:
|
2022-05-28 21:25:20 +03:00
|
|
|
if vv.IsInt64() {
|
|
|
|
vv := vv.Int64()
|
|
|
|
if vv >= math.MinInt && vv <= math.MaxInt {
|
|
|
|
return int(vv), true
|
|
|
|
}
|
|
|
|
return vv, true
|
|
|
|
} else if vv.IsUint64() {
|
|
|
|
vv := vv.Uint64()
|
|
|
|
if vv <= math.MaxInt {
|
|
|
|
return int(vv), true
|
|
|
|
}
|
|
|
|
return vv, true
|
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
return vv, true
|
|
|
|
case string:
|
|
|
|
return vv, true
|
2021-12-09 19:15:21 +03:00
|
|
|
case []byte:
|
|
|
|
return string(vv), true
|
2021-11-03 19:19:33 +03:00
|
|
|
case gojq.JQValue:
|
|
|
|
return ToGoJQValue(vv.JQValueToGoJQ())
|
2022-05-20 16:10:41 +03:00
|
|
|
case []any:
|
|
|
|
vvs := make([]any, len(vv))
|
2021-11-03 19:19:33 +03:00
|
|
|
for i, v := range vv {
|
|
|
|
v, ok := ToGoJQValue(v)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
vvs[i] = v
|
|
|
|
}
|
|
|
|
return vvs, true
|
2022-05-20 16:10:41 +03:00
|
|
|
case map[string]any:
|
|
|
|
vvs := make(map[string]any, len(vv))
|
2021-11-03 19:19:33 +03:00
|
|
|
for k, v := range vv {
|
|
|
|
v, ok := ToGoJQValue(v)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
vvs[k] = v
|
|
|
|
}
|
|
|
|
return vvs, true
|
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|