8000 Fix eval as path expression regression by wader · Pull Request #27 · wader/jqjq · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix eval as path expression regression #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ $ jq -n -L . 'include "jqjq"; eval("def f: 1,8; [f,f] | map(.+105) | implode")'
$ jq -L . 'include "jqjq"; eval("(.+.) | map(.+105) | implode")' <<< '[1,8]'
"jqjq"

# can be used as path expression (only gojq for now because of jq issue)
# can be used as path expression (only gojq and jaq for now because of jq issues like https://github.com/jqlang/jq/issues/3128)
$ gojq -cn -L . 'include "jqjq"; {a:0, b:1} | eval(".a, .b") += 1'
{"a":1,"b":2}
$ jaq -cn -L . 'include "jqjq"; {a:0, b:1} | eval(".a, .b") += 1'
{"a":1,"b":2}
```

### Run tests
Expand Down
44 changes: 20 additions & 24 deletions jqjq.jq
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ def _internal_error($v): {_internal_error: $v} | error;
def _is_internal_error: type == "object" and has("_internal_error");
def _unwrap_internal_error: ._internal_error;

# reimplementation of getpath/1 for jaq
def _getpath($path):
if . == null or ($path | length) == 0 then .
else .[$path[0]] | _getpath($path[1:])
end;

# reimplementation of delpaths/1 for jaq
def _delpaths($paths):
def _delpath($p):
if $p == [] then empty
elif has($p[0]) then .[$p[0]] |= _delpath($p[1:])
else .
end;
reduce ($paths | unique | reverse)[] as $p
(.; _delpath($p));

# TODO: keep track of position?
# TODO: error on unbalanced string stack?
# string_stack is used to keep track of matching ( ) and \( <-> )" or ) (
Expand Down Expand Up @@ -1290,24 +1306,6 @@ def eval_ast($query; $path; $env; undefined_func):
end;
_f($path);

def _getpath($path):
reduce $path[] as $p (
.;
#debug({dot: ., $p}) |
if . == null then null
else .[$p]
end
);

def _delpaths($paths):
def _delpath($p):
if $p == [] then empty
elif has($p[0]) then .[$p[0]] |= _delpath($p[1:])
else .
end;
reduce ($paths | unique | reverse)[] as $p
(.; _delpath($p));

def _e($query; $path; $env):
( . # debug({c: ., $query, $path, $env})
| ( $query
Expand Down Expand Up @@ -2629,12 +2627,10 @@ def eval($expr; $globals; $builtins_env):
# TODO: does not work with jq yet because issue with bind patterns
# $ gojq -cn -L . 'include "jqjq"; {} | {a:1} | eval(".a") += 1'
# {"a":2}
| $value
);
# | if $path | . == [] or . == [null] then $value
# else getpath($path)
# end
# );
| if $path | . == [] or . == [null] then $value
else _getpath($path)
end
);
def eval($expr):
eval($expr; {}; _builtins_env);

Expand Down
0