Skip to content

Commit

Permalink
Enhance Placeholder (#12692)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 357849a077800bf98c19438377a866daf240235b
  • Loading branch information
stephencpope authored and Descartes Labs Build committed Oct 4, 2024
1 parent 938ec51 commit 3cbc295
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
36 changes: 32 additions & 4 deletions descarteslabs/core/catalog/event_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,44 @@ class Placeholder:
detail template.
"""

def __init__(self, text: str):
def __init__(self, text: str, unquoted=False, raw=False):
"""Create a Placeholder object.
By default when unquoted and raw are both False, the text will be rendered as a
string value with the text substituted from the event context.
For example, ``Placeholder("event.detail.id")`` will render as ``"some-id"``.
If unquoted is True, the text will be rendered without enclosing quotes
(typically for a numeric value, JSON object or array). For example,
``Placeholder("event.detail.geometry", unquoted=True)`` will render as
``{"type": "Polygon", "coordinates": [[[0, 0,], [1, 0], [1, 1], [0, 1], [0, 0]]]}``.
If raw is True, then the text will be rendered by Jinja2 as is, without
introducing any additional quotes or substitutions. For example,
``Placeholder('"{{ event.detail.id }}"', raw=True)`` will render as `"some-id"`.
In all cases, the final result after all substitions must be a fragment of
a valid JSON string.
Parameters
----------
text : str
The text to be rendered into the resulting JSON detail template. Typically
a Jinja2 template substitution string.
The text to be rendered into the resulting JSON detail template. How it is
handled depends on the `unquoted` and `raw` parameters.
unquoted: bool, optional
If False, the text will be rendered as a string value. If False,
then the text will be rendered without enclosing quotes. Defaults to False.
Ignored if `raw` is True.
raw : bool, optional
If True, the text will be rendered as is, without wrapping as a substitution
or a string. Defaults to False.
"""
self.text = text
if raw:
self.text = text
elif unquoted:
self.text = f"{{{{ {text} }}}}"
else:
self.text = f'"{{{{ {text} }}}}"'

@classmethod
def json_serialize(cls, obj, placeholders=None):
Expand Down
7 changes: 4 additions & 3 deletions descarteslabs/core/catalog/tests/test_event_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,14 +695,15 @@ def test_delete_non_existent(self):
def test_compute_target(self):
target = EventSubscriptionComputeTarget(
"some-function-id",
"{{ event.detail.id }}",
detail=Placeholder("{{ event.detail }}"),
Placeholder("event.detail.id"),
detail=Placeholder("event.detail", unquoted=True),
source=Placeholder('"{{ event.source }}"', raw=True),
)
assert isinstance(target, EventSubscriptionTarget)
assert target.rule_id == "descarteslabs:compute-job-create"
assert (
target.detail_template
== '{"body": {"function_id": "some-function-id", "args": ["{{ event.detail.id }}"], "kwargs": {"detail": {{ event.detail }}}}}' # noqa: E501
== '{"body": {"function_id": "some-function-id", "args": ["{{ event.detail.id }}"], "kwargs": {"detail": {{ event.detail }}, "source": "{{ event.source }}"}}}' # noqa: E501
)

def test_scheduled_event_subscription(self):
Expand Down

0 comments on commit 3cbc295

Please sign in to comment.