Skip to content
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

Add jq_raw function to simplexpr #952

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RomanHargrave
Copy link

@RomanHargrave RomanHargrave commented Oct 21, 2023

Description

This is a fairly simple change which adds a jq_raw function - jq_raw() shares jq()'s implementation, with the difference being that top level strings are not quoted (nested strings are quoted). This behavior is consistent with jq's documented and observed behavior for --raw-output.

This PR resolves #745.

For instance, we see the following behavior with orthodox jq

$ echo '{"a": ["b"]}' | jq -r '.a'
[
  "b"
]
$ echo '{"a": ["b"]}' | jq -r '.a[]'
b

Similarly, the expressions

{jq_raw("{\"a\": [\"b\"]}", ".a")}
{jq_raw("{\"a\": [\"b\"]}", ".a[]")}

Produce the following output,

image

Usage

Use jq_raw as you would jq when unquoted strings are needed (typically for display)

Checklist

Please make sure you can check all the boxes that apply to this PR.

  • All widgets I've added are correctly documented.
  • I added my changes to CHANGELOG.md, if appropriate.
  • The documentation in the docs/src directory has been adjusted to reflect my changes.
  • I used cargo fmt to automatically format all code before committing

jq_raw() shares the jq()'s implementation, with the difference being
that _top level_ strings are not quoted (nested strings are quoted).
This behavior is consistent with jq's documented and observed behavior
for --raw-output.
Copy link
Contributor

@w-lfchen w-lfchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just wrote down some thoughts to consider

@@ -20,6 +20,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add trigonometric functions (`sin`, `cos`, `tan`, `cot`) and degree/radian conversions (`degtorad`, `radtodeg`) (By: end-4)
- Add `substring` function to simplexpr
- Add `--duration` flag to `eww open`
- Add `jq_raw` function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this entry needs to be moved and potentially updated

Comment on lines +433 to +434
what @ ("jq" | "jq_raw") => match args.as_slice() {
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?, what == "jq_raw")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
what @ ("jq" | "jq_raw") => match args.as_slice() {
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?, what == "jq_raw")
jq_fn @ ("jq" | "jqraw") => match args.as_slice() {
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?, jq_fn == "jqraw")

jqraw would be in line with the other function names, also consider renaming the binding to better reflect what it holds.
two further considerations:

  1. maybe pass jq_fn directly for a better interface, if we ever want to support further flags
  2. split jq and jqraw, this would probably yield better performance

@@ -478,16 +478,23 @@ fn prepare_jaq_filter(code: String) -> Result<Arc<jaq_core::Filter>, EvalError>
Ok(Arc::new(filter))
}

fn run_jaq_function(json: serde_json::Value, code: String) -> Result<DynVal, EvalError> {
let filter = prepare_jaq_filter(code)?;
fn run_jaq_function(json: serde_json::Value, code: String, raw_string: bool) -> Result<DynVal, EvalError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn run_jaq_function(json: serde_json::Value, code: String, raw_string: bool) -> Result<DynVal, EvalError> {
fn run_jaq_function(json: serde_json::Value, code: String, raw_output: bool) -> Result<DynVal, EvalError> {

using the same name as the flag makes it easier to look up

// Per jq docs, "raw-output" behavior simply omits
// quotation marks from strings, and outputs what would
// otherwise be valid JSON, so this should replicate that.
serde_json::Value::String(contents) if raw_string => DynVal::from_string(contents),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serde_json::Value::String(contents) if raw_string => DynVal::from_string(contents),
serde_json::Value::String(contents) if raw_output => DynVal::from_string(contents),

Comment on lines 53 to +54
- `jq(value, jq_filter_string)`: run a [jq](https://stedolan.github.io/jq/manual/) style command on a json value. (Uses [jaq](https://crates.io/crates/jaq) internally).
- `jq_raw(value, jq_filter_string)`: as above, but top-level strings will not be quoted, as with `jq --raw-output`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `jq(value, jq_filter_string)`: run a [jq](https://stedolan.github.io/jq/manual/) style command on a json value. (Uses [jaq](https://crates.io/crates/jaq) internally).
- `jq_raw(value, jq_filter_string)`: as above, but top-level strings will not be quoted, as with `jq --raw-output`.
- `jq(value, jq_filter_string)`: run a [jq](https://jqlang.github.io/jq/manual/) style command on a json value. (Uses [jaq](https://crates.io/crates/jaq) internally).
- `jqraw(value, jq_filter_string)`: acts as if `jq` was invoked with the `--raw-output` flag, removing quotation marks if the resulting value is a JSON string. See the [jq docs](https://jqlang.github.io/jq/manual/) for more information.

point to the documentation here to make things easier for us

@w-lfchen w-lfchen mentioned this pull request Sep 17, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add option to return jq command with raw output
2 participants