Class: Temporalio::Converters::PayloadConverter::JSONProtobuf
- 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
-
#encoding ⇒ String
Encoding that will be put on the payload metadata if this encoding converter can handle the value.
-
#from_payload(payload, hint: nil) ⇒ Object
Convert the payload to a Ruby value.
-
#to_payload(value, hint: nil) ⇒ Api::Common::V1::Payload?
Convert value to payload if this encoding converter can handle it, or return
nil.
Instance Method Details
#encoding ⇒ String
Returns 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, 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.
30 31 32 33 34 35 36 37 |
# File 'lib/temporalio/converters/payload_converter/json_protobuf.rb', line 30 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_json(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 |
# File 'lib/temporalio/converters/payload_converter/json_protobuf.rb', line 20 def to_payload(value, hint: nil) # rubocop:disable Lint/UnusedMethodArgument 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 |