Skip to content

Commit

Permalink
fix(config_parser): handle substitutions with resolved values of type…
Browse files Browse the repository at this point in the history
… ConfigValues (#322)

* fix(config_parser): resolve substitutions of type ConfigValues

* test(config_parser): add tests for nested substitutions
  • Loading branch information
M0dEx authored May 27, 2024
1 parent 4683937 commit 2af3400
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyhocon/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,8 @@ def resolve_substitutions(cls, config, accept_unresolved=False):
cache_values = cache.get(substitution)
if cache_values is None:
continue
if not isinstance(resolved_value, ConfigValues):

if resolved_value:
cache_values.append(substitution)
overrides = [s for s in substitutions if s.parent.overridden_value == substitution.parent]
if len(overrides) > 0:
Expand Down
12 changes: 12 additions & 0 deletions samples/substitutions.d/base.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
foo {
foo-inner {
pre-foo = 1
foo-sub = ${foo.foo-inner.pre-foo}${foo.foo-inner.bar}
}
}

bar {
bar-inner {
foo-sub = ${foo.foo-inner.foo-sub}
}
}
7 changes: 7 additions & 0 deletions samples/substitutions.d/child.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include required("base.conf")

foo {
foo-inner {
bar = 1
}
}
5 changes: 5 additions & 0 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2632,6 +2632,11 @@ def test_escape_sequences_json_equivalence(self):
assert config == expected
assert config == json.loads(source)

def test_nested_substitution(self):
config = ConfigFactory.parse_file("samples/substitutions.d/child.conf")
assert config.get_int("foo.foo-inner.foo-sub") == 11
assert config.get_int("bar.bar-inner.foo-sub") == 11

def test_triple_quotes_keys(self):
config = ConfigFactory.parse_string("\"\"\"foo\"\"\" = bar")
assert config['foo'] == 'bar'
Expand Down

0 comments on commit 2af3400

Please sign in to comment.