Skip to content

Commit e4fb4ff

Browse files
committed
Convert outbound links to pascal case
1 parent 2902c17 commit e4fb4ff

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

temporalio/nexus/_operation_context.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,7 @@ def _workflow_event_to_nexus_link(
514514
workflow_id = urllib.parse.quote(workflow_event.workflow_id)
515515
run_id = urllib.parse.quote(workflow_event.run_id)
516516
path = f"/namespaces/{namespace}/workflows/{workflow_id}/{run_id}/history"
517-
query_params = urllib.parse.urlencode(
518-
{
519-
"eventType": temporalio.api.enums.v1.EventType.Name(
520-
workflow_event.event_ref.event_type
521-
),
522-
"referenceType": "EventReference",
523-
}
524-
)
517+
query_params = _query_params_from_event_reference(workflow_event.event_ref)
525518
return nexusrpc.Link(
526519
url=urllib.parse.urlunparse((scheme, "", path, "", query_params, "")),
527520
type=workflow_event.DESCRIPTOR.full_name,
@@ -555,6 +548,19 @@ def _nexus_link_to_workflow_event(
555548
)
556549

557550

551+
def _query_params_from_event_reference(
552+
event_ref: temporalio.api.common.v1.Link.WorkflowEvent.EventReference,
553+
) -> str:
554+
event_type_name = temporalio.api.enums.v1.EventType.Name(event_ref.event_type)
555+
if event_type_name.startswith("EVENT_TYPE_"):
556+
event_type_name = _constant_case_to_pascal_case(
557+
event_type_name.removeprefix("EVENT_TYPE_")
558+
)
559+
return urllib.parse.urlencode(
560+
{"eventType": event_type_name, "referenceType": "EventReference"}
561+
)
562+
563+
558564
def _event_reference_from_query_params(
559565
raw_query_params: str,
560566
) -> temporalio.api.common.v1.Link.WorkflowEvent.EventReference:
@@ -596,6 +602,16 @@ def _event_reference_from_query_params(
596602
)
597603

598604

605+
def _constant_case_to_pascal_case(s: str) -> str:
606+
"""
607+
Convert a CONSTANT_CASE string to PascalCase.
608+
609+
>>> _constant_case_to_pascal_case("NEXUS_OPERATION_SCHEDULED")
610+
"NexusOperationScheduled"
611+
"""
612+
return re.sub(r"(\b|_)([a-z])", lambda m: m.groups()[1].upper(), s.lower())
613+
614+
599615
def _pascal_case_to_constant_case(s: str) -> str:
600616
"""
601617
Convert a PascalCase string to CONSTANT_CASE.

tests/nexus/test_workflow_caller.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,9 +1277,19 @@ async def test_workflow_run_operation_overloads(
12771277

12781278

12791279
def test_link_conversion_utilities():
1280-
pc2cc = temporalio.nexus._operation_context._pascal_case_to_constant_case
1281-
assert pc2cc("") == ""
1282-
assert pc2cc("A") == "A"
1283-
assert pc2cc("a") == "A"
1284-
assert pc2cc("AbCd") == "AB_CD"
1285-
assert pc2cc("NexusOperationScheduled") == "NEXUS_OPERATION_SCHEDULED"
1280+
p2c = temporalio.nexus._operation_context._pascal_case_to_constant_case
1281+
c2p = temporalio.nexus._operation_context._constant_case_to_pascal_case
1282+
1283+
for p, c in [
1284+
("", ""),
1285+
("A", "A"),
1286+
("Ab", "AB"),
1287+
("AbCd", "AB_CD"),
1288+
("AbCddE", "AB_CDD_E"),
1289+
("NexusOperationScheduled", "NEXUS_OPERATION_SCHEDULED"),
1290+
]:
1291+
assert p2c(p) == c
1292+
assert c2p(c) == p
1293+
1294+
assert p2c("a") == "A"
1295+
assert c2p("A") == "A"

0 commit comments

Comments
 (0)