Skip to content

Commit

Permalink
vdk-core: add is_default() function to config
Browse files Browse the repository at this point in the history
Why?

There are cases where we want to know we're using
the default value for a config option. One such
case is configuration presets

#3055

What?

Add is_default() to the config object.

How was this tested?

Unit tests

What kind of change is this?

Feature/non-breaking

Signed-off-by: Dilyan Marinov <[email protected]>
  • Loading branch information
Dilyan Marinov committed Feb 5, 2024
1 parent 03dde45 commit 6cd3382
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions projects/vdk-core/src/vdk/internal/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ def is_sensitive(self, key: ConfigKey) -> bool | None:
"""
return self.__config_key_to_sensitive.get(key)

def is_default(self, key: ConfigKey) -> bool:
"""
Return True if the configuration value associated with a given key
is the same as the default value
:param key: the configuration key (e.g db_host, service_uri, etc.)
:return: the value corresponding to the configuration key
"""
key = _normalize_config_key(key)
default_value = self.__config_key_to_default_value.get(key)
value = self.__config_key_to_value.get(key, default_value)
return value == default_value

def list_config_keys(self) -> list[ConfigKey]:
"""
List all added (defined) config keys
Expand Down
2 changes: 2 additions & 0 deletions projects/vdk-core/tests/vdk/internal/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_add(key, default_value, show_default_value, description):
assert description is None or description in cfg.get_description(key)
assert default_value == cfg.get_value(key)
assert default_value == cfg[key]
assert cfg.is_default(key)


def test_unknown_key():
Expand Down Expand Up @@ -81,6 +82,7 @@ def test_set_value_overrides_default():
builder.add("key", "default_value", False, "key description")
cfg = builder.build()
assert cfg.get_value("key") == "value"
assert cfg.is_default("key") is False


def test_set_value_overrides_default_preserve_types():
Expand Down

0 comments on commit 6cd3382

Please sign in to comment.