Skip to content

Allow variable in include query value string #34

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

Merged
merged 2 commits into from
Feb 12, 2025
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
strategy:
matrix:
python-version: ["3.7", "3.11", "3.12"]
Expand Down
3 changes: 2 additions & 1 deletion src/yamlprocessor/dataprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ def load_include_file(
if self.VARIABLES_KEY in value:
variable_map.update(value[self.VARIABLES_KEY])
if self.QUERY_KEY in value:
value = jmespath.search(value[self.QUERY_KEY], loaded_value)
query_value = self.process_variable(value[self.QUERY_KEY])
value = jmespath.search(query_value, loaded_value)
else:
value = loaded_value
return value, parent_filenames, variable_map, is_merge
Expand Down
44 changes: 44 additions & 0 deletions src/yamlprocessor/tests/test_dataprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,50 @@ def test_main_18(capsys, monkeypatch):
)


def test_main_19(tmp_path, yaml):
"""Test main, single include with query value as variable."""
data_0 = {'flowers': {
'item1': {
'INCLUDE': '19i.yaml',
'QUERY': '${COLOUR_A}',
'MERGE': True,
},
'item2': {
'INCLUDE': '19i.yaml',
'QUERY': '${COLOUR_B}',
'MERGE': True,
},
}}
data_1 = {
'red': {'rose': 'U+1F339'},
'pink': {'tulip': 'U+1F337'},
'yellow': {'sunflower': 'U+1F33B'},
'white': {},
}
infilename = tmp_path / '19.yaml'
with infilename.open('w') as infile:
yaml.dump(data_0, infile)
with (tmp_path / '19i.yaml').open('w') as infile_1:
yaml.dump(data_1, infile_1)
outfilename = tmp_path / '19o.yaml'
main([
'-DCOLOUR_A=red',
'-DCOLOUR_B=yellow',
str(infilename),
str(outfilename),
])
assert yaml.load(outfilename.open()) == {
'flowers': {'rose': 'U+1F339', 'sunflower': 'U+1F33B'}}
main([
'-DCOLOUR_A=white',
'-DCOLOUR_B=pink',
str(infilename),
str(outfilename),
])
assert yaml.load(outfilename.open()) == {
'flowers': {'tulip': 'U+1F337'}}


def test_main_validate_1(tmp_path, capsys, yaml):
"""Test main, YAML with JSON schema validation."""
schema = {
Expand Down