-
-
Notifications
You must be signed in to change notification settings - Fork 394
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
base: master
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
what @ ("jq" | "jq_raw") => match args.as_slice() { | ||
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?, what == "jq_raw") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
- maybe pass
jq_fn
directly for a better interface, if we ever want to support further flags - split
jq
andjqraw
, 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> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
serde_json::Value::String(contents) if raw_string => DynVal::from_string(contents), | |
serde_json::Value::String(contents) if raw_output => DynVal::from_string(contents), |
- `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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `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
Description
This is a fairly simple change which adds a
jq_raw
function -jq_raw()
sharesjq()
's implementation, with the difference being that top level strings are not quoted (nested strings are quoted). This behavior is consistent withjq
's documented and observed behavior for --raw-output.This PR resolves #745.
For instance, we see the following behavior with orthodox
jq
Similarly, the expressions
Produce the following output,
Usage
Use
jq_raw
as you wouldjq
when unquoted strings are needed (typically for display)Checklist
Please make sure you can check all the boxes that apply to this PR.
docs/src
directory has been adjusted to reflect my changes.cargo fmt
to automatically format all code before committing