generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 26
Add ConfigResolver with shared singleton pattern #641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ubaskota
wants to merge
2
commits into
smithy-lang:config_resolution_main
Choose a base branch
from
ubaskota:config_resolver
base: config_resolution_main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from .resolver import ConfigResolver | ||
|
|
||
| __all__ = [ | ||
| "ConfigResolver", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from collections.abc import Sequence | ||
| from typing import Any | ||
|
|
||
| from smithy_core.interfaces.config import ConfigSource | ||
|
|
||
|
|
||
| class ConfigResolver: | ||
| """Resolves configuration values from multiple sources. | ||
|
|
||
| The resolver iterates through sources in precedence order, returning | ||
| the first non-None value found for a given configuration key. | ||
| """ | ||
|
|
||
| def __init__(self, sources: Sequence[ConfigSource]) -> None: | ||
| """Initialize the resolver with sources in precedence order. | ||
|
|
||
| :param sources: List of configuration sources in precedence order. The first | ||
| source in the list has the highest priority. The list is copied to | ||
| prevent external modification. | ||
ubaskota marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| self._sources = list(sources) | ||
|
|
||
| def get(self, key: str) -> tuple[Any, Any]: | ||
| """Resolve a configuration value from sources by iterating through them in precedence order. | ||
|
|
||
| :param key: The configuration key to resolve (e.g., 'retry_mode') | ||
|
|
||
| :returns: A tuple of (value, source_name). If no source provides a value, | ||
| returns (None, None). | ||
| """ | ||
| for source in self._sources: | ||
| value = source.get(key) | ||
| if value is not None: | ||
| return (value, source.name) | ||
| return (None, None) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 |
127 changes: 127 additions & 0 deletions
127
packages/smithy-core/tests/unit/config/test_resolver.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from typing import Any | ||
|
|
||
| from smithy_core.config.resolver import ConfigResolver | ||
|
|
||
|
|
||
| class StubSource: | ||
| """A simple ConfigSource implementation for testing. | ||
|
|
||
| Returns values from a provided dictionary, or None if the key | ||
| is not present. | ||
| """ | ||
|
|
||
| def __init__(self, source_name: str, data: dict[str, Any] | None = None): | ||
| self._name = source_name | ||
| self._data = data or {} | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| return self._name | ||
|
|
||
| def get(self, key: str) -> Any | None: | ||
| return self._data.get(key) | ||
|
|
||
|
|
||
| class TestConfigResolver: | ||
| def test_returns_value_from_single_source(self): | ||
| source = StubSource("environment", {"region": "us-west-2"}) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result = resolver.get("region") | ||
|
|
||
| assert result == ("us-west-2", "environment") | ||
|
|
||
| def test_returns_None_when_source_has_no_value(self): | ||
| source = StubSource("environment", {}) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result = resolver.get("region") | ||
|
|
||
| assert result == (None, None) | ||
|
|
||
| def test_returns_None_with_empty_source_list(self): | ||
| resolver = ConfigResolver(sources=[]) | ||
|
|
||
| result = resolver.get("region") | ||
|
|
||
| assert result == (None, None) | ||
|
|
||
| def test_first_source_takes_precedence(self): | ||
| first_priority_source = StubSource("source_one", {"region": "us-east-1"}) | ||
| second_priority_source = StubSource("source_two", {"region": "eu-west-1"}) | ||
| resolver = ConfigResolver( | ||
| sources=[first_priority_source, second_priority_source] | ||
| ) | ||
|
|
||
| result = resolver.get("region") | ||
|
|
||
| assert result == ("us-east-1", "source_one") | ||
|
|
||
| def test_skips_source_returning_none_and_uses_next(self): | ||
| empty_source = StubSource("source_one", {}) | ||
| fallback_source = StubSource("source_two", {"region": "ap-south-1"}) | ||
| resolver = ConfigResolver(sources=[empty_source, fallback_source]) | ||
|
|
||
| result = resolver.get("region") | ||
|
|
||
| assert result == ("ap-south-1", "source_two") | ||
|
|
||
| def test_resolves_different_keys_from_different_sources(self): | ||
| instance = StubSource("source_one", {"region": "us-west-2"}) | ||
| environment = StubSource("source_two", {"retry_mode": "adaptive"}) | ||
| resolver = ConfigResolver(sources=[instance, environment]) | ||
|
|
||
| region = resolver.get("region") | ||
| retry_mode = resolver.get("retry_mode") | ||
|
|
||
| assert region == ("us-west-2", "source_one") | ||
| assert retry_mode == ("adaptive", "source_two") | ||
|
|
||
| def test_returns_non_string_values(self): | ||
| source = StubSource( | ||
| "default", | ||
| { | ||
| "max_retries": 3, | ||
| "use_ssl": True, | ||
| }, | ||
| ) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| assert resolver.get("max_retries") == (3, "default") | ||
| assert resolver.get("use_ssl") == (True, "default") | ||
|
|
||
| def test_get_is_idempotent(self): | ||
| source = StubSource("environment", {"region": "us-west-2"}) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result1 = resolver.get("region") | ||
| result2 = resolver.get("region") | ||
| result3 = resolver.get("region") | ||
|
|
||
| assert result1 == result2 == result3 == ("us-west-2", "environment") | ||
|
|
||
| def test_treats_empty_string_as_valid_value(self): | ||
| source = StubSource("test", {"region": ""}) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| value, source_name = resolver.get("region") | ||
|
|
||
| assert value == "" | ||
| assert source_name == "test" | ||
|
|
||
| def test_external_list_modifications_do_not_affect_resolver(self): | ||
| source1 = StubSource("environment", {"region": "us-west-2"}) | ||
| source2 = StubSource("config", {"region": "eu-west-1"}) | ||
| sources = [source1] | ||
|
|
||
| resolver = ConfigResolver(sources=sources) | ||
|
|
||
| # Modify the original list after resolver construction | ||
| sources.append(source2) | ||
| sources.clear() | ||
|
|
||
| # Resolver should use the original source | ||
| result = resolver.get("region") | ||
| assert result == ("us-west-2", "environment") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.