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
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


## [0.4.0] (04.09.2022)

Expand Down
25 changes: 16 additions & 9 deletions crates/simplexpr/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
[json] => Ok(DynVal::from(json.as_json_object()?.len() as i32)),
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
"jq" => match args.as_slice() {
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?)
what @ ("jq" | "jq_raw") => match args.as_slice() {
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?, what == "jq_raw")
Comment on lines +433 to +434
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

.map_err(|e| EvalError::Spanned(code.span(), Box::new(e))),
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
Expand Down Expand Up @@ -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

let inputs = jaq_core::RcIter::new(std::iter::empty());
let out = filter

prepare_jaq_filter(code)?
.run(jaq_core::Ctx::new([], &inputs), jaq_core::Val::from(json))
.map(|x| x.map(Into::<serde_json::Value>::into))
.map(|x| x.map(|x| DynVal::from_string(serde_json::to_string(&x).unwrap())))
.map(|r| r.map(Into::<serde_json::Value>::into))
.map(|r| {
r.map(|v| match v {
// 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),

anyval => DynVal::from_string(serde_json::to_string(&anyval).unwrap()),
})
})
.collect::<Result<_, _>>()
.map_err(|e| EvalError::JaqError(e.to_string()))?;
Ok(out)
.map_err(|e| EvalError::JaqError(e.to_string()))
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions docs/src/expression_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Supported currently are the following features:
- `arraylength(value)`: Gets the length of the array
- `objectlength(value)`: Gets the amount of entries in the object
- `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`.
Comment on lines 53 to +54
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

- `formattime(unix_timestamp, format_str, timezone)`: Gets the time in a given format from UNIX timestamp.
Check [chrono's documentation](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) for more
information about format string and [chrono-tz's documentation](https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html)
Expand Down