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 a to_yaml filter #525

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

Conversation

unpervertedkid
Copy link
Contributor

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

@unpervertedkid unpervertedkid requested a review from a team as a code owner December 14, 2024 11:11
Copy link
Contributor

@jerbly jerbly left a 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.

Comment on lines +136 to +137
let value = serde_json::to_value(value)
.map_err(|error| minijinja::Error::new(ErrorKind::BadSerialization, error.to_string()))?;
Copy link
Contributor

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()))?;
Copy link
Contributor

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> {
Copy link
Contributor

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.

https://github.com/mitsuhiko/minijinja/blob/d1ce496e101239bdfba01e29f407f0a57b8e4aed/minijinja/src/filters.rs#L1018

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.

3 participants