Class: Temporalio::Converters::PayloadConverter::Composite

Inherits:
Temporalio::Converters::PayloadConverter show all
Defined in:
lib/temporalio/converters/payload_converter/composite.rb

Overview

Payload converter that is a collection of Encodings. When converting to a payload, it tries each encoding converter in order until one works. The encoding converter is expected to set the encoding metadata which is then used to match to the proper encoding converter when converting back to a Ruby value.

Defined Under Namespace

Classes: ConverterNotFound, EncodingNotSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Temporalio::Converters::PayloadConverter

default, #from_payloads, new_with_defaults, #to_payloads

Constructor Details

#initialize(*converters) ⇒ Composite

Create a payload converter with the given encoding converters processed in order.

Parameters:

  • converters (Array<Encoding>)

    Encoding converters.



22
23
24
25
26
27
28
29
# File 'lib/temporalio/converters/payload_converter/composite.rb', line 22

def initialize(*converters)
  super()
  @converters = converters.each_with_object({}) do |converter, result|
    result[converter.encoding] = converter
    result
  end
  @converters.freeze
end

Instance Attribute Details

#convertersHash<String, Encoding> (readonly)

Returns Encoding converters processed in order.

Returns:

  • (Hash<String, Encoding>)

    Encoding converters processed in order.



17
18
19
# File 'lib/temporalio/converters/payload_converter/composite.rb', line 17

def converters
  @converters
end

Instance Method Details

#from_payload(payload) ⇒ Object

Convert payload to Ruby value based on its encoding metadata on the payload.

Parameters:

Returns:

  • (Object)

    Converted Ruby value.

Raises:



50
51
52
53
54
55
56
57
58
# File 'lib/temporalio/converters/payload_converter/composite.rb', line 50

def from_payload(payload)
  encoding = payload.['encoding']
  raise EncodingNotSet, 'Missing payload encoding' unless encoding

  converter = converters[encoding]
  raise ConverterNotFound, "No converter for encoding #{encoding}" unless converter

  converter.from_payload(payload)
end

#to_payload(value) ⇒ Api::Common::V1::Payload

Convert Ruby value to a payload by going over each encoding converter in order until one can convert.

Parameters:

  • value (Object)

    Ruby value to convert.

Returns:

Raises:



36
37
38
39
40
41
42
# File 'lib/temporalio/converters/payload_converter/composite.rb', line 36

def to_payload(value)
  converters.each_value do |converter|
    payload = converter.to_payload(value)
    return payload unless payload.nil?
  end
  raise ConverterNotFound, "Value of type #{value} has no known converter"
end