Skip to content

Commit

Permalink
gojq: Update rebased fq fork
Browse files Browse the repository at this point in the history
Use new gojq.TypeOf and gojq.Preview
  • Loading branch information
wader committed May 23, 2022
1 parent 058dc03 commit 9a7ce14
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 18 additions & 3 deletions internal/gojqextra/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gojqextra

import (
"fmt"

"github.com/wader/gojq"
)

// many of these based on errors from gojq
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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) + ")"
}
}
24 changes: 0 additions & 24 deletions internal/gojqextra/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions pkg/interp/testdata/bitops.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@ 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
18446744073709551615
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
4660
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
1208925819614629174645300
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
1208925819614629174701515
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

0 comments on commit 9a7ce14

Please sign in to comment.