Class: Temporalio::Converters::PayloadConverter::JSONProtobuf

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

Overview

Encoding for Protobuf values for json/protobuf encoding.

Constant Summary collapse

ENCODING =
'json/protobuf'

Instance Method Summary collapse

Instance Method Details

#encodingString

Returns Encoding that will be put on the payload metadata if this encoding converter can handle the value.

Returns:

  • (String)

    Encoding that will be put on the payload metadata if this encoding converter can handle the value.



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

def encoding
  ENCODING
end

#from_payload(payload) ⇒ Object

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

Parameters:

Returns:

  • (Object)

    Converted Ruby value.



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

def from_payload(payload)
  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_json(payload.data)
end

#to_payload(value) ⇒ 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.

Parameters:

  • value (Object)

    Ruby value to possibly convert.

Returns:



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

def to_payload(value)
  return nil unless value.is_a?(Google::Protobuf::MessageExts)

  Api::Common::V1::Payload.new(
    metadata: { 'encoding' => ENCODING, 'messageType' => value.class.descriptor.name },
    data: value.to_json
  )
end