Config migration that keeps explicit zeros
Your task
A service's config is moving from a flat v1 format to a nested v2 format. A coding assistant wrote the migration:
def migrate_config(old):
return {
"server": {"host": old.get("host") or "localhost",
"port": old.get("port") or 8080},
"timeout_ms": int((old.get("timeout") or 30) * 1000),
"retries": old.get("retries") or 3,
"log_level": "debug" if old.get("debug") else "info",
}
In staging, the payments worker's config said "retries": 0, because a retried charge can bill a customer twice. After the migration it said "retries": 3. Review the helper against the contract below and write a corrected migrate_config(old).
v1 keys, any of which may be missing: host (string), port (integer), timeout (seconds, a number with at most three decimal places), retries (integer), debug (boolean). A key that is present with the value None (null) counts as missing.
v2 output, always with exactly these keys:
"server":{"host": ..., "port": ...}"timeout_ms": the timeout times 1000, rounded to the nearest whole number"retries""log_level":"debug"whendebugis true, otherwise"info""dropped": the v1 keys that are not one of the five above, whatever their value, sorted in plain string order (by character code, so uppercase before lowercase), so nothing disappears without a trace
Defaults apply to missing keys only: host "localhost", port 8080, timeout 30, retries 3, debug false. A value that is present is kept even when it is 0, false or an empty string.
Examples:
migrate_config({"host": "db.internal", "retries": 0, "timeout": 2.5})returns{"server": {"host": "db.internal", "port": 8080}, "timeout_ms": 2500, "retries": 0, "log_level": "info", "dropped": []}.migrate_config({"timeout": 0, "debug": True, "color": "blue", "retries": None})returns{"server": {"host": "localhost", "port": 8080}, "timeout_ms": 0, "retries": 3, "log_level": "debug", "dropped": ["color"]}.migrate_config({})returns{"server": {"host": "localhost", "port": 8080}, "timeout_ms": 30000, "retries": 3, "log_level": "info", "dropped": []}.
Examples
migrate_config(old: dict) -> dictmigrate_config({"host": "db.internal", "retries": 0, "timeout": 2.5}){"server": {"host": "db.internal", "port": 8080}, "timeout_ms": 2500, "retries": 0, "log_level": "info", "dropped": []}migrate_config({"timeout": 0, "debug": True, "color": "blue", "retries": None}){"server": {"host": "localhost", "port": 8080}, "timeout_ms": 0, "retries": 3, "log_level": "debug", "dropped": ["color"]}migrate_config({}){"server": {"host": "localhost", "port": 8080}, "timeout_ms": 30000, "retries": 3, "log_level": "info", "dropped": []}