Skip to content

Commit

Permalink
fix: Treat nil parents as empty tables
Browse files Browse the repository at this point in the history
Encountered this regression via nextest's test suite
(nextest-rs/nextest#2001). If a table is empty,
it would be treated as a unit value. If attempted to be deserialized
via a `Default` impl, it would lead to deserialization failing with an
error like

```console
profile.default-miri: invalid type: unit value, expected struct CustomProfileImpl
```

A bisect appears to indicate that
ec36bff is responsible. For empty
tables where the Default impl is supposed to work, it no longer would.

I've attempted to restore the old behavior of putting in an empty
table, specifically this section:

ec36bff#diff-c5423e2d2d6c87501239c0304c0f496742e00440defdd20368cf548ba42ab184L175-L178

I'm happy to make changes if there's a better approach.
  • Loading branch information
sunshowers committed Jan 11, 2025
1 parent 413f3b1 commit 2f095ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,16 @@ 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) {
*parent = Map::<String, Value>::new().into();
}

// Continue the deep merge
for (key, val) in incoming_map {
Self::root(key.clone()).set(parent, val.clone());
}
}

_ => {
*parent = value;
}
Expand Down
13 changes: 5 additions & 8 deletions tests/testsuite/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ fn test_merge_whole_config() {
#[cfg(feature = "json")]
fn test_merge_empty_maps() {
#[derive(Debug, Deserialize)]
#[allow(dead_code)] // temporary while this test is broken
struct Settings {
profile: BTreeMap<String, Profile>,
}

#[derive(Debug, Default, Deserialize)]
#[allow(dead_code)] // temporary while this test is broken
struct Profile {
name: Option<String>,
}
Expand Down Expand Up @@ -130,10 +128,9 @@ fn test_merge_empty_maps() {
.build()
.unwrap();

// This is currently broken -- the next commit fixes it.
let error = cfg.try_deserialize::<Settings>().unwrap_err();
assert_eq!(
error.to_string(),
"invalid type: unit value, expected struct Profile for key `profile.baz`"
);
let settings: Settings = cfg.try_deserialize().unwrap();
assert_eq!(settings.profile.len(), 3);
assert_eq!(settings.profile["foo"].name.as_deref(), Some("foo"));
assert_eq!(settings.profile["bar"].name.as_deref(), Some("bar"));
assert_eq!(settings.profile["baz"].name.as_deref(), None);
}

0 comments on commit 2f095ce

Please sign in to comment.