fn merge_toml()

in config-common/src/lib.rs [99:127]


fn merge_toml(base: &mut toml::Value, patch: toml::Value) {
    // Similar to JSON patch, except that:
    //
    // - Maps are called tables.
    // - There is no equivalent of null that can be used to remove keys from an object.
    // - Arrays are merged via concatenating the patch to the base, rather than replacing the base with the patch.
    //   This is needed to make principals work; `[[principal]]` sections from multiple files need to be concatenated.

    if let toml::Value::Table(base) = base {
        if let toml::Value::Table(patch) = patch {
            for (key, value) in patch {
                // Insert a dummy `false` if the original key didn't exist at all. It'll be overwritten by `value` in that case.
                let original_value = base.entry(key).or_insert(toml::Value::Boolean(false));
                merge_toml(original_value, value);
            }

            return;
        }
    }

    if let toml::Value::Array(base) = base {
        if let toml::Value::Array(patch) = patch {
            base.extend(patch);
            return;
        }
    }

    *base = patch;
}