mirror of
https://github.com/wader/fq.git
synced 2024-11-23 00:57:15 +03:00
Merge pull request #276 from wader/gojq-update
gojq: Update rebased fq fork
This commit is contained in:
commit
e59fe499fb
2
go.mod
2
go.mod
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
// fork of github.com/itchyny/gojq, see github.com/wader/gojq fq branch
|
||||
github.com/wader/gojq v0.12.1-0.20220523090556-421507c088ee
|
||||
github.com/wader/gojq v0.12.1-0.20220523192338-6ad601ba3ad4
|
||||
// fork of github.com/chzyer/readline, see github.com/wader/readline fq branch
|
||||
github.com/wader/readline v0.0.0-20220519145246-cc6b808370a7
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -15,8 +15,8 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
|
||||
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/wader/gojq v0.12.1-0.20220523090556-421507c088ee h1:Iz4p9TR23Y5MUGOp1Ju7ObNKZQLTyY//9Gi/xUyCeA8=
|
||||
github.com/wader/gojq v0.12.1-0.20220523090556-421507c088ee/go.mod h1:Pq2wrnwmiGxsaT62vOTEXkH3J3tI81VHJ2K2ZePP6oI=
|
||||
github.com/wader/gojq v0.12.1-0.20220523192338-6ad601ba3ad4 h1:tVeOphgXlGRx0s92DZirhEXUI+6PymV/aHUWFx7ZCcQ=
|
||||
github.com/wader/gojq v0.12.1-0.20220523192338-6ad601ba3ad4/go.mod h1:Pq2wrnwmiGxsaT62vOTEXkH3J3tI81VHJ2K2ZePP6oI=
|
||||
github.com/wader/readline v0.0.0-20220519145246-cc6b808370a7 h1:dzXHW2D21gXwLUYEcauxccmtfUVkOoB/zJ9pqX00FiA=
|
||||
github.com/wader/readline v0.0.0-20220519145246-cc6b808370a7/go.mod h1:7FWZ5rXtHk67PmaVWEAUAVNaqdEqq3H6T75m+sPlx+w=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
|
@ -2,6 +2,8 @@ package gojqextra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/wader/gojq"
|
||||
)
|
||||
|
||||
// many of these based on errors from gojq
|
||||
@ -14,7 +16,7 @@ type UnaryTypeError struct {
|
||||
}
|
||||
|
||||
func (err *UnaryTypeError) Error() string {
|
||||
return fmt.Sprintf("cannot %s: %s", err.Name, Typeof(err.V))
|
||||
return fmt.Sprintf("cannot %s: %s", err.Name, typeErrorPreview(err.V))
|
||||
}
|
||||
|
||||
type BinopTypeError struct {
|
||||
@ -23,7 +25,7 @@ type BinopTypeError struct {
|
||||
}
|
||||
|
||||
func (err *BinopTypeError) Error() string {
|
||||
return "cannot " + err.Name + ": " + Typeof(err.L) + " and " + Typeof(err.R)
|
||||
return "cannot " + err.Name + ": " + typeErrorPreview(err.L) + " and " + typeErrorPreview(err.R)
|
||||
}
|
||||
|
||||
type NonUpdatableTypeError struct {
|
||||
@ -40,7 +42,9 @@ type FuncTypeError struct {
|
||||
V any
|
||||
}
|
||||
|
||||
func (err FuncTypeError) Error() string { return err.Name + " cannot be applied to: " + Typeof(err.V) }
|
||||
func (err FuncTypeError) Error() string {
|
||||
return err.Name + " cannot be applied to: " + typeErrorPreview(err.V)
|
||||
}
|
||||
|
||||
type FuncTypeNameError struct {
|
||||
Name string
|
||||
@ -108,3 +112,14 @@ type ArrayIndexTooLargeError struct {
|
||||
func (err *ArrayIndexTooLargeError) Error() string {
|
||||
return fmt.Sprintf("array index too large: %v", err.V)
|
||||
}
|
||||
|
||||
func typeErrorPreview(v interface{}) string {
|
||||
switch v.(type) {
|
||||
case nil:
|
||||
return "null"
|
||||
case gojq.Iter:
|
||||
return "gojq.Iter"
|
||||
default:
|
||||
return gojq.TypeOf(v) + " (" + gojq.Preview(v) + ")"
|
||||
}
|
||||
}
|
||||
|
@ -4,36 +4,12 @@ package gojqextra
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/wader/fq/internal/colorjson"
|
||||
|
||||
"github.com/wader/gojq"
|
||||
)
|
||||
|
||||
func Typeof(v any) string {
|
||||
switch v := v.(type) {
|
||||
case nil:
|
||||
return "null"
|
||||
case bool:
|
||||
return "boolean"
|
||||
case int, float64, *big.Int:
|
||||
return "number"
|
||||
case string:
|
||||
return "string"
|
||||
case []any:
|
||||
return "array"
|
||||
case map[string]any:
|
||||
return "object"
|
||||
case gojq.JQValue:
|
||||
return v.JQValueType()
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid value: %v", v))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: preview errors
|
||||
|
||||
// array
|
||||
|
||||
var _ gojq.JQValue = Array{}
|
||||
|
@ -552,7 +552,7 @@ func (i *Interp) _extType(c any, a []any) any {
|
||||
if v, ok := c.(Value); ok {
|
||||
return v.ExtType()
|
||||
}
|
||||
return gojqextra.Typeof(c)
|
||||
return gojq.TypeOf(c)
|
||||
}
|
||||
|
||||
func (i *Interp) makeStateFn(state *any) func(c any, a []any) any {
|
||||
|
10
pkg/interp/testdata/bitops.fqtest
vendored
10
pkg/interp/testdata/bitops.fqtest
vendored
@ -15,7 +15,7 @@ null> [0,0], [8,1], [0xffff_ffff_ffff_ffff,1] | bsl(.[0]; .[1])
|
||||
null> bsl(1)
|
||||
error: expr: function not defined: bsl/1
|
||||
null> bsl(null; 1)
|
||||
error: cannot bsl: null and number
|
||||
error: cannot bsl: null and number (1)
|
||||
null> [0,0], [8,1], [0x1_ffff_ffff_ffff_fffe,1] | bsr(.[0]; .[1])
|
||||
0
|
||||
4
|
||||
@ -23,7 +23,7 @@ null> [0,0], [8,1], [0x1_ffff_ffff_ffff_fffe,1] | bsr(.[0]; .[1])
|
||||
null> bsr(1)
|
||||
error: expr: function not defined: bsr/1
|
||||
null> bsr(null; 1)
|
||||
error: cannot bsr: null and number
|
||||
error: cannot bsr: null and number (1)
|
||||
null> [0,0], [0xffff_ffff_ffff_ffff_ffff,0x1234], [0x1234,0xffff_ffff_ffff_ffff_ffff,0x1234] | band(.[0]; .[1])
|
||||
0
|
||||
4660
|
||||
@ -31,7 +31,7 @@ null> [0,0], [0xffff_ffff_ffff_ffff_ffff,0x1234], [0x1234,0xffff_ffff_ffff_ffff_
|
||||
null> band(1)
|
||||
error: expr: function not defined: band/1
|
||||
null> band(null; 1)
|
||||
error: cannot band: null and number
|
||||
error: cannot band: null and number (1)
|
||||
null> [0,0], [0xffff_ffff_ffff_ffff_0000,0x1234], [0x1234,0xffff_ffff_ffff_ffff_0000,0x1234] | bor(.[0]; .[1])
|
||||
0
|
||||
1208925819614629174645300
|
||||
@ -39,7 +39,7 @@ null> [0,0], [0xffff_ffff_ffff_ffff_0000,0x1234], [0x1234,0xffff_ffff_ffff_ffff_
|
||||
null> bor(1)
|
||||
error: expr: function not defined: bor/1
|
||||
null> bor(null; 1)
|
||||
error: cannot bor: null and number
|
||||
error: cannot bor: null and number (1)
|
||||
null> [0,0], [0xffff_ffff_ffff_ffff_ffff,0x1234], [0x1234,0xffff_ffff_ffff_ffff_ffff,0x1234] | bxor(.[0]; .[1])
|
||||
0
|
||||
1208925819614629174701515
|
||||
@ -47,5 +47,5 @@ null> [0,0], [0xffff_ffff_ffff_ffff_ffff,0x1234], [0x1234,0xffff_ffff_ffff_ffff_
|
||||
null> bxor(1)
|
||||
error: expr: function not defined: bxor/1
|
||||
null> bxor(null; 1)
|
||||
error: cannot bxor: null and number
|
||||
error: cannot bxor: null and number (1)
|
||||
null> ^D
|
||||
|
Loading…
Reference in New Issue
Block a user