Skip to content

Commit 77e3cff

Browse files
author
Alan Christie
committed
fix: Variable mapping now exposed as a Translation dataclass
1 parent 8809440 commit 77e3cff

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

workflow/decoder.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import os
7+
from dataclasses import dataclass
78
from typing import Any
89

910
import jsonschema
@@ -23,6 +24,14 @@
2324
assert _WORKFLOW_SCHEMA
2425

2526

27+
@dataclass
28+
class Translation:
29+
"""A source ("in_") to destination ("out") variable map."""
30+
31+
in_: str
32+
out: str
33+
34+
2635
def validate_schema(workflow: dict[str, Any]) -> str | None:
2736
"""Checks the Workflow Definition against the built-in schema.
2837
If there's an error the error text is returned, otherwise None.
@@ -107,28 +116,27 @@ def get_step_input_variable_names(
107116
return variable_names
108117

109118

110-
def get_step_workflow_variable_mapping(
111-
*, step: dict[str, Any]
112-
) -> list[tuple[str, str]]:
119+
def get_step_workflow_variable_mapping(*, step: dict[str, Any]) -> list[Translation]:
113120
"""Returns a list of workflow vaiable name to step variable name tuples
114121
for the given step."""
115-
variable_mapping: list[tuple[str, str]] = []
122+
variable_mapping: list[Translation] = []
116123
if "variable-mapping" in step:
117124
for v_map in step["variable-mapping"]:
118125
if "from-workflow" in v_map:
119-
# Tuple is "from" -> "to"
120126
variable_mapping.append(
121-
(v_map["from-workflow"]["variable"], v_map["variable"])
127+
Translation(
128+
in_=v_map["from-workflow"]["variable"], out=v_map["variable"]
129+
)
122130
)
123131
return variable_mapping
124132

125133

126134
def get_step_prior_step_variable_mapping(
127135
*, step: dict[str, Any]
128-
) -> dict[str, list[tuple[str, str]]]:
129-
"""Returns list of tuples, indexed by prior step name, of source step vaiable name
130-
to this step's variable name."""
131-
variable_mapping: dict[str, list[tuple[str, str]]] = {}
136+
) -> dict[str, list[Translation]]:
137+
"""Returns list of translate objects, indexed by prior step name,
138+
that identify source step vaiable name to this step's variable name."""
139+
variable_mapping: dict[str, list[Translation]] = {}
132140
if "variable-mapping" in step:
133141
for v_map in step["variable-mapping"]:
134142
if "from-step" in v_map:
@@ -137,10 +145,12 @@ def get_step_prior_step_variable_mapping(
137145
# Tuple is "from" -> "to"
138146
if step_name in variable_mapping:
139147
variable_mapping[step_name].append(
140-
(step_variable, v_map["variable"])
148+
Translation(in_=step_variable, out=v_map["variable"])
141149
)
142150
else:
143-
variable_mapping[step_name] = [(step_variable, v_map["variable"])]
151+
variable_mapping[step_name] = [
152+
Translation(in_=step_variable, out=v_map["variable"])
153+
]
144154
return variable_mapping
145155

146156

workflow/workflow_engine.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040

4141
from .decoder import (
42+
Translation,
4243
get_step_input_variable_names,
4344
get_step_prior_step_variable_mapping,
4445
get_step_replicator,
@@ -364,14 +365,14 @@ def _validate_step_command(
364365
# "in" variables are worklfow variables, and "out" variables
365366
# are expected Job variables. We use this to add variables
366367
# to the "all variables" map.
367-
for from_to in get_step_workflow_variable_mapping(step=step):
368-
all_variables[from_to[1]] = running_workflow_variables[from_to[0]]
368+
for tr in get_step_workflow_variable_mapping(step=step):
369+
all_variables[tr.out] = running_workflow_variables[tr.in_]
369370

370371
# Now we apply variables from the "variable mapping" block
371372
# related to values used in prior steps. The decoder gives
372373
# us a map indexed by prior step name that's a list of "in" "out"
373374
# tuples as above.
374-
step_prior_v_map: dict[str, list[tuple[str, str]]] = (
375+
step_prior_v_map: dict[str, list[Translation]] = (
375376
get_step_prior_step_variable_mapping(step=step)
376377
)
377378
for prior_step_name, v_map in step_prior_v_map.items():
@@ -381,8 +382,8 @@ def _validate_step_command(
381382
name=prior_step_name, running_workflow_id=running_workflow_id
382383
)
383384
# Copy "in" value to "out"...
384-
for from_to in v_map:
385-
all_variables[from_to[1]] = prior_step["variables"][from_to[0]]
385+
for tr in v_map:
386+
all_variables[tr.out] = prior_step["variables"][tr.in_]
386387

387388
_LOGGER.debug(
388389
"Index %s (%s) workflow_variables=%s",

workflow/workflow_validator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ def _validate_tag_level(
123123
.get("variable")
124124
):
125125
found: bool = False
126-
for variable_map in get_step_workflow_variable_mapping(step=step):
127-
if replicate_using_input == variable_map[0]:
126+
for translation in get_step_workflow_variable_mapping(step=step):
127+
if replicate_using_input == translation.out:
128128
found = True
129129
break
130130
if not found:
131131
for (
132132
step_name,
133-
variable_map_list,
133+
translations,
134134
) in get_step_prior_step_variable_mapping(step=step).items():
135-
for variable_map in variable_map_list:
136-
if replicate_using_input == variable_map[0]:
135+
for translation in translations:
136+
if replicate_using_input == translation.out:
137137
found = True
138138
break
139139
if found:

0 commit comments

Comments
 (0)