1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +03:00

interp: Add internal _is_<type> helpers

This commit is contained in:
Mattias Wadman 2022-05-20 16:38:28 +02:00
parent e1dc5c5737
commit 95e619659b
8 changed files with 32 additions and 26 deletions

View File

@ -84,6 +84,6 @@ def duration:
) )
] | join("") ] | join("")
end; end;
if . | type == "string" then _string if type == "string" then _string
elif . | type == "number" then _number elif type == "number" then _number
else error("expected string or number") end; else error("expected string or number") end;

View File

@ -75,7 +75,7 @@ def _eval_query_rewrite($opts):
# TODO: better way? what about nested eval errors? # TODO: better way? what about nested eval errors?
def _eval_is_compile_error: def _eval_is_compile_error:
type == "object" and .error != null and .what != null; _is_object and .error != null and .what != null;
def _eval_compile_error_tostring: def _eval_compile_error_tostring:
[ (.filename // "expr") [ (.filename // "expr")
, if .line != 1 or .column != 0 then "\(.line):\(.column)" , if .line != 1 or .column != 0 then "\(.line):\(.column)"

View File

@ -9,7 +9,7 @@ def display($opts):
( options($opts) as $opts ( options($opts) as $opts
| if _can_display then _display($opts) | if _can_display then _display($opts)
else else
( if type == "string" and $opts.raw_string then print ( if _is_string and $opts.raw_string then print
else _print_color_json($opts) else _print_color_json($opts)
end end
, ( $opts.join_string , ( $opts.join_string
@ -302,8 +302,8 @@ def tojq($style):
} as $styles } as $styles
| _f( | _f(
( $style // "compact" ( $style // "compact"
| if type == "string" then $styles[.] | if _is_string then $styles[.]
elif type == "object" then . elif _is_object then .
else error("invalid style") else error("invalid style")
end end
) )

View File

@ -8,7 +8,7 @@ def grep_by(f):
); );
def _value_grep_string_cond($v; $flags): def _value_grep_string_cond($v; $flags):
if type == "string" then test($v; $flags) if _is_string then test($v; $flags)
else false else false
end; end;
@ -16,7 +16,7 @@ def _value_grep_other_cond($v; $flags):
. == $v; . == $v;
def vgrep($v; $flags): def vgrep($v; $flags):
if $v | type == "string" then if $v | _is_string then
grep_by(_is_scalar and _value_grep_string_cond($v; $flags)) grep_by(_is_scalar and _value_grep_string_cond($v; $flags))
else else
grep_by(_is_scalar and _value_grep_other_cond($v; $flags)) grep_by(_is_scalar and _value_grep_other_cond($v; $flags))
@ -26,7 +26,7 @@ def vgrep($v): vgrep($v; "");
def _buf_grep_any_cond($v; $flags): def _buf_grep_any_cond($v; $flags):
(isempty(tobytesrange | match($v; $flags)) | not)? // false; (isempty(tobytesrange | match($v; $flags)) | not)? // false;
def bgrep($v; $flags): def bgrep($v; $flags):
if $v | type == "string" then if $v | _is_string then
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags)) grep_by(_is_scalar and _buf_grep_any_cond($v; $flags))
else else
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags)) grep_by(_is_scalar and _buf_grep_any_cond($v; $flags))
@ -34,7 +34,7 @@ def bgrep($v; $flags):
def bgrep($v): bgrep($v; ""); def bgrep($v): bgrep($v; "");
def grep($v; $flags): def grep($v; $flags):
if $v | type == "string" then if $v | _is_string then
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags) or _value_grep_string_cond($v; $flags)) grep_by(_is_scalar and _buf_grep_any_cond($v; $flags) or _value_grep_string_cond($v; $flags))
else else
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags) or _value_grep_other_cond($v; $flags)) grep_by(_is_scalar and _buf_grep_any_cond($v; $flags) or _value_grep_other_cond($v; $flags))

View File

