Class: Temporalio::Converters::DataConverter
- Inherits:
-
Object
- Object
- Temporalio::Converters::DataConverter
- Defined in:
- lib/temporalio/converters/data_converter.rb
Overview
Data converter for converting/encoding payloads to/from Ruby values.
Instance Attribute Summary collapse
-
#failure_converter ⇒ FailureConverter
readonly
Failure converter.
-
#payload_codec ⇒ PayloadCodec?
readonly
Optional codec for encoding/decoding payload bytes such as for encryption.
-
#payload_converter ⇒ PayloadConverter
readonly
Payload converter.
Class Method Summary collapse
-
.default ⇒ DataConverter
Default data converter.
Instance Method Summary collapse
-
#from_failure(failure) ⇒ Exception
Decode and convert a Temporal failure to a Ruby error.
-
#from_payload(payload) ⇒ Object
Decode and convert a payload to a Ruby value.
-
#from_payloads(payloads) ⇒ Array<Object>
Decode and convert a payload set to Ruby values.
-
#initialize(payload_converter: PayloadConverter.default, failure_converter: FailureConverter.default, payload_codec: nil) ⇒ DataConverter
constructor
Create data converter.
-
#to_failure(error) ⇒ Api::Failure::V1::Failure
Convert a Ruby error to a Temporal failure and encode it.
-
#to_payload(value) ⇒ Api::Common::V1::Payload
Convert a Ruby value to a payload and encode it.
-
#to_payloads(values) ⇒ Api::Common::V1::Payloads
Convert multiple Ruby values to a payload set and encode it.
Constructor Details
#initialize(payload_converter: PayloadConverter.default, failure_converter: FailureConverter.default, payload_codec: nil) ⇒ DataConverter
Create data converter.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/temporalio/converters/data_converter.rb', line 30 def initialize( payload_converter: PayloadConverter.default, failure_converter: FailureConverter.default, payload_codec: nil ) raise 'Payload converter not shareable' unless Ractor.shareable?(payload_converter) raise 'Failure converter not shareable' unless Ractor.shareable?(failure_converter) @payload_converter = payload_converter @failure_converter = failure_converter @payload_codec = payload_codec end |
Instance Attribute Details
#failure_converter ⇒ FailureConverter (readonly)
Returns Failure converter. This must be Ractor shareable.
15 16 17 |
# File 'lib/temporalio/converters/data_converter.rb', line 15 def failure_converter @failure_converter end |
#payload_codec ⇒ PayloadCodec? (readonly)
Returns Optional codec for encoding/decoding payload bytes such as for encryption.
18 19 20 |
# File 'lib/temporalio/converters/data_converter.rb', line 18 def payload_codec @payload_codec end |
#payload_converter ⇒ PayloadConverter (readonly)
Returns Payload converter. This must be Ractor shareable.
12 13 14 |
# File 'lib/temporalio/converters/data_converter.rb', line 12 def payload_converter @payload_converter end |
Class Method Details
.default ⇒ DataConverter
Returns Default data converter.
21 22 23 |
# File 'lib/temporalio/converters/data_converter.rb', line 21 def self.default @default ||= DataConverter.new end |
Instance Method Details
#from_failure(failure) ⇒ Exception
Decode and convert a Temporal failure to a Ruby error.
97 98 99 |
# File 'lib/temporalio/converters/data_converter.rb', line 97 def from_failure(failure) failure_converter.from_failure(failure, self) end |
#from_payload(payload) ⇒ Object
Decode and convert a payload to a Ruby value.
67 68 69 70 |
# File 'lib/temporalio/converters/data_converter.rb', line 67 def from_payload(payload) payload = payload_codec.decode([payload]).first if payload_codec payload_converter.from_payload(payload) end |
#from_payloads(payloads) ⇒ Array<Object>
Decode and convert a payload set to Ruby values.
76 77 78 79 80 81 82 83 |
# File 'lib/temporalio/converters/data_converter.rb', line 76 def from_payloads(payloads) return [] unless payloads && !payloads.payloads.empty? if payload_codec && !payloads.payloads.empty? payloads = Api::Common::V1::Payloads.new(payloads: payload_codec.decode(payloads.payloads)) end payload_converter.from_payloads(payloads) end |
#to_failure(error) ⇒ Api::Failure::V1::Failure
Convert a Ruby error to a Temporal failure and encode it.
89 90 91 |
# File 'lib/temporalio/converters/data_converter.rb', line 89 def to_failure(error) failure_converter.to_failure(error, self) end |
#to_payload(value) ⇒ Api::Common::V1::Payload
Convert a Ruby value to a payload and encode it.
47 48 49 50 51 |
# File 'lib/temporalio/converters/data_converter.rb', line 47 def to_payload(value) payload = payload_converter.to_payload(value) payload = payload_codec.encode([payload]).first if payload_codec payload end |
#to_payloads(values) ⇒ Api::Common::V1::Payloads
Convert multiple Ruby values to a payload set and encode it.
57 58 59 60 61 |
# File 'lib/temporalio/converters/data_converter.rb', line 57 def to_payloads(values) payloads = payload_converter.to_payloads(values) payloads.payloads.replace(payload_codec.encode(payloads.payloads)) if payload_codec && !payloads.payloads.empty? payloads end |