mirror of
https://github.com/wader/fq.git
synced 2024-12-23 05:13:30 +03:00
interp: Move _is_decode_value to jq
Make _extype work like type
This commit is contained in:
parent
ba948219f5
commit
9dc59e5d39
2
format/matroska/testdata/path.fqtest
vendored
2
format/matroska/testdata/path.fqtest
vendored
@ -16,4 +16,4 @@ error: not matroska format
|
||||
$ fq -n '1 | matroska_path(".Segment")'
|
||||
exitcode: 5
|
||||
stderr:
|
||||
error: expected a decode value but got: number (1)
|
||||
error: expected decode value but got: number (1)
|
||||
|
2
format/mp4/testdata/path.fqtest
vendored
2
format/mp4/testdata/path.fqtest
vendored
@ -16,4 +16,4 @@ error: not mp4 format
|
||||
$ fq -n '1 | mp4_path(".moov")'
|
||||
exitcode: 5
|
||||
stderr:
|
||||
error: expected a decode value but got: number (1)
|
||||
error: expected decode value but got: number (1)
|
||||
|
@ -2,9 +2,6 @@ package gojqextra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/wader/gojq"
|
||||
)
|
||||
|
||||
// many of these based on errors from gojq
|
||||
@ -17,7 +14,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, Typeof(err.V))
|
||||
}
|
||||
|
||||
type BinopTypeError struct {
|
||||
@ -26,7 +23,7 @@ type BinopTypeError struct {
|
||||
}
|
||||
|
||||
func (err *BinopTypeError) Error() string {
|
||||
return "cannot " + err.Name + ": " + typeof(err.L) + " and " + typeof(err.R)
|
||||
return "cannot " + err.Name + ": " + Typeof(err.L) + " and " + Typeof(err.R)
|
||||
}
|
||||
|
||||
type NonUpdatableTypeError struct {
|
||||
@ -43,7 +40,7 @@ type FuncTypeError struct {
|
||||
V interface{}
|
||||
}
|
||||
|
||||
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: " + Typeof(err.V) }
|
||||
|
||||
type FuncTypeNameError struct {
|
||||
Name string
|
||||
@ -111,24 +108,3 @@ type ArrayIndexTooLargeError struct {
|
||||
func (err *ArrayIndexTooLargeError) Error() string {
|
||||
return fmt.Sprintf("array index too large: %v", err.V)
|
||||
}
|
||||
|
||||
func typeof(v interface{}) 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 []interface{}:
|
||||
return "array"
|
||||
case map[string]interface{}:
|
||||
return "object"
|
||||
case gojq.JQValue:
|
||||
return fmt.Sprintf("JQValue(%s)", v.JQValueType())
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid value: %v", v))
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,34 @@ package gojqextra
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/wader/fq/internal/colorjson"
|
||||
|
||||
"github.com/wader/gojq"
|
||||
)
|
||||
|
||||
func Typeof(v interface{}) 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 []interface{}:
|
||||
return "array"
|
||||
case map[string]interface{}:
|
||||
return "object"
|
||||
case gojq.JQValue:
|
||||
return v.JQValueType()
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid value: %v", v))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: preview errors
|
||||
|
||||
func expectedArrayOrObject(key interface{}, typ string) error {
|
||||
|
@ -27,7 +27,6 @@ func init() {
|
||||
{"_registry", 0, 0, i._registry, nil},
|
||||
{"_tovalue", 1, 1, i._toValue, nil},
|
||||
{"_decode", 2, 2, i._decode, nil},
|
||||
{"_is_decode_value", 0, 0, i._isDecodeValue, nil},
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -229,11 +228,6 @@ func (i *Interp) _decode(c interface{}, a []interface{}) interface{} {
|
||||
return makeDecodeValue(dv)
|
||||
}
|
||||
|
||||
func (i *Interp) _isDecodeValue(c interface{}, a []interface{}) interface{} {
|
||||
_, ok := c.(DecodeValue)
|
||||
return ok
|
||||
}
|
||||
|
||||
func valueKey(name string, a, b func(name string) interface{}) interface{} {
|
||||
if strings.HasPrefix(name, "_") {
|
||||
return a(name)
|
||||
|
@ -1,7 +1,8 @@
|
||||
# TODO: error value preview
|
||||
def _expected_decode_value:
|
||||
error("expected a decode value but got: \(. | type) (\(. | tostring))");
|
||||
# TODO: helper? _is_decode_value?
|
||||
error("expected decode value but got: \(. | type) (\(. | tostring))");
|
||||
def _is_decode_value: _exttype == "decode_value";
|
||||
|
||||
def _decode_value(f; ef):
|
||||
if _is_decode_value then f
|
||||
else ef
|
||||
|
@ -546,7 +546,7 @@ func (i *Interp) _extType(c interface{}, a []interface{}) interface{} {
|
||||
if v, ok := c.(Value); ok {
|
||||
return v.ExtType()
|
||||
}
|
||||
return nil
|
||||
return gojqextra.Typeof(c)
|
||||
}
|
||||
|
||||
func (i *Interp) makeStateFn(state *interface{}) func(c interface{}, a []interface{}) interface{} {
|
||||
|
2
pkg/interp/testdata/inputs.fqtest
vendored
2
pkg/interp/testdata/inputs.fqtest
vendored
@ -20,7 +20,7 @@ exitcode: 5
|
||||
stderr:
|
||||
error: break
|
||||
$ fq -d raw -n '(.,inputs) | try todescription catch .' /a /b /c
|
||||
"expected a decode value but got: null (null)"
|
||||
"expected decode value but got: null (null)"
|
||||
"/a"
|
||||
"/b"
|
||||
"/c"
|
||||
|
12
pkg/interp/testdata/value.fqtest
vendored
12
pkg/interp/testdata/value.fqtest
vendored
@ -45,7 +45,7 @@ mp3> . | (root, buffer_root, format_root, parent | try topath | path_to_expr cat
|
||||
"."
|
||||
"."
|
||||
"."
|
||||
"expected a decode value but got: null (null)"
|
||||
"expected decode value but got: null (null)"
|
||||
[]
|
||||
mp3> .headers[0].magic | (root, buffer_root, format_root, parent | topath | path_to_expr), [parents | topath | path_to_expr]
|
||||
"."
|
||||
@ -70,15 +70,15 @@ mp3> .frames[0].side_info.granules[1] | (root, buffer_root, format_root, parent
|
||||
"."
|
||||
]
|
||||
mp3> 123 | root
|
||||
error: expected a decode value but got: number (123)
|
||||
error: expected decode value but got: number (123)
|
||||
mp3> 123 | buffer_root
|
||||
error: expected a decode value but got: number (123)
|
||||
error: expected decode value but got: number (123)
|
||||
mp3> 123 | format_root
|
||||
error: expected a decode value but got: number (123)
|
||||
error: expected decode value but got: number (123)
|
||||
mp3> 123 | parent
|
||||
error: expected a decode value but got: number (123)
|
||||
error: expected decode value but got: number (123)
|
||||
mp3> 123 | parents
|
||||
error: expected a decode value but got: number (123)
|
||||
error: expected decode value but got: number (123)
|
||||
mp3> ^D
|
||||
$ fq -d mp3 -i . /test.mp3
|
||||
mp3> . as $c | ("headers", 0, "abc") as $n | $n, try ($c | has($n)) catch .
|
||||
|
Loading…
Reference in New Issue
Block a user