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 support for merge keys and aliases in YAML #531

Merged
merged 3 commits into from
Jul 2, 2024
Merged
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 @@ -8,6 +8,7 @@ All notable changes to MiniJinja are documented here.
`str.isalnum`, `str.isalpha`, `str.isascii`, `str.isdigit`,
`str.isnumeric`, `str.join`, `str.startswith`. #522
- Added the missing tests `boolean`, `divisibleby`, `lower` and `upper`. #592
- minijinja-cli now supports YAML aliases and merge keys. #531

## 2.0.2

Expand Down
62 changes: 33 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion minijinja-cabi/rustfmt.toml

This file was deleted.

4 changes: 2 additions & 2 deletions minijinja-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version = "1.65"

[features]
default = ["toml", "yaml", "querystring", "cbor", "datetime", "json5", "repl", "unicode"]
yaml = ["serde_yaml"]
yaml = ["serde_yml"]
querystring = ["serde_qs"]
cbor = ["ciborium"]
datetime = ["minijinja-contrib/datetime", "minijinja-contrib/timezone"]
Expand Down Expand Up @@ -47,7 +47,7 @@ serde = "1.0.183"
serde_json = "1.0.105"
serde_json5 = { version = "0.1.0", optional = true }
serde_qs = { version = "0.12.0", optional = true }
serde_yaml = { version = "0.9.25", optional = true }
serde_yml = { version = "0.0.10", optional = true }
tempfile = "3.9.0"
toml = { version = "0.7.6", optional = true }
clap_complete = { version = "4", optional = true }
Expand Down
1 change: 1 addition & 0 deletions minijinja-cli/examples/alias.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ dict3 }}
9 changes: 9 additions & 0 deletions minijinja-cli/examples/alias.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dict1: &dict1_anchor
key1: value1

dict2: &dict2_anchor
key2: value2

dict3:
<<: [*dict1_anchor, *dict2_anchor]
key3: value3
1 change: 1 addition & 0 deletions minijinja-cli/examples/dump.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ debug() }}
11 changes: 9 additions & 2 deletions minijinja-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ fn load_data(
#[cfg(feature = "querystring")]
"querystring" => Value::from(serde_qs::from_str::<HashMap<String, Value>>(&contents)?),
#[cfg(feature = "yaml")]
"yaml" => serde_yaml::from_str(&contents)?,
"yaml" => {
// for merge keys to work we need to manually call `apply_merge`.
// For this reason we need to deserialize into a serde_yml::Value
// before converting it into a final value.
let mut v: serde_yml::Value = serde_yml::from_str(&contents)?;
v.apply_merge()?;
Value::from_serialize(v)
}
#[cfg(feature = "toml")]
"toml" => toml::from_str(&contents)?,
#[cfg(feature = "cbor")]
Expand Down Expand Up @@ -157,7 +164,7 @@ fn interpret_raw_value(s: &str) -> Result<Value, Error> {
}
#[cfg(feature = "yaml")]
mod imp {
pub use serde_yaml::from_str;
pub use serde_yml::from_str;
pub const FMT: &str = "JSON";
}
imp::from_str::<Value>(s)
Expand Down
37 changes: 37 additions & 0 deletions minijinja-cli/tests/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,43 @@ fn test_yaml() {
"###);
}

#[test]
#[cfg(feature = "yaml")]
fn test_yaml_aliases() {
let input = file_with_contents_and_ext(
r#"
a: &a
key1: value1

b: &b
key2: value2

c:
<<: *a
key2: from-c

d:
<<: [*a, *b]
key3: value3
"#,
".yaml",
);
let tmpl = file_with_contents(r#"{{ [c.key1, c.key2] }}\n{{ [d.key1, d.key2, d.key3] }}"#);

assert_cmd_snapshot!(
cli()
.arg(tmpl.path())
.arg(input.path()),
@r###"
success: true
exit_code: 0
----- stdout -----
["value1", "from-c"]\n["value1", "value2", "value3"]

----- stderr -----
"###);
}

#[test]
#[cfg(feature = "toml")]
fn test_toml() {
Expand Down