From 93322bc43421f090ca729cfd208cd9029795d004 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Wed, 3 Nov 2021 10:58:33 +0100 Subject: [PATCH] interp: Implement format/0 in jq --- pkg/interp/funcs.go | 13 ------------- pkg/interp/funcs.jq | 2 ++ pkg/interp/internal.jq | 5 +++-- pkg/interp/testdata/decode.fqtest | 4 ++++ 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/pkg/interp/funcs.go b/pkg/interp/funcs.go index 912729fb..d54315c7 100644 --- a/pkg/interp/funcs.go +++ b/pkg/interp/funcs.go @@ -55,7 +55,6 @@ func (i *Interp) makeFunctions() []Function { {[]string{"_decode"}, 2, 2, i._decode, nil}, {[]string{"_is_decode_value"}, 0, 0, i._isDecodeValue, nil}, - {[]string{"format"}, 0, 0, i.format, nil}, {[]string{"_display"}, 1, 1, nil, i._display}, {[]string{"_hexdump"}, 1, 1, nil, i._hexdump}, @@ -651,18 +650,6 @@ func (i *Interp) _isDecodeValue(c interface{}, a []interface{}) interface{} { return ok } -func (i *Interp) format(c interface{}, a []interface{}) interface{} { - cj, ok := c.(gojq.JQValue) - if !ok { - return nil - } - f, ok := cj.JQValueKey("_format").(string) - if !ok { - return nil - } - return f -} - func (i *Interp) _display(c interface{}, a []interface{}) gojq.Iter { opts := i.Options(a[0]) diff --git a/pkg/interp/funcs.jq b/pkg/interp/funcs.jq index 292fdd7e..bc274451 100644 --- a/pkg/interp/funcs.jq +++ b/pkg/interp/funcs.jq @@ -77,6 +77,8 @@ def hexdump: hexdump({display_bytes: 0}); def hd($opts): hexdump($opts); def hd: hexdump; +def format: _decode_value(._format; null); + def root: _decode_value(._root); def buffer_root: _decode_value(._buffer_root); def format_root: _decode_value(._format_root); diff --git a/pkg/interp/internal.jq b/pkg/interp/internal.jq index f1679557..6073f005 100644 --- a/pkg/interp/internal.jq +++ b/pkg/interp/internal.jq @@ -109,10 +109,11 @@ def _is_scalar: def _expected_decode_value: error("expected a decode value but got: \(. | type) (\(. | tostring))"); # TODO: helper? _is_decode_value? -def _decode_value(f): +def _decode_value(f; ef): if _is_decode_value then f - else _expected_decode_value + else ef end; +def _decode_value(f): _decode_value(f; _expected_decode_value); def _is_context_canceled_error: . == "context canceled"; diff --git a/pkg/interp/testdata/decode.fqtest b/pkg/interp/testdata/decode.fqtest index 03b4fcf6..4386a902 100644 --- a/pkg/interp/testdata/decode.fqtest +++ b/pkg/interp/testdata/decode.fqtest @@ -39,6 +39,10 @@ mp3> probe({}) 0x030|c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00|................| * |until 0x283.7 (end) (599) | | | | | footers: [0] +mp3> format +"mp3" +mp3> null | format +null mp3> ^D $ fq -d raw 'png | d' /test.mp3 |00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} (png)