Class: Synapse::Partitioning::JsonMessagePacker

Inherits:
MessagePacker show all
Defined in:
lib/synapse/partitioning/packing/json_packer.rb

Overview

Implementation of a message packer that serializes the metadata and payload of any message and then serializes the entire message so it can go onto the wire.

Instance Method Summary collapse

Constructor Details

#initialize(serializer) ⇒ undefined



8
9
10
11
12
13
14
15
16
17
# File 'lib/synapse/partitioning/packing/json_packer.rb', line 8

def initialize(serializer)
  @serializer = serializer

  @serialization_target = String
  # Ideally, we want to serialize the metadata and payload to hashes so that we don't
  # duplicate serialization while serializing the message as a whole to a string
  if serializer.can_serialize_to? Hash
    @serialization_target = Hash
  end
end

Instance Method Details

#pack_message(unpacked) ⇒ String



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/synapse/partitioning/packing/json_packer.rb', line 21

def pack_message(unpacked)
  message_type = type_for unpacked

   = @serializer.serialize unpacked., @serialization_target
  payload = @serializer.serialize unpacked.payload, @serialization_target

  packed = {
    message_type: message_type,
    id: unpacked.id,
    metadata: .content,
    payload: payload.content,
    payload_type: payload.type.name,
    payload_revision: payload.type.revision
  }

  if [:event, :domain_event].include? message_type
    pack_event unpacked, packed
  end

  if :domain_event == message_type
    pack_domain_event unpacked, packed
  end

  JSON.dump packed
end