8000 Feature/#60 normalize hyphens (#61) · markusressel/container-app-conf@7aebc0a · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
Feature/#60 normalize hyphens (#61)
Browse files Browse the repository at this point in the history
* updated Pipfile.lock

* check for both original and normalized keys and get whichever is found first

* added test for env source

* updated README

* dont advertise the backwards compatible stuff in the README
  • Loading branch information
markusressel authored Apr 15, 2021
1 parent e4ec0c8 commit 7aebc0a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
20 changes: 14 additions & 6 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,14 @@ implementations are available:

Since you only specify the key path of a config entry the ENV
key is generated automatically by concatenating all key path items
using an underscore and converting to uppercase:
using an underscore, converting to uppercase and replacing any remaining
hyphens also with an underscore:

```python
key_path = ["my_app", "example"]
env_key = "_".join(key_path).upper()
key_path = ["my_app", "my-example"]
```

yields `MY_APP_EXAMPLE`.

would yield `MY_APP_MY_EXAMPLE`.

### Filesystem Source

Expand Down
9 changes: 6 additions & 3 deletions container_app_conf/source/env_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ class EnvSource(DataSource):
KEY_SPLIT_CHAR = "_"

def has(self, entry: ConfigEntry) -> bool:
return self.env_key(entry) in self.root.keys()
original_key = self.env_key(entry)
normalized_key = original_key.replace('-', '_')
return original_key in self.root.keys() or normalized_key in self.root.keys()

def get(self, entry: ConfigEntry) -> str or None:
key = self.env_key(entry)
return self.root.get(key, None)
original_key = self.env_key(entry)
normalized_key = original_key.replace('-', '_')
return self.root.get(original_key, self.root.get(normalized_key, None))

@staticmethod
def env_key(entry: ConfigEntry) -> str:
Expand Down
32 changes: 32 additions & 0 deletions tests/data_source/source_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
from typing import Dict
from unittest import mock

from container_app_conf import ConfigEntry
from container_app_conf.entry.int import IntConfigEntry
from container_app_conf.entry.string import StringConfigEntry
from container_app_conf.source.env_source import EnvSource
from container_app_conf.source.json_source import JsonSource
from container_app_conf.source.toml_source import TomlSource
from container_app_conf.source.yaml_source import YamlSource
Expand Down Expand Up @@ -110,3 +113,32 @@ def test_json(self):
self.assertEqual(source.get(str_entry), "value")
self.assertTrue(source.has(int_entry))
self.assertEqual(source.get(int_entry), 2)

def test_env(self):
str_entry = StringConfigEntry(
key_path=["test-ing", "key1"],
default="value"
)
int_entry = IntConfigEntry(
key_path=["testing", "key2"],
default=2
)

source = EnvSource()
original_key = EnvSource.env_key(str_entry)
expected = "expected"
with mock.patch.dict(os.environ, {original_key: expected}, clear=True):
source.load()

self.assertTrue(source.has(str_entry))
self.assertEqual(source.get(str_entry), expected)
self.assertFalse(source.has(int_entry))

normalized_env_key = original_key.replace('-', '_')
self.assertNotEqual(original_key, normalized_env_key)
with mock.patch.dict(os.environ, {normalized_env_key: expected + '2'}, clear=True):
source.load()

self.assertTrue(source.has(str_entry))
self.assertEqual(source.get(str_entry), expected + '2')
self.assertFalse(source.has(int_entry))

0 comments on commit 7aebc0a

Please sign in to comment.
0