Skip to content

Commit

Permalink
allow merging of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Dec 9, 2021
1 parent 1804b47 commit f1fb837
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tpl"
version = "0.1.1"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl TemplateObject {
format!("Error parsing set option {}={}", key, value)
})?;
for seg in key.rsplit(".").map(str::trim) {
let mut seg = seg.to_string();
while seg.ends_with("[]") {
seg = seg.chars().take(seg.len() - 2).collect();
stage = vec![stage].into();
}
let mut map = HashMap::new();
map.insert(seg.to_string(), stage);
stage = map.into();
Expand Down Expand Up @@ -72,12 +77,19 @@ impl Merge for HashMap<String, Value> {
}
}

impl Merge for Vec<Value> {
fn merge(&mut self, other: Self) {
self.extend(other)
}
}

impl Merge for Value {
fn merge(&mut self, other: Self) {
use Value::*;
match (self, other) {
(Object(a), Object(b)) => a.merge(b),
(Map(a), Map(b)) => a.merge(b),
(Array(a), Array(b)) => a.merge(b),
(_, Nil) => (),
(a, b) => *a = b,
}
Expand Down

0 comments on commit f1fb837

Please sign in to comment.