-
Notifications
You must be signed in to change notification settings - Fork 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
Add a to_yaml filter #525
base: main
Are you sure you want to change the base?
Add a to_yaml filter #525
Conversation
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.
Remember to also add a changelog entry. Thanks.
let value = serde_json::to_value(value) | ||
.map_err(|error| minijinja::Error::new(ErrorKind::BadSerialization, error.to_string()))?; |
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 is unnecessary, you can pass a reference to minijinja::Value
directly to serde_yaml::to_string
because it implements serde::Serialize
.
.map_err(|error| minijinja::Error::new(ErrorKind::BadSerialization, error.to_string()))?; | ||
|
||
let yaml = serde_yaml::to_string(&value) | ||
.map_err(|error| minijinja::Error::new(ErrorKind::BadSerialization, error.to_string()))?; |
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.
Perhaps there's a way to write a test to exercise the error condition?
@@ -131,6 +132,16 @@ pub fn acronym(acronyms: Vec<String>) -> impl Fn(&str) -> String { | |||
} | |||
} | |||
|
|||
fn to_yaml(value: Value) -> Result<String, minijinja::Error> { |
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.
You should take the tojson
filter implemented in the standard minijinja
library as an example. The method’s signature is slightly different, and more importantly, they address the compatibility of the output with HTML (pay attention to the last map function). We don’t need the pretty-printing part since YAML is always pretty-printed and we probably don't need the indent parameter too.
See the permalink below.
Need
People have expressed a need to dump a value into yaml file. #423
Solution
WIP: This pull request adds a
to_yaml
filter that can be used to dump a value into a YAML file.Known limitation
The implementation does not currently maintain ordering