Skip to content

Commit e780df9

Browse files
committed
feat: Allow normal format string behavior for cancelation mode Per review feedback on openjd-specifications PR #148, a format-string cancelation mode is no longer restricted to a single whole-field "{{ ... }}" expression. Any format string is statically valid (gated on FEATURE_BUNDLE_1); the resolved value is checked against the two mode names at run time. Whole-field expressions keep their string? null semantics: a null result drops the cancelation object. Mirrors the matching change in openjd-rs.
1 parent fe4200a commit e780df9

2 files changed

Lines changed: 34 additions & 45 deletions

File tree

‎src/openjd/model/v2023_09/_model.py‎

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ class CancelationMethodTerminate(OpenJDModel_v2023_09):
368368

369369

370370
class CancelationMethodDeferred(OpenJDModel_v2023_09):
371-
"""A cancelation whose ``mode`` is a whole-field format string, resolved
372-
at run time (Template Schemas 5.3, FEATURE_BUNDLE_1 extension).
371+
"""A cancelation whose ``mode`` is a format string, resolved at run
372+
time (Template Schemas 5.3, FEATURE_BUNDLE_1 extension).
373373
374374
What is the problem this solves?
375375
@@ -390,35 +390,38 @@ class CancelationMethodDeferred(OpenJDModel_v2023_09):
390390
wrap hooks impossible (pydantic's discriminated union rejected the
391391
template with "does not match any of the expected tags").
392392
393-
The fix is this class: the parser accepts a whole-field ``{{...}}``
394-
expression in ``mode`` as a third, "decided later" state (gated on the
395-
FEATURE_BUNDLE_1 extension), and the shape decision moves to resolution
396-
time, right before the action runs:
393+
The fix is this class: the parser accepts a format string in ``mode``
394+
as a third, "decided later" state (gated on the FEATURE_BUNDLE_1
395+
extension), and the shape decision moves to resolution time, right
396+
before the action runs:
397397
398398
1. The runtime seeds ``WrappedAction.Cancelation.Mode`` from the
399399
wrapped action (``"TERMINATE"``, ``"NOTIFY_THEN_TERMINATE"``, or
400400
``None``).
401401
2. It resolves the ``mode:`` expression against that.
402402
3. ``"TERMINATE"``/``"NOTIFY_THEN_TERMINATE"`` — the cancelation block
403403
now acts as that method, and its sibling fields are validated
404-
against that shape. ``None`` (null) — the whole ``cancelation:``
405-
block is treated as never written. Anything else — the action fails.
404+
against that shape. ``None`` (null, whole-field expressions only) —
405+
the whole ``cancelation:`` block is treated as never written.
406+
Anything else — the action fails.
406407
407408
Static validation is *not* deferred: at parse time the validator still
408-
checks the expression is well-formed, that ``WrappedAction.*`` is only
409-
referenced inside wrap hooks, and that the value is a single
410-
whole-field expression. You just can't know *which* of the two modes
411-
it'll be until the wrapped action is in front of you — which is
412-
inherent to forwarding: the same wrap environment gets reused across
413-
many steps whose cancelation settings differ.
409+
checks the expression is well-formed and that ``WrappedAction.*`` is
410+
only referenced inside wrap hooks. Any format string is accepted —
411+
normal interpolation like ``"{{Prefix}}_THEN_TERMINATE"`` is permitted;
412+
only the resolved value is constrained. You just can't know *which* of
413+
the two modes it'll be until the wrapped action is in front of you —
414+
which is inherent to forwarding: the same wrap environment gets reused
415+
across many steps whose cancelation settings differ.
414416
415417
Mirrors ``CancelationMode::DeferredMode`` in openjd-rs. See
416418
openjd-specifications Template Schemas 5.3 and RFC 0008 "Cancelation
417419
behavior".
418420
419421
Attributes:
420-
mode (FormatString): A single whole-field interpolation expression
421-
resolving to "TERMINATE", "NOTIFY_THEN_TERMINATE", or null.
422+
mode (FormatString): A format string resolving to "TERMINATE" or
423+
"NOTIFY_THEN_TERMINATE"; a whole-field interpolation expression
424+
may also resolve to null.
422425
notifyPeriodInSeconds (Optional[Union[int, FormatString]]): As on
423426
CancelationMethodNotifyThenTerminate; only meaningful when the
424427
mode resolves to NOTIFY_THEN_TERMINATE, and must resolve to
@@ -439,24 +442,11 @@ def _validate_mode(cls, v: Any, info: ValidationInfo) -> Any:
439442
raise ValueError(
440443
"a format string in cancelation mode requires the FEATURE_BUNDLE_1 extension."
441444
)
442-
raw = v.strip()
443-
# The mode is the schema selector, so partial interpolation
444-
# like "TERMIN{{X}}" has no meaningful semantics: require a
445-
# literal or a single whole-field expression. Exactly one "{{"
446-
# AND exactly one "}}", anchored at both ends — counting only
447-
# "{{" would accept partial interpolations like "{{X}}Y}}"
448-
# (one open, two closes), which can never resolve to a valid
449-
# mode.
450-
if not (
451-
raw.startswith("{{")
452-
and raw.endswith("}}")
453-
and raw.count("{{") == 1
454-
and raw.count("}}") == 1
455-
):
456-
raise ValueError(
457-
"cancelation mode must be a literal or a single "
458-
'whole-field "{{ ... }}" expression.'
459-
)
445+
# Any format string is permitted (normal format string
446+
# behavior, Template Schemas 5.3); the resolved value is
447+
# checked against the two mode names at run time. Only a
448+
# whole-field expression additionally gets string? null
449+
# semantics (a null result drops the cancelation object).
460450
return v
461451

