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 (
|
2023-05-15 18:23:59 +03:00
|
|
|
"fmt"
|
2021-10-15 19:19:59 +03:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 18:23:59 +03:00
|
|
|
func ToGoJQValue(v any) (any, error) {
|
|
|
|
return ToGoJQValueFn(v, func(v any) (any, error) {
|
2023-04-28 19:44:24 +03:00
|
|
|
switch v := v.(type) {
|
|
|
|
case gojq.JQValue:
|
2023-05-15 18:23:59 +03:00
|
|
|
return v.JQValueToGoJQ(), nil
|
2023-04-28 19:44:24 +03:00
|
|
|
default:
|
2023-05-15 18:23:59 +03:00
|
|
|
return nil, fmt.Errorf("not a JQValue")
|
2023-04-28 19:44:24 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-15 18:23:59 +03:00
|
|
|
func ToGoJQValueFn(v any, valueFn func(v any) (any, error)) (any, error) {
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := v.(type) {
|
|
|
|
case nil:
|
2023-05-15 18:23:59 +03:00
|
|
|
return vv, nil
|
2021-11-03 19:19:33 +03:00
|
|
|
case bool:
|
2023-05-15 18:23:59 +03:00
|
|
|
return vv, nil
|
2021-11-03 19:19:33 +03:00
|
|
|
case int:
|
2023-05-15 18:23:59 +03:00
|
|
|
return vv, nil
|
2021-11-03 19:19:33 +03:00
|
|
|
case int64:
|
2022-05-28 21:25:20 +03:00
|
|
|
if vv >= math.MinInt && vv <= math.MaxInt {
|
2023-05-15 18:23:59 +03:00
|
|
|
return int(vv), nil
|
2022-05-28 21:25:20 +03:00
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
return big.NewInt(vv), nil
|
2021-11-03 19:19:33 +03:00
|
|
|
case uint64:
|
2022-05-28 21:25:20 +03:00
|
|
|
if vv <= math.MaxInt {
|
2023-05-15 18:23:59 +03:00
|
|
|
return int(vv), nil
|
2022-05-28 21:25:20 +03:00
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
return new(big.Int).SetUint64(vv), nil
|
2021-12-09 19:15:21 +03:00
|
|
|
case float32:
|
2023-05-15 18:23:59 +03:00
|
|
|
return float64(vv), nil
|
2021-11-03 19:19:33 +03:00
|
|
|
case float64:
|
2023-05-15 18:23:59 +03:00
|
|
|
return vv, nil
|
2021-11-03 19:19:33 +03:00
|
|
|
case *big.Int:
|
2022-05-28 21:25:20 +03:00
|
|
|
if vv.IsInt64() {
|
|
|
|
vv := vv.Int64()
|
|
|
|
if vv >= math.MinInt && vv <= math.MaxInt {
|
2023-05-15 18:23:59 +03:00
|
|
|
return int(vv), nil
|
2022-05-28 21:25:20 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
return vv, nil
|
2021-11-03 19:19:33 +03:00
|
|
|
case string:
|
2023-05-15 18:23:59 +03:00
|
|
|
return vv, nil
|
2021-12-09 19:15:21 +03:00
|
|
|
case []byte:
|
2023-05-15 18:23:59 +03:00
|
|
|
return string(vv), nil
|
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 {
|
2023-05-15 18:23:59 +03:00
|
|
|
v, err := ToGoJQValueFn(v, valueFn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
vvs[i] = v
|
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
return vvs, nil
|
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 {
|
2023-05-15 18:23:59 +03:00
|
|
|
v, err := ToGoJQValueFn(v, valueFn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
vvs[k] = v
|
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
return vvs, nil
|
2021-11-03 19:19:33 +03:00
|
|
|
default:
|
2023-05-15 18:23:59 +03:00
|
|
|
nv, err := valueFn(vv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-04-28 19:44:24 +03:00
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
|
|
|
|
return ToGoJQValueFn(nv, valueFn)
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
}
|