-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add data converter features in Python (#81) #306
Merged
antlai-temporal
merged 2 commits into
temporalio:main
from
antlai-temporal:data-converter-python
Jul 10, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ node_modules | |
|
||
# Python stuff | ||
__pycache__ | ||
pyrightconfig.json | ||
|
||
# Build stuff | ||
bin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import dataclasses | ||
|
||
from temporalio import workflow | ||
from temporalio.api.common.v1 import DataBlob | ||
from temporalio.client import WorkflowHandle | ||
from temporalio.converter import ( | ||
BinaryNullPayloadConverter, | ||
BinaryProtoPayloadConverter, | ||
CompositePayloadConverter, | ||
DataConverter, | ||
) | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
EXPECTED_RESULT = DataBlob(data=bytes.fromhex("deadbeef")) | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: DataBlob) -> DataBlob: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is DataBlob `0xdeadbeef` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "binary/protobuf" | ||
|
||
message_type = payload.metadata["messageType"].decode("utf-8") | ||
assert message_type == "temporal.api.common.v1.DataBlob" | ||
|
||
result_in_history = DataBlob() | ||
result_in_history.ParseFromString(payload.data) | ||
assert result == result_in_history | ||
|
||
payload_arg = await get_workflow_argument_payload(handle) | ||
assert payload == payload_arg | ||
|
||
|
||
class DefaultBinProtoPayloadConverter(CompositePayloadConverter): | ||
def __init__(self) -> None: | ||
super().__init__( | ||
# Disable ByteSlice, ProtoJSON, and JSON converters | ||
BinaryNullPayloadConverter(), | ||
BinaryProtoPayloadConverter(), | ||
) | ||
|
||
|
||
register_feature( | ||
workflows=[Workflow], | ||
check_result=check_result, | ||
start_options={"arg": EXPECTED_RESULT}, | ||
data_converter=dataclasses.replace( | ||
DataConverter.default, payload_converter_class=DefaultBinProtoPayloadConverter | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import base64 | ||
import dataclasses | ||
import json | ||
from typing import Dict, List, Sequence | ||
|
||
from temporalio import workflow | ||
from temporalio.api.common.v1 import Payload | ||
from temporalio.client import WorkflowHandle | ||
from temporalio.converter import DataConverter, PayloadCodec | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
Result = Dict[str, bool] | ||
|
||
EXPECTED_RESULT: Result = {"spec": True} | ||
|
||
CODEC_ENCODING = "my_encoding" | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: Result) -> Result: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is `{"spec": true}` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == CODEC_ENCODING | ||
|
||
extractedData = base64.b64decode(payload.data) | ||
innerPayload = Payload() | ||
innerPayload.ParseFromString(extractedData) | ||
|
||
encoding = innerPayload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "json/plain" | ||
|
||
result_in_history = json.loads(innerPayload.data) | ||
assert result == result_in_history | ||
|
||
payload_arg = await get_workflow_argument_payload(handle) | ||
assert payload == payload_arg | ||
|
||
|
||
# Based on samples-python/encryption/codec.py | ||
class Base64PayloadCodec(PayloadCodec): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
async def encode(self, payloads: Sequence[Payload]) -> List[Payload]: | ||
return [ | ||
Payload( | ||
metadata={ | ||
"encoding": b"my_encoding", | ||
}, | ||
data=base64.b64encode(p.SerializeToString()), | ||
) | ||
for p in payloads | ||
] | ||
|
||
async def decode(self, payloads: Sequence[Payload]) -> List[Payload]: | ||
ret: List[Payload] = [] | ||
for p in payloads: | ||
if p.metadata.get("encoding", b"").decode() != CODEC_ENCODING: | ||
ret.append(p) | ||
continue | ||
ret.append(Payload.FromString(base64.b64decode(p.data))) | ||
return ret | ||
|
||
|
||
register_feature( | ||
workflows=[Workflow], | ||
check_result=check_result, | ||
start_options={"arg": EXPECTED_RESULT}, | ||
data_converter=dataclasses.replace( | ||
DataConverter.default, | ||
payload_codec=Base64PayloadCodec(), | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import json | ||
from typing import Dict | ||
|
||
from temporalio import workflow | ||
from temporalio.client import WorkflowHandle | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
Result = Dict[str, bool] | ||
|
||
EXPECTED_RESULT: Result = {"spec": True} | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: Result) -> Result: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is `{"spec": true}` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "json/plain" | ||
|
||
result_in_history = json.loads(payload.data) | ||
assert result == result_in_history | ||
|
||
payload_arg = await get_workflow_argument_payload(handle) | ||
assert payload == payload_arg | ||
|
||
|
||
register_feature( | ||
workflows=[Workflow], | ||
check_result=check_result, | ||
start_options={"arg": EXPECTED_RESULT}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from temporalio import workflow | ||
from temporalio.api.common.v1 import DataBlob | ||
from temporalio.client import WorkflowHandle | ||
from temporalio.converter import JSONProtoPayloadConverter | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
EXPECTED_RESULT = DataBlob(data=bytes.fromhex("deadbeef")) | ||
JSONP_decoder = JSONProtoPayloadConverter() | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: DataBlob) -> DataBlob: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is DataBlob `0xdeadbeef` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "json/protobuf" | ||
|
||
message_type = payload.metadata["messageType"].decode("utf-8") | ||
assert message_type == "temporal.api.common.v1.DataBlob" | ||
|
||
result_in_history = JSONP_decoder.from_payload(payload) | ||
assert result == result_in_history | ||
|
||
payload_arg = await get_workflow_argument_payload(handle) | ||
assert payload == payload_arg | ||
|
||
|
||
register_feature( | ||
workflows=[Workflow], | ||
check_result=check_result, | ||
start_options={"arg": EXPECTED_RESULT}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably don't need this alias