462452
@field_validator("notifyPeriodInSeconds", mode="before")
@@ -583,7 +573,7 @@ class Action(OpenJDModel_v2023_09):
583573
cancelation (Optional[CancelationMethod]): If defined, provides details
584574
regarding how this action should be canceled. One of
585575
CancelationMethodNotifyThenTerminate, CancelationMethodTerminate, or
586-
CancelationMethodDeferred (a whole-field format-string mode resolved
576+
CancelationMethodDeferred (a format-string mode resolved
587577
at run time; FEATURE_BUNDLE_1).
588578
Default: CancelationMethodTerminate
589579
"""

‎test/openjd/model_v0/v2023_09/test_wrap_actions.py‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,19 +384,18 @@ def test_fmtstring_mode_requires_feature_bundle_1(self):
384384
"mode",
385385
[
386386
pytest.param("TERMIN{{WrappedAction.Cancelation.Mode}}", id="leading-text"),
387-
# Starts with "{{", ends with "}}", exactly one "{{" — but it is
388-
# the expression followed by literal text "Y}}"; the guard must
389-
# count closing braces too.
390-
pytest.param("{{WrappedAction.Cancelation.Mode}}Y}}", id="trailing-text"),
387+
pytest.param("{{ 'NOTIFY' }}_THEN_TERMINATE", id="trailing-text"),
391388
],
392389
)
393-
def test_partial_fmtstring_mode_rejected(self, mode: str):
394-
# The mode must be a literal or a single whole-field expression;
395-
# partial interpolation has no meaningful semantics for a schema
396-
# selector.
390+
def test_partial_fmtstring_mode_accepted(self, mode: str):
391+
# A format-string mode gets normal format string behavior
392+
# (Template Schemas 5.3): partial interpolation is statically
393+
# valid, and the resolved value is checked against the two mode
394+
# names at run time. Only a whole-field expression additionally
395+
# gets string? null semantics (a null result drops the
396+
# cancelation object).
397397
tmpl = _env_template(
398398
self._wrap_actions({"mode": mode}),
399399
extensions=self._ROUND_TRIP_EXTS,
400400
)
401-
with pytest.raises(DecodeValidationError, match="whole-field"):
402-
_decode(tmpl, extensions=self._ROUND_TRIP_EXTS)
401+
_decode(tmpl, extensions=self._ROUND_TRIP_EXTS)

0 commit comments

Comments
 (0)