mirror of
https://github.com/wader/fq.git
synced 2025-01-08 23:59:50 +03:00
gojqextra: Cleanup gojq type cast code
This commit is contained in:
parent
f37ac864bd
commit
32361deefe
@ -1,12 +0,0 @@
|
||||
// from gojq
|
||||
// The MIT License (MIT)
|
||||
// Copyright (c) 2019-2021 itchyny
|
||||
|
||||
package gojqextra
|
||||
|
||||
import "math/bits"
|
||||
|
||||
const (
|
||||
maxInt = 1<<(bits.UintSize-1) - 1 // math.MaxInt64 or math.MaxInt32
|
||||
minInt = -maxInt - 1 // math.MinInt64 or math.MinInt32
|
||||
)
|
@ -8,55 +8,10 @@ package gojqextra
|
||||
import (
|
||||
"math"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
"github.com/wader/gojq"
|
||||
)
|
||||
|
||||
func ToString(x any) (string, bool) {
|
||||
switch x := x.(type) {
|
||||
case string:
|
||||
return x, true
|
||||
case gojq.JQValue:
|
||||
return ToString(x.JQValueToGoJQ())
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func ToObject(x any) (map[string]any, bool) {
|
||||
switch x := x.(type) {
|
||||
case map[string]any:
|
||||
return x, true
|
||||
case gojq.JQValue:
|
||||
return ToObject(x.JQValueToGoJQ())
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
func ToArray(x any) ([]any, bool) {
|
||||
switch x := x.(type) {
|
||||
case []any:
|
||||
return x, true
|
||||
case gojq.JQValue:
|
||||
return ToArray(x.JQValueToGoJQ())
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
func ToBoolean(x any) (bool, bool) {
|
||||
switch x := x.(type) {
|
||||
case bool:
|
||||
return x, true
|
||||
case gojq.JQValue:
|
||||
return ToBoolean(x.JQValueToGoJQ())
|
||||
default:
|
||||
return false, false
|
||||
}
|
||||
}
|
||||
|
||||
func IsNull(x any) bool {
|
||||
switch x := x.(type) {
|
||||
case nil:
|
||||
@ -68,66 +23,6 @@ func IsNull(x any) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func ToInt(x any) (int, bool) {
|
||||
switch x := x.(type) {
|
||||
case int:
|
||||
return x, true
|
||||
case float64:
|
||||
return floatToInt(x), true
|
||||
case *big.Int:
|
||||
if x.IsInt64() {
|
||||
if i := x.Int64(); minInt <= i && i <= maxInt {
|
||||
return int(i), true
|
||||
}
|
||||
}
|
||||
if x.Sign() > 0 {
|
||||
return maxInt, true
|
||||
}
|
||||
return minInt, true
|
||||
case gojq.JQValue:
|
||||
return ToInt(x.JQValueToGoJQ())
|
||||
default:
|
||||
// nil and other should fail, "null | tonumber" in jq is an error
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func floatToInt(x float64) int {
|
||||
if minInt <= x && x <= maxInt {
|
||||
return int(x)
|
||||
}
|
||||
if x > 0 {
|
||||
return maxInt
|
||||
}
|
||||
return minInt
|
||||
}
|
||||
|
||||
func ToFloat(x any) (float64, bool) {
|
||||
switch x := x.(type) {
|
||||
case int:
|
||||
return float64(x), true
|
||||
case float64:
|
||||
return x, true
|
||||
case *big.Int:
|
||||
return bigToFloat(x), true
|
||||
case gojq.JQValue:
|
||||
return ToFloat(x.JQValueToGoJQ())
|
||||
default:
|
||||
// nil and other should fail, "null | tonumber" in jq is an error
|
||||
return 0.0, false
|
||||
}
|
||||
}
|
||||
|
||||
func bigToFloat(x *big.Int) float64 {
|
||||
if x.IsInt64() {
|
||||
return float64(x.Int64())
|
||||
}
|
||||
if f, err := strconv.ParseFloat(x.String(), 64); err == nil {
|
||||
return f
|
||||
}
|
||||
return math.Inf(x.Sign())
|
||||
}
|
||||
|
||||
func ToGoJQValue(v any) (any, bool) {
|
||||
switch vv := v.(type) {
|
||||
case nil:
|
||||
|
@ -113,15 +113,15 @@ func toBitReaderEx(v any, inArray bool) (bitio.ReaderAtSeeker, error) {
|
||||
|
||||
// note is used to implement tobytes* also
|
||||
func (i *Interp) _toBits(c any, a []any) any {
|
||||
unit, ok := gojqextra.ToInt(a[0])
|
||||
unit, ok := gojqextra.Cast[int](a[0])
|
||||
if !ok {
|
||||
return gojqextra.FuncTypeError{Name: "_tobits", V: a[0]}
|
||||
}
|
||||
keepRange, ok := gojqextra.ToBoolean(a[1])
|
||||
keepRange, ok := gojqextra.Cast[bool](a[1])
|
||||
if !ok {
|
||||
return gojqextra.FuncTypeError{Name: "_tobits", V: a[1]}
|
||||
}
|
||||
padToUnits, ok := gojqextra.ToInt(a[2])
|
||||
padToUnits, ok := gojqextra.Cast[int](a[2])
|
||||
if !ok {
|
||||
return gojqextra.FuncTypeError{Name: "_tobits", V: a[2]}
|
||||
}
|
||||
|
@ -893,15 +893,15 @@ func init() {
|
||||
toURLValues := func(c map[string]any) url.Values {
|
||||
qv := url.Values{}
|
||||
for k, v := range c {
|
||||
if va, ok := gojqextra.ToArray(v); ok {
|
||||
if va, ok := gojqextra.Cast[[]any](v); ok {
|
||||
var ss []string
|
||||
for _, s := range va {
|
||||
if s, ok := gojqextra.ToString(s); ok {
|
||||
if s, ok := gojqextra.Cast[string](s); ok {
|
||||
ss = append(ss, s)
|
||||
}
|
||||
}
|
||||
qv[k] = ss
|
||||
} else if vs, ok := gojqextra.ToString(v); ok {
|
||||
} else if vs, ok := gojqextra.Cast[string](v); ok {
|
||||
qv[k] = []string{vs}
|
||||
}
|
||||
}
|
||||
|
@ -578,7 +578,7 @@ func (i *Interp) makeStdioFn(name string, t Terminal) func(c any, a []any) gojq.
|
||||
if !ok {
|
||||
return gojq.NewIter(fmt.Errorf("%s is not readable", name))
|
||||
}
|
||||
l, ok := gojqextra.ToInt(a[0])
|
||||
l, ok := gojqextra.Cast[int](a[0])
|
||||
if !ok {
|
||||
return gojq.NewIter(gojqextra.FuncTypeError{Name: name, V: a[0]})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user