Skip to content

Commit

Permalink
Feature/detect casting comb token from converters (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenretel committed Aug 16, 2022
1 parent 078aaef commit 5297d07
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dynaconf/utils/parse_conf.py
Expand Up @@ -309,7 +309,8 @@ def _parse_conf_data(data, tomlfy=False, box_settings=None):
):
# Check combination token is used
comb_token = re.match(
r"^@(str|int|float|bool|json) @(jinja|format)", data
f"^({'|'.join(converters.keys())}) @(jinja|format)",
data,
)
if comb_token:
tokens = comb_token.group(0)
Expand Down
23 changes: 23 additions & 0 deletions example/custom_cast_token/app.py
@@ -0,0 +1,23 @@
from __future__ import annotations

from pathlib import Path

from dynaconf import Dynaconf
from dynaconf.utils import parse_conf

settings = Dynaconf(
envvar_prefix="DYNACONF",
settings_files=["settings.toml"],
environments=True,
)

# Add a custom casting token casting string to pathlib.Path object
parse_conf.converters["@path"] = (
lambda value: value.set_casting(Path)
if isinstance(value, parse_conf.Lazy)
else Path(value)
)

assert isinstance(settings.parent, Path)
assert isinstance(settings.child, Path)
assert str(settings.child).find("@format") == -1
3 changes: 3 additions & 0 deletions example/custom_cast_token/settings.toml
@@ -0,0 +1,3 @@
[default]
parent = "@path @format {env[HOME]}/parent"
child = "@path @format {this.parent}/child"

0 comments on commit 5297d07

Please sign in to comment.