Skip to content

Commit

Permalink
fix: Always overwrite with tables, even if empty
Browse files Browse the repository at this point in the history
While the last commit restored behavior to mostly where it was in
v0.15.2, this commit introduces a behavior change with the goal of
treating empty tables the same as non-empty ones (in particular,
`int_to_empty` is now treated the same as `int_to_non_empty`, both of
them overwriting the integer with the table).

Treating them the same makes a lot of sense logically, and the fact that
we weren't doing so is probably a bug in the old implementation. But it
is a notable behavior change, and one worth considering carefully.
  • Loading branch information
sunshowers authored and epage committed Jan 14, 2025
1 parent c28334c commit 6203c80
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ impl Expression {
let parent = self.get_mut_forcibly(root);
match value.kind {
ValueKind::Table(ref incoming_map) => {
// If the parent is nil, treat it as an empty table
if matches!(parent.kind, ValueKind::Nil) {
// If the parent is not a table, overwrite it, treating it as a
// table
if !matches!(parent.kind, ValueKind::Table(_)) {
*parent = Map::<String, Value>::new().into();
}

Expand Down
13 changes: 11 additions & 2 deletions tests/testsuite/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,17 @@ Settings {
.unwrap();
let res = cfg.try_deserialize::<Settings>();
assert_data_eq!(
res.unwrap_err().to_string(),
str!["invalid type: integer `42`, expected struct Profile for key `profile.int_to_empty`"]
res.unwrap().to_debug(),
str![[r#"
Settings {
profile: {
"int_to_empty": Profile {
name: None,
},
},
}
"#]]
);

// * int_to_non_empty: int -> map with k/v
Expand Down

0 comments on commit 6203c80

Please sign in to comment.