-
-
Notifications
You must be signed in to change notification settings - Fork 398
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
.map_err(|e| EvalError::Spanned(code.span(), Box::new(e))), | ||||||||||
_ => Err(EvalError::WrongArgCount(name.to_string())), | ||||||||||
}, | ||||||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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), | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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)] | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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) | ||||||||||
|
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