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.

[View source]

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

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.


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

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:

[View source]

54
55
56
57
58
59
60
61
62
# File 'lib/temporalio/converters/payload_converter/composite.rb', line 54

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:

[View source]

37
38
39
40
41
42
43
44
45
46
# File 'lib/temporalio/converters/payload_converter/composite.rb', line 37

def to_payload(value)
  # As a special case, raw values just return the payload within
  return value.payload if value.is_a?(RawValue)

  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