Skip to content
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

WIP: #281: Fixes for lists expansion operator (+=) #290

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pyhocon/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,10 @@ def _resolve_variable(cls, config, substitution):
"""
variable = substitution.variable
try:
return True, config.get(variable)
if substitution.self_ref:
return True, substitution.parent.overriden_value
else:
return True, config.get(variable)
except ConfigMissingException:
# default to environment variable
value = os.environ.get(variable)
Expand Down Expand Up @@ -859,10 +862,10 @@ def postParse(self, instring, loc, token_list):
else:
value = values[0]
if isinstance(value, list) and operator == "+=":
value = ConfigValues([ConfigSubstitution(key, True, '', False, loc), value], False, loc)
value = ConfigValues([ConfigSubstitution(key, True, '', False, loc, self_ref=True), value], False, loc)
config_tree.put(key, value, False)
elif isinstance(value, unicode) and operator == "+=":
value = ConfigValues([ConfigSubstitution(key, True, '', True, loc), ' ' + value], True, loc)
value = ConfigValues([ConfigSubstitution(key, True, '', True, loc, self_ref=True), ' ' + value], True, loc)
config_tree.put(key, value, False)
elif isinstance(value, list):
config_tree.put(key, value, False)
Expand Down
3 changes: 2 additions & 1 deletion pyhocon/config_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,15 @@ def __repr__(self): # pragma: no cover


class ConfigSubstitution(object):
def __init__(self, variable, optional, ws, instring, loc):
def __init__(self, variable, optional, ws, instring, loc, self_ref=False):
self.variable = variable
self.optional = optional
self.ws = ws
self.index = None
self.parent = None
self.instring = instring
self.loc = loc
self.self_ref = self_ref

def raw_str(self):
return self.variable
Expand Down
70 changes: 68 additions & 2 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ def test_self_ref_substitution_array(self):
)
assert config.get("x") == [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]

def test_self_append_array(self):
def test_self_extend_array(self):
config = ConfigFactory.parse_string(
"""
x = [1,2]
Expand All @@ -816,6 +816,37 @@ def test_self_append_array(self):
)
assert config.get("x") == [1, 2, 3, 4]

def test_self_append_array(self):
config = ConfigFactory.parse_string(
"""
x = [1,2]
x += 3
"""
)
assert config.get("x") == [1, 2, 3]

def test_self_extend_array_inside_dict(self):
config = ConfigFactory.parse_string(
"""
d {
x = [1,2]
x += [3,4]
}
"""
)
assert config.get("d.x") == [1, 2, 3, 4]

def test_self_append_array_inside_dict(self):
config = ConfigFactory.parse_string(
"""
d {
x = [1,2]
x += 3
}
"""
)
assert config.get("d.x") == [1, 2, 3]

def test_self_append_string(self):
'''
Should be equivalent to
Expand All @@ -830,6 +861,22 @@ def test_self_append_string(self):
)
assert config.get("x") == "abc def"

def test_self_append_string_inside_dict(self):
'''
Should be equivalent to
x = abc
x = ${?x} def
'''
config = ConfigFactory.parse_string(
"""
d {
x = abc
x += def
}
"""
)
assert config.get("d.x") == "abc def"

def test_self_append_non_existent_string(self):
'''
Should be equivalent to x = ${?x} def
Expand All @@ -841,14 +888,22 @@ def test_self_append_non_existent_string(self):
)
assert config.get("x") == " def"

def test_self_append_nonexistent_array(self):
def test_self_extend_nonexistent_array(self):
config = ConfigFactory.parse_string(
"""
x += [1,2]
"""
)
assert config.get("x") == [1, 2]

def test_self_append_nonexistent_array(self):
config = ConfigFactory.parse_string(
"""
x += 1
"""
)
assert config.get("x") == [1]

def test_self_append_object(self):
config = ConfigFactory.parse_string(
"""
Expand All @@ -858,6 +913,17 @@ def test_self_append_object(self):
)
assert config.get("x") == {'a': 1, 'b': 2}

def test_self_append_object_inside_dict(self):
config = ConfigFactory.parse_string(
"""
d {
x = {a: 1}
x += {b: 2}
}
"""
)
assert config.get("d.x") == {'a': 1, 'b': 2}

def test_self_append_nonexistent_object(self):
config = ConfigFactory.parse_string(
"""
Expand Down