1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 09:56:07 +03:00

interp: Implement format/0 in jq

This commit is contained in:
Mattias Wadman 2021-11-03 10:58:33 +01:00
parent d469edfcbc
commit 93322bc434
4 changed files with 9 additions and 15 deletions

View File

@ -55,7 +55,6 @@ func (i *Interp) makeFunctions() []Function {
{[]string{"_decode"}, 2, 2, i._decode, nil}, {[]string{"_decode"}, 2, 2, i._decode, nil},
{[]string{"_is_decode_value"}, 0, 0, i._isDecodeValue, 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{"_display"}, 1, 1, nil, i._display},
{[]string{"_hexdump"}, 1, 1, nil, i._hexdump}, {[]string{"_hexdump"}, 1, 1, nil, i._hexdump},
@ -651,18 +650,6 @@ func (i *Interp) _isDecodeValue(c interface{}, a []interface{}) interface{} {
return ok 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 { func (i *Interp) _display(c interface{}, a []interface{}) gojq.Iter {
opts := i.Options(a[0]) opts := i.Options(a[0])

View File

@ -77,6 +77,8 @@ def hexdump: hexdump({display_bytes: 0});
def hd($opts): hexdump($opts); def hd($opts): hexdump($opts);
def hd: hexdump; def hd: hexdump;
def format: _decode_value(._format; null);
def root: _decode_value(._root); def root: _decode_value(._root);
def buffer_root: _decode_value(._buffer_root); def buffer_root: _decode_value(._buffer_root);
def format_root: _decode_value(._format_root); def format_root: _decode_value(._format_root);

View File

@ -109,10 +109,11 @@ def _is_scalar:
def _expected_decode_value: def _expected_decode_value:
error("expected a decode value but got: \(. | type) (\(. | tostring))"); error("expected a decode value but got: \(. | type) (\(. | tostring))");
# TODO: helper? _is_decode_value? # TODO: helper? _is_decode_value?
def _decode_value(f): def _decode_value(f; ef):
if _is_decode_value then f if _is_decode_value then f
else _expected_decode_value else ef
end; end;
def _decode_value(f): _decode_value(f; _expected_decode_value);
def _is_context_canceled_error: . == "context canceled"; def _is_context_canceled_error: . == "context canceled";

View File

@ -39,6 +39,10 @@ mp3> probe({})
0x030|c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00|................| 0x030|c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00|................|
* |until 0x283.7 (end) (599) | | * |until 0x283.7 (end) (599) | |
| | | footers: [0] | | | footers: [0]
mp3> format
"mp3"
mp3> null | format
null
mp3> ^D mp3> ^D
$ fq -d raw 'png | d' /test.mp3 $ 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) |00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} (png)