temporalio 0.2.0-aarch64-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +23 -0
- data/Rakefile +387 -0
- data/lib/temporalio/activity/complete_async_error.rb +11 -0
- data/lib/temporalio/activity/context.rb +107 -0
- data/lib/temporalio/activity/definition.rb +77 -0
- data/lib/temporalio/activity/info.rb +63 -0
- data/lib/temporalio/activity.rb +69 -0
- data/lib/temporalio/api/batch/v1/message.rb +31 -0
- data/lib/temporalio/api/cloud/cloudservice/v1/request_response.rb +93 -0
- data/lib/temporalio/api/cloud/cloudservice/v1/service.rb +25 -0
- data/lib/temporalio/api/cloud/cloudservice.rb +3 -0
- data/lib/temporalio/api/cloud/identity/v1/message.rb +36 -0
- data/lib/temporalio/api/cloud/namespace/v1/message.rb +35 -0
- data/lib/temporalio/api/cloud/operation/v1/message.rb +27 -0
- data/lib/temporalio/api/cloud/region/v1/message.rb +23 -0
- data/lib/temporalio/api/command/v1/message.rb +46 -0
- data/lib/temporalio/api/common/v1/grpc_status.rb +23 -0
- data/lib/temporalio/api/common/v1/message.rb +41 -0
- data/lib/temporalio/api/enums/v1/batch_operation.rb +22 -0
- data/lib/temporalio/api/enums/v1/command_type.rb +21 -0
- data/lib/temporalio/api/enums/v1/common.rb +26 -0
- data/lib/temporalio/api/enums/v1/event_type.rb +21 -0
- data/lib/temporalio/api/enums/v1/failed_cause.rb +26 -0
- data/lib/temporalio/api/enums/v1/namespace.rb +23 -0
- data/lib/temporalio/api/enums/v1/query.rb +22 -0
- data/lib/temporalio/api/enums/v1/reset.rb +23 -0
- data/lib/temporalio/api/enums/v1/schedule.rb +21 -0
- data/lib/temporalio/api/enums/v1/task_queue.rb +25 -0
- data/lib/temporalio/api/enums/v1/update.rb +22 -0
- data/lib/temporalio/api/enums/v1/workflow.rb +30 -0
- data/lib/temporalio/api/errordetails/v1/message.rb +42 -0
- data/lib/temporalio/api/export/v1/message.rb +24 -0
- data/lib/temporalio/api/failure/v1/message.rb +35 -0
- data/lib/temporalio/api/filter/v1/message.rb +27 -0
- data/lib/temporalio/api/history/v1/message.rb +90 -0
- data/lib/temporalio/api/namespace/v1/message.rb +31 -0
- data/lib/temporalio/api/nexus/v1/message.rb +40 -0
- data/lib/temporalio/api/operatorservice/v1/request_response.rb +49 -0
- data/lib/temporalio/api/operatorservice/v1/service.rb +23 -0
- data/lib/temporalio/api/operatorservice.rb +3 -0
- data/lib/temporalio/api/protocol/v1/message.rb +23 -0
- data/lib/temporalio/api/query/v1/message.rb +27 -0
- data/lib/temporalio/api/replication/v1/message.rb +26 -0
- data/lib/temporalio/api/schedule/v1/message.rb +42 -0
- data/lib/temporalio/api/sdk/v1/enhanced_stack_trace.rb +25 -0
- data/lib/temporalio/api/sdk/v1/task_complete_metadata.rb +21 -0
- data/lib/temporalio/api/sdk/v1/user_metadata.rb +23 -0
- data/lib/temporalio/api/sdk/v1/workflow_metadata.rb +23 -0
- data/lib/temporalio/api/taskqueue/v1/message.rb +45 -0
- data/lib/temporalio/api/update/v1/message.rb +33 -0
- data/lib/temporalio/api/version/v1/message.rb +26 -0
- data/lib/temporalio/api/workflow/v1/message.rb +43 -0
- data/lib/temporalio/api/workflowservice/v1/request_response.rb +189 -0
- data/lib/temporalio/api/workflowservice/v1/service.rb +23 -0
- data/lib/temporalio/api/workflowservice.rb +3 -0
- data/lib/temporalio/api.rb +13 -0
- data/lib/temporalio/cancellation.rb +150 -0
- data/lib/temporalio/client/activity_id_reference.rb +32 -0
- data/lib/temporalio/client/async_activity_handle.rb +110 -0
- data/lib/temporalio/client/connection/cloud_service.rb +648 -0
- data/lib/temporalio/client/connection/operator_service.rb +249 -0
- data/lib/temporalio/client/connection/service.rb +41 -0
- data/lib/temporalio/client/connection/workflow_service.rb +1218 -0
- data/lib/temporalio/client/connection.rb +270 -0
- data/lib/temporalio/client/interceptor.rb +316 -0
- data/lib/temporalio/client/workflow_execution.rb +103 -0
- data/lib/temporalio/client/workflow_execution_count.rb +36 -0
- data/lib/temporalio/client/workflow_execution_status.rb +18 -0
- data/lib/temporalio/client/workflow_handle.rb +446 -0
- data/lib/temporalio/client/workflow_query_reject_condition.rb +14 -0
- data/lib/temporalio/client/workflow_update_handle.rb +67 -0
- data/lib/temporalio/client/workflow_update_wait_stage.rb +17 -0
- data/lib/temporalio/client.rb +404 -0
- data/lib/temporalio/common_enums.rb +24 -0
- data/lib/temporalio/converters/data_converter.rb +102 -0
- data/lib/temporalio/converters/failure_converter.rb +200 -0
- data/lib/temporalio/converters/payload_codec.rb +26 -0
- data/lib/temporalio/converters/payload_converter/binary_null.rb +34 -0
- data/lib/temporalio/converters/payload_converter/binary_plain.rb +35 -0
- data/lib/temporalio/converters/payload_converter/binary_protobuf.rb +42 -0
- data/lib/temporalio/converters/payload_converter/composite.rb +62 -0
- data/lib/temporalio/converters/payload_converter/encoding.rb +35 -0
- data/lib/temporalio/converters/payload_converter/json_plain.rb +44 -0
- data/lib/temporalio/converters/payload_converter/json_protobuf.rb +41 -0
- data/lib/temporalio/converters/payload_converter.rb +73 -0
- data/lib/temporalio/converters.rb +9 -0
- data/lib/temporalio/error/failure.rb +219 -0
- data/lib/temporalio/error.rb +147 -0
- data/lib/temporalio/internal/bridge/3.1/temporalio_bridge.so +0 -0
- data/lib/temporalio/internal/bridge/3.2/temporalio_bridge.so +0 -0
- data/lib/temporalio/internal/bridge/3.3/temporalio_bridge.so +0 -0
- data/lib/temporalio/internal/bridge/api/activity_result/activity_result.rb +34 -0
- data/lib/temporalio/internal/bridge/api/activity_task/activity_task.rb +31 -0
- data/lib/temporalio/internal/bridge/api/child_workflow/child_workflow.rb +33 -0
- data/lib/temporalio/internal/bridge/api/common/common.rb +26 -0
- data/lib/temporalio/internal/bridge/api/core_interface.rb +36 -0
- data/lib/temporalio/internal/bridge/api/external_data/external_data.rb +27 -0
- data/lib/temporalio/internal/bridge/api/workflow_activation/workflow_activation.rb +52 -0
- data/lib/temporalio/internal/bridge/api/workflow_commands/workflow_commands.rb +54 -0
- data/lib/temporalio/internal/bridge/api/workflow_completion/workflow_completion.rb +30 -0
- data/lib/temporalio/internal/bridge/api.rb +3 -0
- data/lib/temporalio/internal/bridge/client.rb +90 -0
- data/lib/temporalio/internal/bridge/runtime.rb +53 -0
- data/lib/temporalio/internal/bridge/testing.rb +46 -0
- data/lib/temporalio/internal/bridge/worker.rb +83 -0
- data/lib/temporalio/internal/bridge.rb +36 -0
- data/lib/temporalio/internal/client/implementation.rb +525 -0
- data/lib/temporalio/internal/proto_utils.rb +54 -0
- data/lib/temporalio/internal/worker/activity_worker.rb +345 -0
- data/lib/temporalio/internal/worker/multi_runner.rb +169 -0
- data/lib/temporalio/internal.rb +7 -0
- data/lib/temporalio/retry_policy.rb +51 -0
- data/lib/temporalio/runtime.rb +271 -0
- data/lib/temporalio/scoped_logger.rb +96 -0
- data/lib/temporalio/search_attributes.rb +300 -0
- data/lib/temporalio/testing/activity_environment.rb +132 -0
- data/lib/temporalio/testing/workflow_environment.rb +137 -0
- data/lib/temporalio/testing.rb +10 -0
- data/lib/temporalio/version.rb +5 -0
- data/lib/temporalio/worker/activity_executor/fiber.rb +49 -0
- data/lib/temporalio/worker/activity_executor/thread_pool.rb +254 -0
- data/lib/temporalio/worker/activity_executor.rb +55 -0
- data/lib/temporalio/worker/interceptor.rb +88 -0
- data/lib/temporalio/worker/tuner.rb +151 -0
- data/lib/temporalio/worker.rb +426 -0
- data/lib/temporalio/workflow_history.rb +22 -0
- data/lib/temporalio.rb +7 -0
- data/temporalio.gemspec +28 -0
- metadata +191 -0
@@ -0,0 +1,200 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'temporalio/api'
|
4
|
+
require 'temporalio/error'
|
5
|
+
require 'temporalio/internal/proto_utils'
|
6
|
+
|
7
|
+
module Temporalio
|
8
|
+
module Converters
|
9
|
+
# Base class for converting Ruby errors to/from Temporal failures.
|
10
|
+
class FailureConverter
|
11
|
+
# @return [FailureConverter] Default failure converter.
|
12
|
+
def self.default
|
13
|
+
@default ||= Ractor.make_shareable(FailureConverter.new)
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Boolean] If +true+, the message and stack trace of the failure will be moved into the encoded attribute
|
17
|
+
# section of the failure which can be encoded with a codec.
|
18
|
+
attr_reader :encode_common_attributes
|
19
|
+
|
20
|
+
# Create failure converter.
|
21
|
+
#
|
22
|
+
# @param encode_common_attributes [Boolean] If +true+, the message and stack trace of the failure will be moved
|
23
|
+
# into the encoded attribute section of the failure which can be encoded with a codec.
|
24
|
+
def initialize(encode_common_attributes: false)
|
25
|
+
@encode_common_attributes = encode_common_attributes
|
26
|
+
end
|
27
|
+
|
28
|
+
# Convert a Ruby error to a Temporal failure.
|
29
|
+
#
|
30
|
+
# @param error [Exception] Ruby error.
|
31
|
+
# @param converter [DataConverter, PayloadConverter] Converter for payloads.
|
32
|
+
# @return [Api::Failure::V1::Failure] Converted failure.
|
33
|
+
def to_failure(error, converter)
|
34
|
+
failure = Api::Failure::V1::Failure.new(
|
35
|
+
message: error.message,
|
36
|
+
stack_trace: error.backtrace&.join("\n")
|
37
|
+
)
|
38
|
+
cause = error.cause
|
39
|
+
failure.cause = to_failure(cause, converter) if cause
|
40
|
+
|
41
|
+
# Convert specific error type details
|
42
|
+
case error
|
43
|
+
when Error::ApplicationError
|
44
|
+
failure.application_failure_info = Api::Failure::V1::ApplicationFailureInfo.new(
|
45
|
+
type: error.type,
|
46
|
+
non_retryable: error.non_retryable,
|
47
|
+
details: converter.to_payloads(error.details),
|
48
|
+
next_retry_delay: Internal::ProtoUtils.seconds_to_duration(error.next_retry_delay)
|
49
|
+
)
|
50
|
+
when Error::TimeoutError
|
51
|
+
failure.timeout_failure_info = Api::Failure::V1::TimeoutFailureInfo.new(
|
52
|
+
timeout_type: error.type,
|
53
|
+
last_heartbeat_details: converter.to_payloads(error.last_heartbeat_details)
|
54
|
+
)
|
55
|
+
when Error::CanceledError
|
56
|
+
failure.canceled_failure_info = Api::Failure::V1::CanceledFailureInfo.new(
|
57
|
+
details: converter.to_payloads(error.details)
|
58
|
+
)
|
59
|
+
when Error::TerminatedError
|
60
|
+
failure.terminated_failure_info = Api::Failure::V1::TerminatedFailureInfo.new
|
61
|
+
when Error::ServerError
|
62
|
+
failure.server_failure_info = Api::Failure::V1::ServerFailureInfo.new(
|
63
|
+
non_retryable: error.non_retryable
|
64
|
+
)
|
65
|
+
when Error::ActivityError
|
66
|
+
failure.activity_failure_info = Api::Failure::V1::ActivityFailureInfo.new(
|
67
|
+
scheduled_event_id: error.scheduled_event_id,
|
68
|
+
started_event_id: error.started_event_id,
|
69
|
+
identity: error.identity,
|
70
|
+
activity_type: Api::Common::V1::ActivityType.new(name: error.activity_type),
|
71
|
+
activity_id: error.activity_id,
|
72
|
+
retry_state: error.retry_state
|
73
|
+
)
|
74
|
+
when Error::ChildWorkflowError
|
75
|
+
failure.child_workflow_execution_failure_info = Api::Failure::V1::ChildWorkflowExecutionFailureInfo.new(
|
76
|
+
namespace: error.namespace,
|
77
|
+
workflow_execution: Api::Common::V1::WorkflowExecution.new(
|
78
|
+
workflow_id: error.workflow_id,
|
79
|
+
run_id: error.run_id
|
80
|
+
),
|
81
|
+
workflow_type: Api::Common::V1::WorkflowType.new(name: error.workflow_type),
|
82
|
+
initiated_event_id: error.initiated_event_id,
|
83
|
+
started_event_id: error.started_event_id,
|
84
|
+
retry_state: error.retry_state
|
85
|
+
)
|
86
|
+
else
|
87
|
+
failure.application_failure_info = Api::Failure::V1::ApplicationFailureInfo.new(
|
88
|
+
type: error.class.name.split('::').last
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
# If encoding common attributes, move message and stack trace
|
93
|
+
if @encode_common_attributes
|
94
|
+
failure.encoded_attributes = converter.to_payload(
|
95
|
+
{ message: failure.message, stack_trace: failure.stack_trace }
|
96
|
+
)
|
97
|
+
failure.message = 'Encoded failure'
|
98
|
+
failure.stack_trace = ''
|
99
|
+
end
|
100
|
+
|
101
|
+
failure
|
102
|
+
end
|
103
|
+
|
104
|
+
# Convert a Temporal failure to a Ruby error.
|
105
|
+
#
|
106
|
+
# @param failure [Api::Failure::V1::Failure] Failure.
|
107
|
+
# @param converter [DataConverter, PayloadConverter] Converter for payloads.
|
108
|
+
# @return [Error::Failure] Converted Ruby error.
|
109
|
+
def from_failure(failure, converter)
|
110
|
+
# If encoded attributes have any of the fields we expect, try to decode
|
111
|
+
# but ignore any error
|
112
|
+
unless failure.encoded_attributes.nil?
|
113
|
+
begin
|
114
|
+
attrs = converter.from_payload(failure.encoded_attributes)
|
115
|
+
if attrs.is_a?(Hash)
|
116
|
+
# Shallow dup failure here to avoid affecting caller
|
117
|
+
failure = failure.dup
|
118
|
+
failure.message = attrs['message'] if attrs.key?('message')
|
119
|
+
failure.stack_trace = attrs['stack_trace'] if attrs.key?('stack_trace')
|
120
|
+
end
|
121
|
+
rescue StandardError
|
122
|
+
# Ignore failures
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Convert
|
127
|
+
error = if failure.application_failure_info
|
128
|
+
Error::ApplicationError.new(
|
129
|
+
Internal::ProtoUtils.string_or(failure.message, 'Application error'),
|
130
|
+
*converter.from_payloads(failure.application_failure_info.details),
|
131
|
+
type: Internal::ProtoUtils.string_or(failure.application_failure_info.type),
|
132
|
+
non_retryable: failure.application_failure_info.non_retryable,
|
133
|
+
next_retry_delay: failure.application_failure_info.next_retry_delay&.to_f
|
134
|
+
)
|
135
|
+
elsif failure.timeout_failure_info
|
136
|
+
Error::TimeoutError.new(
|
137
|
+
Internal::ProtoUtils.string_or(failure.message, 'Timeout'),
|
138
|
+
type: Internal::ProtoUtils.enum_to_int(Api::Enums::V1::TimeoutType,
|
139
|
+
failure.timeout_failure_info.timeout_type),
|
140
|
+
last_heartbeat_details: converter.from_payloads(
|
141
|
+
failure.timeout_failure_info.last_heartbeat_details
|
142
|
+
)
|
143
|
+
)
|
144
|
+
elsif failure.canceled_failure_info
|
145
|
+
Error::CanceledError.new(
|
146
|
+
Internal::ProtoUtils.string_or(failure.message, 'Canceled'),
|
147
|
+
details: converter.from_payloads(failure.canceled_failure_info.details)
|
148
|
+
)
|
149
|
+
elsif failure.terminated_failure_info
|
150
|
+
Error::TerminatedError.new(
|
151
|
+
Internal::ProtoUtils.string_or(failure.message, 'Terminated'),
|
152
|
+
details: []
|
153
|
+
)
|
154
|
+
elsif failure.server_failure_info
|
155
|
+
Error::ServerError.new(
|
156
|
+
Internal::ProtoUtils.string_or(failure.message, 'Server error'),
|
157
|
+
non_retryable: failure.server_failure_info.non_retryable
|
158
|
+
)
|
159
|
+
elsif failure.activity_failure_info
|
160
|
+
Error::ActivityError.new(
|
161
|
+
Internal::ProtoUtils.string_or(failure.message, 'Activity error'),
|
162
|
+
scheduled_event_id: failure.activity_failure_info.scheduled_event_id,
|
163
|
+
started_event_id: failure.activity_failure_info.started_event_id,
|
164
|
+
identity: failure.activity_failure_info.identity,
|
165
|
+
activity_type: failure.activity_failure_info.activity_type.name,
|
166
|
+
activity_id: failure.activity_failure_info.activity_id,
|
167
|
+
retry_state: Internal::ProtoUtils.enum_to_int(
|
168
|
+
Api::Enums::V1::RetryState,
|
169
|
+
failure.activity_failure_info.retry_state,
|
170
|
+
zero_means_nil: true
|
171
|
+
)
|
172
|
+
)
|
173
|
+
elsif failure.child_workflow_execution_failure_info
|
174
|
+
Error::ChildWorkflowError.new(
|
175
|
+
Internal::ProtoUtils.string_or(failure.message, 'Child workflow error'),
|
176
|
+
namespace: failure.child_workflow_execution_failure_info.namespace,
|
177
|
+
workflow_id: failure.child_workflow_execution_failure_info.workflow_execution.workflow_id,
|
178
|
+
run_id: failure.child_workflow_execution_failure_info.workflow_execution.run_id,
|
179
|
+
workflow_type: failure.child_workflow_execution_failure_info.workflow_type.name,
|
180
|
+
initiated_event_id: failure.child_workflow_execution_failure_info.initiated_event_id,
|
181
|
+
started_event_id: failure.child_workflow_execution_failure_info.started_event_id,
|
182
|
+
retry_state: Internal::ProtoUtils.enum_to_int(
|
183
|
+
Api::Enums::V1::RetryState,
|
184
|
+
failure.child_workflow_execution_failure_info.retry_state,
|
185
|
+
zero_means_nil: true
|
186
|
+
)
|
187
|
+
)
|
188
|
+
else
|
189
|
+
Error::Failure.new(Internal::ProtoUtils.string_or(failure.message, 'Failure error'))
|
190
|
+
end
|
191
|
+
|
192
|
+
Error._with_backtrace_and_cause(
|
193
|
+
error,
|
194
|
+
backtrace: failure.stack_trace.split("\n"),
|
195
|
+
cause: failure.cause ? from_failure(failure.cause, converter) : nil
|
196
|
+
)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Temporalio
|
4
|
+
module Converters
|
5
|
+
# Base class for encoding and decoding payloads. Commonly used for encryption.
|
6
|
+
class PayloadCodec
|
7
|
+
# Encode the given payloads into a new set of payloads.
|
8
|
+
#
|
9
|
+
# @param payloads [Enumerable<Api::Common::V1::Payload>] Payloads to encode. This value should not be mutated.
|
10
|
+
# @return [Array<Api::Common::V1::Payload>] Encoded payloads. Note, this does not have to be the same number as
|
11
|
+
# payloads given, but it must be at least one and cannot be more than was given.
|
12
|
+
def encode(payloads)
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
# Decode the given payloads into a new set of payloads.
|
17
|
+
#
|
18
|
+
# @param payloads [Enumerable<Api::Common::V1::Payload>] Payloads to decode. This value should not be mutated.
|
19
|
+
# @return [Array<Api::Common::V1::Payload>] Decoded payloads. Note, this does not have to be the same number as
|
20
|
+
# payloads given, but it must be at least one and cannot be more than was given.
|
21
|
+
def decode(payloads)
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'temporalio/api'
|
4
|
+
require 'temporalio/converters/payload_converter/encoding'
|
5
|
+
|
6
|
+
module Temporalio
|
7
|
+
module Converters
|
8
|
+
class PayloadConverter
|
9
|
+
# Encoding for +nil+ values for +binary/null+ encoding.
|
10
|
+
class BinaryNull < Encoding
|
11
|
+
ENCODING = 'binary/null'
|
12
|
+
|
13
|
+
# (see Encoding.encoding)
|
14
|
+
def encoding
|
15
|
+
ENCODING
|
16
|
+
end
|
17
|
+
|
18
|
+
# (see Encoding.to_payload)
|
19
|
+
def to_payload(value)
|
20
|
+
return nil unless value.nil?
|
21
|
+
|
22
|
+
Api::Common::V1::Payload.new(
|
23
|
+
metadata: { 'encoding' => ENCODING }
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
# (see Encoding.from_payload)
|
28
|
+
def from_payload(payload) # rubocop:disable Lint/UnusedMethodArgument
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'temporalio/api'
|
4
|
+
require 'temporalio/converters/payload_converter/encoding'
|
5
|
+
|
6
|
+
module Temporalio
|
7
|
+
module Converters
|
8
|
+
class PayloadConverter
|
9
|
+
# Encoding for +ASCII_8BIT+ string values for +binary/plain+ encoding.
|
10
|
+
class BinaryPlain < Encoding
|
11
|
+
ENCODING = 'binary/plain'
|
12
|
+
|
13
|
+
# (see Encoding.encoding)
|
14
|
+
def encoding
|
15
|
+
ENCODING
|
16
|
+
end
|
17
|
+
|
18
|
+
# (see Encoding.to_payload)
|
19
|
+
def to_payload(value)
|
20
|
+
return nil unless value.is_a?(String) && value.encoding == ::Encoding::ASCII_8BIT
|
21
|
+
|
22
|
+
Temporalio::Api::Common::V1::Payload.new(
|
23
|
+
metadata: { 'encoding' => ENCODING },
|
24
|
+
data: value
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
# (see Encoding.from_payload)
|
29
|
+
def from_payload(payload)
|
30
|
+
payload.data
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'google/protobuf'
|
4
|
+
require 'temporalio/api'
|
5
|
+
require 'temporalio/converters/payload_converter/encoding'
|
6
|
+
|
7
|
+
module Temporalio
|
8
|
+
module Converters
|
9
|
+
class PayloadConverter
|
10
|
+
# Encoding for Protobuf values for +binary/protobuf+ encoding.
|
11
|
+
class BinaryProtobuf < Encoding
|
12
|
+
ENCODING = 'binary/protobuf'
|
13
|
+
|
14
|
+
# (see Encoding.encoding)
|
15
|
+
def encoding
|
16
|
+
ENCODING
|
17
|
+
end
|
18
|
+
|
19
|
+
# (see Encoding.to_payload)
|
20
|
+
def to_payload(value)
|
21
|
+
return nil unless value.is_a?(Google::Protobuf::MessageExts)
|
22
|
+
|
23
|
+
# @type var value: Google::Protobuf::MessageExts
|
24
|
+
Api::Common::V1::Payload.new(
|
25
|
+
metadata: { 'encoding' => ENCODING, 'messageType' => value.class.descriptor.name },
|
26
|
+
data: value.to_proto
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
# (see Encoding.from_payload)
|
31
|
+
def from_payload(payload)
|
32
|
+
type = payload.metadata['messageType']
|
33
|
+
# @type var desc: untyped
|
34
|
+
desc = Google::Protobuf::DescriptorPool.generated_pool.lookup(type)
|
35
|
+
raise "No protobuf message found in global pool for message type #{type}" unless desc
|
36
|
+
|
37
|
+
desc.msgclass.decode(payload.data)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'temporalio/api'
|
4
|
+
require 'temporalio/converters/payload_converter'
|
5
|
+
|
6
|
+
module Temporalio
|
7
|
+
module Converters
|
8
|
+
class PayloadConverter
|
9
|
+
# Payload converter that is a collection of {Encoding}s. When converting to a payload, it tries each encoding
|
10
|
+
# converter in order until one works. The encoding converter is expected to set the +encoding+ metadata which is
|
11
|
+
# then used to match to the proper encoding converter when converting back to a Ruby value.
|
12
|
+
class Composite < PayloadConverter
|
13
|
+
class ConverterNotFound < Error; end
|
14
|
+
class EncodingNotSet < Error; end
|
15
|
+
|
16
|
+
# @return [Hash<String, Encoding>] Encoding converters processed in order.
|
17
|
+
attr_reader :converters
|
18
|
+
|
19
|
+
# Create a payload converter with the given encoding converters processed in order.
|
20
|
+
#
|
21
|
+
# @param converters [Array<Encoding>] Encoding converters.
|
22
|
+
def initialize(*converters)
|
23
|
+
super()
|
24
|
+
@converters = converters.each_with_object({}) do |converter, result|
|
25
|
+
result[converter.encoding] = converter
|
26
|
+
result
|
27
|
+
end
|
28
|
+
@converters.freeze
|
29
|
+
end
|
30
|
+
|
31
|
+
# Convert Ruby value to a payload by going over each encoding converter in order until one can convert.
|
32
|
+
#
|
33
|
+
# @param value [Object] Ruby value to convert.
|
34
|
+
# @return [Api::Common::V1::Payload] Converted payload.
|
35
|
+
# @raise [ConverterNotFound] If no converters can process the value.
|
36
|
+
def to_payload(value)
|
37
|
+
converters.each_value do |converter|
|
38
|
+
payload = converter.to_payload(value)
|
39
|
+
return payload unless payload.nil?
|
40
|
+
end
|
41
|
+
raise ConverterNotFound, "Value of type #{value} has no known converter"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Convert payload to Ruby value based on its +encoding+ metadata on the payload.
|
45
|
+
#
|
46
|
+
# @param payload [Api::Common::V1::Payload] Payload to convert.
|
47
|
+
# @return [Object] Converted Ruby value.
|
48
|
+
# @raise [EncodingNotSet] If encoding not set on the metadata.
|
49
|
+
# @raise [ConverterNotFound] If no converter found for the encoding.
|
50
|
+
def from_payload(payload)
|
51
|
+
encoding = payload.metadata['encoding']
|
52
|
+
raise EncodingNotSet, 'Missing payload encoding' unless encoding
|
53
|
+
|
54
|
+
converter = converters[encoding]
|
55
|
+
raise ConverterNotFound, "No converter for encoding #{encoding}" unless converter
|
56
|
+
|
57
|
+
converter.from_payload(payload)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Temporalio
|
4
|
+
module Converters
|
5
|
+
class PayloadConverter
|
6
|
+
# Base class for encoding converters that can be used for {Composite} converters. Each converter has an {encoding}
|
7
|
+
# that should be set on the Payload metadata for values it can process. Implementers must implement {encoding}
|
8
|
+
class Encoding
|
9
|
+
# @return [String] Encoding that will be put on the payload metadata if this encoding converter can handle the
|
10
|
+
# value.
|
11
|
+
def encoding
|
12
|
+
raise NotImplementedError
|
13
|
+
end
|
14
|
+
|
15
|
+
# Convert value to payload if this encoding converter can handle it, or return +nil+. If the converter can
|
16
|
+
# handle it, the resulting payload must have +encoding+ metadata on the payload set to the value of {encoding}.
|
17
|
+
#
|
18
|
+
# @param value [Object] Ruby value to possibly convert.
|
19
|
+
# @return [Api::Common::V1::Payload, nil] Converted payload if it can handle it, +nil+ otherwise.
|
20
|
+
def to_payload(value)
|
21
|
+
raise NotImplementedError
|
22
|
+
end
|
23
|
+
|
24
|
+
# Convert the payload to a Ruby value. The caller confirms the +encoding+ metadata matches {encoding}, so this
|
25
|
+
# will error if it cannot convert.
|
26
|
+
#
|
27
|
+
# @param payload [Api::Common::V1::Payload] Payload to convert.
|
28
|
+
# @return [Object] Converted Ruby value.
|
29
|
+
def from_payload(payload)
|
30
|
+
raise NotImplementedError
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'temporalio/api'
|
5
|
+
require 'temporalio/converters/payload_converter/encoding'
|
6
|
+
|
7
|
+
module Temporalio
|
8
|
+
module Converters
|
9
|
+
class PayloadConverter
|
10
|
+
# Encoding for all values for +json/plain+ encoding.
|
11
|
+
class JSONPlain < Encoding
|
12
|
+
ENCODING = 'json/plain'
|
13
|
+
|
14
|
+
# Create JSONPlain converter.
|
15
|
+
#
|
16
|
+
# @param parse_options [Hash] Options for {::JSON.parse}.
|
17
|
+
# @param generate_options [Hash] Options for {::JSON.generate}.
|
18
|
+
def initialize(parse_options: { create_additions: true }, generate_options: {})
|
19
|
+
super()
|
20
|
+
@parse_options = parse_options
|
21
|
+
@generate_options = generate_options
|
22
|
+
end
|
23
|
+
|
24
|
+
# (see Encoding.encoding)
|
25
|
+
def encoding
|
26
|
+
ENCODING
|
27
|
+
end
|
28
|
+
|
29
|
+
# (see Encoding.to_payload)
|
30
|
+
def to_payload(value)
|
31
|
+
Api::Common::V1::Payload.new(
|
32
|
+
metadata: { 'encoding' => ENCODING },
|
33
|
+
data: JSON.generate(value, @generate_options).b
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
# (see Encoding.from_payload)
|
38
|
+
def from_payload(payload)
|
39
|
+
JSON.parse(payload.data, @parse_options)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'google/protobuf'
|
4
|
+
require 'temporalio/api'
|
5
|
+
require 'temporalio/converters/payload_converter/encoding'
|
6
|
+
|
7
|
+
module Temporalio
|
8
|
+
module Converters
|
9
|
+
class PayloadConverter
|
10
|
+
# Encoding for Protobuf values for +json/protobuf+ encoding.
|
11
|
+
class JSONProtobuf < Encoding
|
12
|
+
ENCODING = 'json/protobuf'
|
13
|
+
|
14
|
+
# (see Encoding.encoding)
|
15
|
+
def encoding
|
16
|
+
ENCODING
|
17
|
+
end
|
18
|
+
|
19
|
+
# (see Encoding.to_payload)
|
20
|
+
def to_payload(value)
|
21
|
+
return nil unless value.is_a?(Google::Protobuf::MessageExts)
|
22
|
+
|
23
|
+
Api::Common::V1::Payload.new(
|
24
|
+
metadata: { 'encoding' => ENCODING, 'messageType' => value.class.descriptor.name },
|
25
|
+
data: value.to_json
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
# (see Encoding.from_payload)
|
30
|
+
def from_payload(payload)
|
31
|
+
type = payload.metadata['messageType']
|
32
|
+
# @type var desc: untyped
|
33
|
+
desc = Google::Protobuf::DescriptorPool.generated_pool.lookup(type)
|
34
|
+
raise "No protobuf message found in global pool for message type #{type}" unless desc
|
35
|
+
|
36
|
+
desc.msgclass.decode_json(payload.data)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'temporalio/converters/payload_converter/binary_null'
|
4
|
+
require 'temporalio/converters/payload_converter/binary_plain'
|
5
|
+
require 'temporalio/converters/payload_converter/binary_protobuf'
|
6
|
+
require 'temporalio/converters/payload_converter/composite'
|
7
|
+
require 'temporalio/converters/payload_converter/json_plain'
|
8
|
+
require 'temporalio/converters/payload_converter/json_protobuf'
|
9
|
+
|
10
|
+
module Temporalio
|
11
|
+
module Converters
|
12
|
+
# Base class for converting Ruby values to/from Temporal payloads.
|
13
|
+
class PayloadConverter
|
14
|
+
# @return [PayloadConverter::Composite] Default payload converter.
|
15
|
+
def self.default
|
16
|
+
@default ||= new_with_defaults
|
17
|
+
end
|
18
|
+
|
19
|
+
# Create a new payload converter with the default set of encoding converters.
|
20
|
+
#
|
21
|
+
# @param json_parse_options [Hash] Options for {::JSON.parse}.
|
22
|
+
# @param json_generate_options [Hash] Options for {::JSON.generate}.
|
23
|
+
# @return [PayloadConverter::Composite] Created payload converter.
|
24
|
+
def self.new_with_defaults(json_parse_options: { create_additions: true }, json_generate_options: {})
|
25
|
+
Ractor.make_shareable(
|
26
|
+
PayloadConverter::Composite.new(
|
27
|
+
PayloadConverter::BinaryNull.new,
|
28
|
+
PayloadConverter::BinaryPlain.new,
|
29
|
+
PayloadConverter::JSONProtobuf.new,
|
30
|
+
PayloadConverter::BinaryProtobuf.new,
|
31
|
+
PayloadConverter::JSONPlain.new(parse_options: json_parse_options, generate_options: json_generate_options)
|
32
|
+
)
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convert a Ruby value to a payload.
|
37
|
+
#
|
38
|
+
# @param value [Object] Ruby value.
|
39
|
+
# @return [Api::Common::V1::Payload] Converted payload.
|
40
|
+
def to_payload(value)
|
41
|
+
raise NotImplementedError
|
42
|
+
end
|
43
|
+
|
44
|
+
# Convert multiple Ruby values to a payload set.
|
45
|
+
#
|
46
|
+
# @param values [Object] Ruby values, converted to array via {::Array}.
|
47
|
+
# @return [Api::Common::V1::Payloads] Converted payload set.
|
48
|
+
def to_payloads(values)
|
49
|
+
Api::Common::V1::Payloads.new(
|
50
|
+
payloads: Array(values).map { |value| to_payload(value) }
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Convert a payload to a Ruby value.
|
55
|
+
#
|
56
|
+
# @param payload [Api::Common::V1::Payload] Payload.
|
57
|
+
# @return [Object] Converted Ruby value.
|
58
|
+
def from_payload(payload)
|
59
|
+
raise NotImplementedError
|
60
|
+
end
|
61
|
+
|
62
|
+
# Convert a payload set to Ruby values.
|
63
|
+
#
|
64
|
+
# @param payloads [Api::Common::V1::Payloads, nil] Payload set.
|
65
|
+
# @return [Array<Object>] Converted Ruby values.
|
66
|
+
def from_payloads(payloads)
|
67
|
+
return [] unless payloads
|
68
|
+
|
69
|
+
payloads.payloads.map { |payload| from_payload(payload) }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|