Fix the config reader for a timeout setting
Your task
Your service reads an optional timeout from its config and falls back to a default. The first version wrapped the parse in catch (Exception e) {}, so a typo like 3O (letter O) silently became the default with nothing in the logs. The new version returns the value together with the reason, so the caller can log anything that is not fine.
An assistant wrote resolve_timeout_setting(config_pairs, key, default_seconds). It is in the editor. It has a bug. Find it and fix it.
Input and output:
config_pairs: config entries, each writtenkey=value.key: the setting to read, such astimeout.default_seconds: the default timeout.- Return
[value, reason].reasonis one of"ok","missing","empty","malformed"and"out_of_range". For every reason except"ok",valueisdefault_secondswritten as text.
Rules the tests check:
- Split each entry at its first
=into a key and a value. Skip an entry with no=. Keys match exactly: nothing is trimmed and case matters. - When the key appears more than once, the last entry wins.
- Check the value in this order and return the first reason that applies:
- The key never appears:
"missing". - The value is empty:
"empty". - The value is not an optional
-followed by one or more digits from 0 to 9, with nothing else (no spaces, no+):"malformed". - After dropping leading zeros, more than 9 digits are left:
"malformed". - The number is 0 or less:
"out_of_range".
- The key never appears:
- Otherwise return the number as text without leading zeros, and
"ok". So0030gives["30", "ok"].
Examples
resolve_timeout_setting(config_pairs: list[str], key: str, default_seconds: int) -> list[str]Example 1
Input
resolve_timeout_setting(["timeout=45", "retries=3"], "timeout", 30)Output
["45", "ok"]Example 2
Input
resolve_timeout_setting(["retries=3"], "timeout", 30)Output
["30", "missing"]Example 3
Input
resolve_timeout_setting(["timeout=3O"], "timeout", 30)Output
["30", "malformed"]