Skip to content

Commit

Permalink
interp: Implement format/0 in jq
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Nov 3, 2021
1 parent d469edf commit 93322bc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
13 changes: 0 additions & 13 deletions pkg/interp/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},

Expand Down Expand Up @@ -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])

Expand Down
2 changes: 2 additions & 0 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions pkg/interp/internal.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
4 changes: 4 additions & 0 deletions pkg/interp/testdata/decode.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 93322bc

Please sign in to comment.