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:
parent
d469edfcbc
commit
93322bc434
@ -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])
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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";
|
||||
|
||||
|
4
pkg/interp/testdata/decode.fqtest
vendored
4
pkg/interp/testdata/decode.fqtest
vendored
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user