Class: Temporalio::Converters::PayloadConverter::BinaryProtobuf

Inherits:
Encoding
  • Object
show all
Defined in:
lib/temporalio/converters/payload_converter/binary_protobuf.rb

Overview

Encoding for Protobuf values for binary/protobuf encoding.

Constant Summary collapse

ENCODING =
'binary/protobuf'

Instance Method Summary collapse

Instance Method Details

#encodingString



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

def encoding
  ENCODING
end

#from_payload(payload, hint: nil) ⇒ Object

Convert the payload to a Ruby value. The caller confirms the encoding metadata matches #encoding, so this will error if it cannot convert.



31
32
33
34
35
36
37
38
# File 'lib/temporalio/converters/payload_converter/binary_protobuf.rb', line 31

def from_payload(payload, hint: nil) # rubocop:disable Lint/UnusedMethodArgument
  type = payload.['messageType']
  # @type var desc: untyped
  desc = Google::Protobuf::DescriptorPool.generated_pool.lookup(type)
  raise "No protobuf message found in global pool for message type #{type}" unless desc

  desc.msgclass.decode(payload.data)
end

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

Convert value to payload if this encoding converter can handle it, or return nil. If the converter can handle it, the resulting payload must have encoding metadata on the payload set to the value of #encoding.



20
21
22
23
24
25
26
27
28
# File 'lib/temporalio/converters/payload_converter/binary_protobuf.rb', line 20

def to_payload(value, hint: nil) # rubocop:disable Lint/UnusedMethodArgument
  return nil unless value.is_a?(Google::Protobuf::MessageExts)

  # @type var value: Google::Protobuf::MessageExts
  Api::Common::V1::Payload.new(
    metadata: { 'encoding' => ENCODING, 'messageType' => value.class.descriptor.name },
    data: value.to_proto
  )
end