Fix the settings merge for user overrides
Your task
Your settings page stores settings as flat key=value entries, with dots in keys for nesting, like profile.name=ada. A user's override is applied on top of the defaults. An override value of null means the user cleared that setting. The first version, a recursive deep merge, treated null as an object and kept the old value.
An assistant wrote apply_settings_override(base_pairs, override_pairs) to replace it. It is in the editor. It has a bug. Find it and fix it.
Input and output:
base_pairs: the default settings, askey=valueentries.override_pairs: the user's overrides, askey=valueentries.- Return the merged settings as
key=valuestrings.
Rules the tests check:
- Split each entry at its first
=into a key and a value. Skip an entry with no=. - Read
base_pairsin order, thenoverride_pairsin order. A later entry for a key replaces the earlier value, within a list and across the two lists. - A value is replaced as a whole.
tags=aovertags=x|ygivestags=a. - An override whose value is exactly
nullremoves the key, if it is there. A later override can set it again. Inbase_pairs,nullis an ordinary value, and an empty value is always an ordinary value. - A key that is only in the overrides is added.
- Return the remaining settings sorted as plain strings.
Examples
apply_settings_override(base_pairs: list[str], override_pairs: list[str]) -> list[str]Example 1
Input
apply_settings_override(["theme=dark", "avatar=cat.png", "tags=x|y"], ["avatar=null"])Output
["tags=x|y", "theme=dark"]Example 2
Input
apply_settings_override(["tags=x|y", "theme=dark"], ["tags=a"])Output
["tags=a", "theme=dark"]Example 3
Input
apply_settings_override(["profile.name=ada"], ["profile.bio=hi"])Output
["profile.bio=hi", "profile.name=ada"]