@ -102,8 +102,6 @@ def _intdiv($a; $b):
| ($a - ($a % $b)) / $b | ($a - ($a % $b)) / $b
); );
# valid jq identifier, start with alpha or underscore then zero or more alpha, num or underscore
def _is_ident: type == "string" and test("^[a-zA-Z_][a-zA-Z_0-9]*$");
# escape " and \ # escape " and \
def _escape_ident: gsub("(?<g>[\\\\\"])"; "\\\(.g)"); def _escape_ident: gsub("(?<g>[\\\\\"])"; "\\\(.g)");
@ -131,8 +129,16 @@ def _recurse_break(f):
else error else error
end; end;
def _is_scalar: def _is_null: type == "null";
type |. != "array" and . != "object"; def _is_string: type == "string";
def _is_number: type == "number";
def _is_boolean: type == "boolean";
def _is_array: type == "array";
def _is_object: type == "object";
def _is_scalar: (_is_array or _is_object) | not;
# valid jq identifier, start with alpha or underscore then zero or more alpha, num or underscore
def _is_ident: _is_string and test("^[a-zA-Z_][a-zA-Z_0-9]*$");
def _is_context_canceled_error: . == "context canceled"; def _is_context_canceled_error: . == "context canceled";
@ -146,7 +152,7 @@ def _path_to_expr($opts):
[""] + . [""] + .
end end
| map( | map(
if type == "number" then if _is_number then
( ("[" | _ansi_if($opts; "array")) ( ("[" | _ansi_if($opts; "array"))
, _ansi_if($opts; "number") , _ansi_if($opts; "number")
, ("]" | _ansi_if($opts; "array")) , ("]" | _ansi_if($opts; "array"))
@ -178,10 +184,10 @@ def _tree_path(children; name; $v):
# ["a", "b", 1] => ["a", 0, "b", 1] # ["a", "b", 1] => ["a", 0, "b", 1]
def _normalize_path: def _normalize_path:
( . as $np ( . as $np
| if $np | last | type == "string" then $np+[0] end | if $np | last | _is_string then $np+[0] end
# state is [path acc, possible pending zero index] # state is [path acc, possible pending zero index]
| ( reduce .[] as $np ([[], []]; | ( reduce .[] as $np ([[], []];
if $np | type == "string" then if $np | _is_string then
[(.[0]+.[1]+[$np]), [0]] [(.[0]+.[1]+[$np]), [0]]
else else
[.[0]+[$np], []] [.[0]+[$np], []]
@ -193,7 +199,7 @@ def _tree_path(children; name; $v):
| _expr_to_path | _expr_to_path
| _normalize_path | _normalize_path
| reduce .[] as $n ($c; | reduce .[] as $n ($c;
if $n | type == "string" then if $n | _is_string then
children | map(select(name == $n)) children | map(select(name == $n))
else else
.[$n] .[$n]
@ -217,7 +223,7 @@ def _tree_path(children; name; $v):
] ]
| flatten | flatten
| join(""); | join("");
if $v | type == "string" then _lookup if $v | _is_string then _lookup
else _path else _path
end; end;

View File

@ -55,7 +55,7 @@ def input:
( . as $err ( . as $err
| _input_decode_errors(. += {($name): $err}) as $_ | _input_decode_errors(. += {($name): $err}) as $_
| [ $opts.decode_format | [ $opts.decode_format
, if $err | type == "string" then ": \($err)" , if $err | _is_string then ": \($err)"
# TODO: if not string assume decode itself failed for now # TODO: if not string assume decode itself failed for now
else ": failed to decode (try -d FORMAT)" else ": failed to decode (try -d FORMAT)"
end end
@ -121,7 +121,7 @@ def input_filename: _input_filename;
# user expr error, report and continue # user expr error, report and continue
def _cli_eval_on_expr_error: def _cli_eval_on_expr_error:
( if type == "object" then ( if _is_object then
if .error | _eval_is_compile_error then .error | _eval_compile_error_tostring if .error | _eval_is_compile_error then .error | _eval_compile_error_tostring
elif .error then .error elif .error then .error
end end

View File

@ -110,7 +110,7 @@ def _opt_options:
def _opt_eval($rest): def _opt_eval($rest):
( with_entries( ( with_entries(
( select(.value | type == "string" and startswith("@")) ( select(.value | _is_string and startswith("@"))
| .value |= | .value |=
( . as $v ( . as $v
| try | try
@ -239,17 +239,17 @@ def _opt_to_string:
def _opt_from_string: if . then tojson[1:-1] else "" end; def _opt_from_string: if . then tojson[1:-1] else "" end;
def _opt_is_string_pair: def _opt_is_string_pair:
type == "array" and length == 2 and all(type == "string"); _is_array and length == 2 and all(_is_string);
def _opt_to_array(f): def _opt_to_array(f):
try try
( fromjson ( fromjson
| if type == "array" and (all(f) | not) then null end | if _is_array and (all(f) | not) then null end
) )
catch null; catch null;
def _opt_to_array_string_pair: _opt_to_array(_opt_is_string_pair); def _opt_to_array_string_pair: _opt_to_array(_opt_is_string_pair);
def _opt_to_array_string: _opt_to_array(type == "string"); def _opt_to_array_string: _opt_to_array(_is_string);
def _opt_from_array: tojson; def _opt_from_array: tojson;

View File

@ -121,7 +121,7 @@ def _prompt($opts):
def _value_path: def _value_path:
(._path? // []) | if . == [] then empty else _path_to_expr($opts) end; (._path? // []) | if . == [] then empty else _path_to_expr($opts) end;
def _value_preview($depth): def _value_preview($depth):
if $depth == 0 and format == null and type == "array" then if $depth == 0 and format == null and _is_array then
[ "[" [ "["
, if length == 0 then empty , if length == 0 then empty
else else