Class: Temporalio::Converters::DataConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/converters/data_converter.rb

Overview

Data converter for converting/encoding payloads to/from Ruby values.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload_converter: PayloadConverter.default, failure_converter: FailureConverter.default, payload_codec: nil) ⇒ DataConverter

Create data converter.

Parameters:

  • payload_converter (PayloadConverter) (defaults to: PayloadConverter.default)

    Payload converter to use. This must be Ractor shareable.

  • failure_converter (FailureConverter) (defaults to: FailureConverter.default)

    Failure converter to use. This must be Ractor shareable.

  • payload_codec (PayloadCodec, nil) (defaults to: nil)

    Payload codec to use.



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_converterFailureConverter (readonly)

Returns Failure converter. This must be Ractor shareable.

Returns:



15
16
17
# File 'lib/temporalio/converters/data_converter.rb', line 15

def failure_converter
  @failure_converter
end

#payload_codecPayloadCodec? (readonly)

Returns Optional codec for encoding/decoding payload bytes such as for encryption.

Returns:

  • (PayloadCodec, nil)

    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_converterPayloadConverter (readonly)

Returns Payload converter. This must be Ractor shareable.

Returns:



12
13
14
# File 'lib/temporalio/converters/data_converter.rb', line 12

def payload_converter
  @payload_converter
end

Class Method Details

.defaultDataConverter

Returns Default data converter.

Returns:



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.

Parameters:

Returns:

  • (Exception)

    Decoded and converted 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.

Parameters:

Returns:

  • (Object)

    Decoded and converted 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.

Parameters:

Returns:

  • (Array<Object>)

    Decoded and converted 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.

Parameters:

  • error (Exception)

    Ruby error.

Returns:



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.

Parameters:

  • value (Object)

    Ruby value.

Returns:



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.

Parameters:

  • values (Object)

    Ruby values, converted to array via Array.

Returns:



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