From 95e619659b3ac79b1e10251174e3d47db8706d3b Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Fri, 20 May 2022 16:38:28 +0200 Subject: [PATCH] interp: Add internal _is_ helpers --- dev/snippets.jq | 4 ++-- pkg/interp/eval.jq | 2 +- pkg/interp/funcs.jq | 6 +++--- pkg/interp/grep.jq | 8 ++++---- pkg/interp/internal.jq | 24 +++++++++++++++--------- pkg/interp/interp.jq | 4 ++-- pkg/interp/options.jq | 8 ++++---- pkg/interp/repl.jq | 2 +- 8 files changed, 32 insertions(+), 26 deletions(-) diff --git a/dev/snippets.jq b/dev/snippets.jq index 6ec411be..b5c26fc2 100644 --- a/dev/snippets.jq +++ b/dev/snippets.jq @@ -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; diff --git a/pkg/interp/eval.jq b/pkg/interp/eval.jq index 4d82365f..0649eb16 100644 --- a/pkg/interp/eval.jq +++ b/pkg/interp/eval.jq @@ -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)" diff --git a/pkg/interp/funcs.jq b/pkg/interp/funcs.jq index 65b38598..81defd7f 100644 --- a/pkg/interp/funcs.jq +++ b/pkg/interp/funcs.jq @@ -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 ) diff --git a/pkg/interp/grep.jq b/pkg/interp/grep.jq index e0e133f4..95bda844 100644 --- a/pkg/interp/grep.jq +++ b/pkg/interp/grep.jq @@ -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)) diff --git a/pkg/interp/internal.jq b/pkg/interp/internal.jq index 11082734..c0b0d825 100644 --- a/pkg/interp/internal.jq +++ b/pkg/interp/internal.jq @@ -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)"); @@ -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; diff --git a/pkg/interp/interp.jq b/pkg/interp/interp.jq index 2993ebf6..bc09b083 100644 --- a/pkg/interp/interp.jq +++ b/pkg/interp/interp.jq @@ -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 diff --git a/pkg/interp/options.jq b/pkg/interp/options.jq index 6f38cb26..e374bdf7 100644 --- a/pkg/interp/options.jq +++ b/pkg/interp/options.jq @@ -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; diff --git a/pkg/interp/repl.jq b/pkg/interp/repl.jq index 26529622..9ffc3930 100644 --- a/pkg/interp/repl.jq +++ b/pkg/interp/repl.jq @@ -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