Skip to content

Commit 4b9e386

Browse files
committed
feat: substitution recurses into nested dicts
1 parent d7f79f1 commit 4b9e386

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/popper/parser.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@ def __apply_substitution(wf_element, k, v, used_registry):
141141
for ek in wf_element:
142142
if k in ek:
143143
log.fail("Substitutions not allowed on dictionary keys")
144-
if k in wf_element[ek]:
145-
log.debug(f"Applying substitution to value associated to key {k}")
146-
wf_element[ek] = wf_element[ek].replace(k, v)
147-
used_registry[k] = 1
144+
wf_element[ek] = WorkflowParser.__apply_substitution(wf_element[ek], k, v, used_registry)
148145

149146
return wf_element
150147

src/test/test_parser.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,37 @@ def test_substitutions(self):
163163
**{"wf_data": wf_data, "substitutions": substitutions}
164164
)
165165

166+
# substitute nested
167+
wf_data = {
168+
"steps": [
169+
{
170+
"uses": "some_$_SUB1",
171+
"id": "some other $_SUB2",
172+
"env": {"FOO": "env_$_SUB3"},
173+
"secrets": ["secret_$_SUB4"],
174+
"options": {
175+
"labels": {"timestamp": "$_TIMESTAMP"}
176+
},
177+
}
178+
]
179+
}
180+
substitutions = [
181+
"_TIMESTAMP=1613916937",
182+
"_SUB1=ONE",
183+
"_SUB2=TWO",
184+
"_SUB3=THREE",
185+
"_SUB4=4",
186+
]
187+
wf = WorkflowParser.parse(
188+
wf_data=wf_data, substitutions=substitutions,
189+
)
190+
step = wf.steps[0]
191+
self.assertEqual("some_ONE", step.uses)
192+
self.assertEqual("some other TWO", step.id)
193+
self.assertEqual("env_THREE", step.env["FOO"])
194+
self.assertEqual(("secret_4",), step.secrets)
195+
self.assertEqual({"timestamp": "1613916937"}, step.options.labels)
196+
166197
# allow loose substitutions
167198
wf = WorkflowParser.parse(
168199
wf_data=wf_data, substitutions=substitutions, allow_loose=True

0 commit comments

Comments
 (0)