From 9dc59e5d394cfe433e818ad4b79b0f80038f4904 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Mon, 14 Feb 2022 23:43:49 +0100 Subject: [PATCH] interp: Move _is_decode_value to jq Make _extype work like type --- format/matroska/testdata/path.fqtest | 2 +- format/mp4/testdata/path.fqtest | 2 +- internal/gojqextra/error.go | 30 +++------------------------- internal/gojqextra/types.go | 22 ++++++++++++++++++++ pkg/interp/decode.go | 6 ------ pkg/interp/decode.jq | 5 +++-- pkg/interp/interp.go | 2 +- pkg/interp/testdata/inputs.fqtest | 2 +- pkg/interp/testdata/value.fqtest | 12 +++++------ 9 files changed, 38 insertions(+), 45 deletions(-) diff --git a/format/matroska/testdata/path.fqtest b/format/matroska/testdata/path.fqtest index 944f71be..61aa3aab 100644 --- a/format/matroska/testdata/path.fqtest +++ b/format/matroska/testdata/path.fqtest @@ -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) diff --git a/format/mp4/testdata/path.fqtest b/format/mp4/testdata/path.fqtest index 29f65ff5..892adc56 100644 --- a/format/mp4/testdata/path.fqtest +++ b/format/mp4/testdata/path.fqtest @@ -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) diff --git a/internal/gojqextra/error.go b/internal/gojqextra/error.go index e1cfaed6..dfbe1e24 100644 --- a/internal/gojqextra/error.go +++ b/internal/gojqextra/error.go @@ -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)) - } -} diff --git a/internal/gojqextra/types.go b/internal/gojqextra/types.go index 3561803a..c33c57e1 100644 --- a/internal/gojqextra/types.go +++ b/internal/gojqextra/types.go @@ -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 { diff --git a/pkg/interp/decode.go b/pkg/interp/decode.go index 88451ee1..74706eb8 100644 --- a/pkg/interp/decode.go +++ b/pkg/interp/decode.go @@ -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) diff --git a/pkg/interp/decode.jq b/pkg/interp/decode.jq index a2692a35..b555982a 100644 --- a/pkg/interp/decode.jq +++ b/pkg/interp/decode.jq @@ -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 diff --git a/pkg/interp/interp.go b/pkg/interp/interp.go index 64472d2d..3dc5ca93 100644 --- a/pkg/interp/interp.go +++ b/pkg/interp/interp.go @@ -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{} { diff --git a/pkg/interp/testdata/inputs.fqtest b/pkg/interp/testdata/inputs.fqtest index 8c95f37d..217aa9ef 100644 --- a/pkg/interp/testdata/inputs.fqtest +++ b/pkg/interp/testdata/inputs.fqtest @@ -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" diff --git a/pkg/interp/testdata/value.fqtest b/pkg/interp/testdata/value.fqtest index c9d41739..b9edbbcd 100644 --- a/pkg/interp/testdata/value.fqtest +++ b/pkg/interp/testdata/value.fqtest @@ -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 .