mirror of
https://github.com/wader/fq.git
synced 2024-12-23 05:13:30 +03:00
interp: Add internal _is_<type> helpers
This commit is contained in:
parent
e1dc5c5737
commit
95e619659b
@ -84,6 +84,6 @@ def duration:
|
||||
)
|
||||
] | join("")
|
||||
end;
|
||||
if . | type == "string" then _string
|
||||
elif . | type == "number" then _number
|
||||
if type == "string" then _string
|
||||
elif type == "number" then _number
|
||||
else error("expected string or number") end;
|
||||
|
@ -75,7 +75,7 @@ def _eval_query_rewrite($opts):
|
||||
|
||||
# TODO: better way? what about nested eval errors?
|
||||
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:
|
||||
[ (.filename // "expr")
|
||||
, if .line != 1 or .column != 0 then "\(.line):\(.column)"
|
||||
|
@ -9,7 +9,7 @@ def display($opts):
|
||||
( options($opts) as $opts
|
||||
| if _can_display then _display($opts)
|
||||
else
|
||||
( if type == "string" and $opts.raw_string then print
|
||||
( if _is_string and $opts.raw_string then print
|
||||
else _print_color_json($opts)
|
||||
end
|
||||
, ( $opts.join_string
|
||||
@ -302,8 +302,8 @@ def tojq($style):
|
||||
} as $styles
|
||||
| _f(
|
||||
( $style // "compact"
|
||||
| if type == "string" then $styles[.]
|
||||
elif type == "object" then .
|
||||
| if _is_string then $styles[.]
|
||||
elif _is_object then .
|
||||
else error("invalid style")
|
||||
end
|
||||
)
|
||||
|
@ -8,7 +8,7 @@ def grep_by(f):
|
||||
);
|
||||
|
||||
def _value_grep_string_cond($v; $flags):
|
||||
if type == "string" then test($v; $flags)
|
||||
if _is_string then test($v; $flags)
|
||||
else false
|
||||
end;
|
||||
|
||||
@ -16,7 +16,7 @@ def _value_grep_other_cond($v; $flags):
|
||||
. == $v;
|
||||
|
||||
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))
|
||||
else
|
||||
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):
|
||||
(isempty(tobytesrange | match($v; $flags)) | not)? // false;
|
||||
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))
|
||||
else
|
||||
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 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))
|
||||
else
|
||||
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags) or _value_grep_other_cond($v; $flags))
|
||||
|
@ -102,8 +102,6 @@ def _intdiv($a; $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 \
|
||||
def _escape_ident: gsub("(?<g>[\\\\\"])"; "\\\(.g)");
|
||||
|
||||
@ -131,8 +129,16 @@ def _recurse_break(f):
|
||||
else error
|
||||
end;
|
||||
|
||||
def _is_scalar:
|
||||
type |. != "array" and . != "object";
|
||||
def _is_null: type == "null";
|
||||
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";
|
||||
|
||||
@ -146,7 +152,7 @@ def _path_to_expr($opts):
|
||||
[""] + .
|
||||
end
|
||||
| map(
|
||||
if type == "number" then
|
||||
if _is_number then
|
||||
( ("[" | _ansi_if($opts; "array"))
|
||||
, _ansi_if($opts; "number")
|
||||
, ("]" | _ansi_if($opts; "array"))
|
||||
@ -178,10 +184,10 @@ def _tree_path(children; name; $v):
|
||||
# ["a", "b", 1] => ["a", 0, "b", 1]
|
||||
def _normalize_path:
|
||||
( . 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]
|
||||
| ( reduce .[] as $np ([[], []];
|
||||
if $np | type == "string" then
|
||||
if $np | _is_string then
|
||||
[(.[0]+.[1]+[$np]), [0]]
|
||||
else
|
||||
[.[0]+[$np], []]
|
||||
@ -193,7 +199,7 @@ def _tree_path(children; name; $v):
|
||||
| _expr_to_path
|
||||
| _normalize_path
|
||||
| reduce .[] as $n ($c;
|
||||
if $n | type == "string" then
|
||||
if $n | _is_string then
|
||||
children | map(select(name == $n))
|
||||
else
|
||||
.[$n]
|
||||
@ -217,7 +223,7 @@ def _tree_path(children; name; $v):
|
||||
]
|
||||
| flatten
|
||||
| join("");
|
||||
if $v | type == "string" then _lookup
|
||||
if $v | _is_string then _lookup
|
||||
else _path
|
||||
end;
|
||||
|
||||
|
@ -55,7 +55,7 @@ def input:
|
||||
( . as $err
|
||||
| _input_decode_errors(. += {($name): $err}) as $_
|
||||
| [ $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
|
||||
else ": failed to decode (try -d FORMAT)"
|
||||
end
|
||||
@ -121,7 +121,7 @@ def input_filename: _input_filename;
|
||||
|
||||
# user expr error, report and continue
|
||||
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
|
||||
elif .error then .error
|
||||
end
|
||||
|
@ -110,7 +110,7 @@ def _opt_options:
|
||||
|
||||
def _opt_eval($rest):
|
||||
( with_entries(
|
||||
( select(.value | type == "string" and startswith("@"))
|
||||
( select(.value | _is_string and startswith("@"))
|
||||
| .value |=
|
||||
( . as $v
|
||||
| try
|
||||
@ -239,17 +239,17 @@ def _opt_to_string:
|
||||
def _opt_from_string: if . then tojson[1:-1] else "" end;
|
||||
|
||||
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):
|
||||
try
|
||||
( fromjson
|
||||
| if type == "array" and (all(f) | not) then null end
|
||||
| if _is_array and (all(f) | not) then null end
|
||||
)
|
||||
catch null;
|
||||
|
||||
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;
|
||||
|
||||
|
@ -121,7 +121,7 @@ def _prompt($opts):
|
||||
def _value_path:
|
||||
(._path? // []) | if . == [] then empty else _path_to_expr($opts) end;
|
||||
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
|
||||
else
|
||||
|
Loading…
Reference in New Issue
Block a user