Class: Temporalio::Converters::PayloadConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/converters/payload_converter.rb,
lib/temporalio/converters/payload_converter/encoding.rb,
lib/temporalio/converters/payload_converter/composite.rb,
lib/temporalio/converters/payload_converter/json_plain.rb,
lib/temporalio/converters/payload_converter/binary_null.rb,
lib/temporalio/converters/payload_converter/binary_plain.rb,
lib/temporalio/converters/payload_converter/json_protobuf.rb,
lib/temporalio/converters/payload_converter/binary_protobuf.rb

Overview

Base class for converting Ruby values to/from Temporal payloads.

Direct Known Subclasses

Composite

Defined Under Namespace

Classes: BinaryNull, BinaryPlain, BinaryProtobuf, Composite, Encoding, JSONPlain, JSONProtobuf

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultPayloadConverter::Composite

Returns Default payload converter.

Returns:



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

def self.default
  @default ||= new_with_defaults
end

.new_with_defaults(json_parse_options: { create_additions: true }, json_generate_options: {}) ⇒ PayloadConverter::Composite

Create a new payload converter with the default set of encoding converters.

Parameters:

  • json_parse_options (Hash) (defaults to: { create_additions: true })

    Options for JSON.parse.

  • json_generate_options (Hash) (defaults to: {})

    Options for JSON.generate.

Returns:



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

def self.new_with_defaults(json_parse_options: { create_additions: true }, json_generate_options: {})
  PayloadConverter::Composite.new(
    PayloadConverter::BinaryNull.new,
    PayloadConverter::BinaryPlain.new,
    PayloadConverter::JSONProtobuf.new,
    PayloadConverter::BinaryProtobuf.new,
    PayloadConverter::JSONPlain.new(parse_options: json_parse_options, generate_options: json_generate_options)
  )
end

Instance Method Details

#from_payload(payload, hint: nil) ⇒ Object

Convert a payload to a Ruby value.

Parameters:

  • payload (Api::Common::V1::Payload)

    Payload.

  • hint (Object, nil) (defaults to: nil)

    Hint, if any, to assist conversion.

Returns:

  • (Object)

    Converted Ruby value.

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/temporalio/converters/payload_converter.rb', line 62

def from_payload(payload, hint: nil)
  raise NotImplementedError
end

#from_payloads(payloads, hints: nil) ⇒ Array<Object>

Convert a payload set to Ruby values.

Parameters:

  • payloads (Api::Common::V1::Payloads, nil)

    Payload set.

  • hints (Array<Object>, nil) (defaults to: nil)

    Hints, if any, to assist conversion. Note, when using the default converter that converts a value at a time, hints for each payload are taken from the array at that payload’s index. So if there are fewer hints than payloads, some payloads will not have a hint. Similarly if there are more hints than payloads, the trailing hints are not used.

Returns:

  • (Array<Object>)

    Converted Ruby values.



74
75
76
77
78
# File 'lib/temporalio/converters/payload_converter.rb', line 74

def from_payloads(payloads, hints: nil)
  return [] unless payloads

  payloads.payloads.zip(Array(hints)).map { |payload, hint| from_payload(payload, hint:) }
end

#to_payload(value, hint: nil) ⇒ Api::Common::V1::Payload

Convert a Ruby value to a payload.

Parameters:

  • value (Object)

    Ruby value.

  • hint (Object, nil) (defaults to: nil)

    Hint, if any, to assist conversion.

Returns:

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/temporalio/converters/payload_converter.rb', line 39

def to_payload(value, hint: nil)
  raise NotImplementedError
end

#to_payloads(values, hints: nil) ⇒ Api::Common::V1::Payloads

Convert multiple Ruby values to a payload set.

Parameters:

  • values (Object)

    Ruby values, converted to array via Array.

  • hints (Array<Object>, nil) (defaults to: nil)

    Hints, if any, to assist conversion. Note, when using the default converter that converts a payload at a time, hints for each value are taken from the array at that value’s index. So if there are fewer hints than values, some values will not have a hint. Similarly if there are more hints than values, the trailing hints are not used.

Returns:



51
52
53
54
55
# File 'lib/temporalio/converters/payload_converter.rb', line 51

def to_payloads(values, hints: nil)
  Api::Common::V1::Payloads.new(
    payloads: Array(values).zip(Array(hints)).map { |value, hint| to_payload(value, hint:) }
  )
end