Sha256: 80e2a9318a152a81689c1ada1fbbb4060735000584bb97045d049d8590af2adb

Contents?: true

Size: 1.93 KB

Versions: 22

Compression:

Stored size: 1.93 KB

Contents

require 'avro_turf/messaging'

module Avromatic
  module Model

    # This concern adds support for serialization based on AvroTurf::Messaging.
    # This serialization leverages a schema registry to prefix encoded values
    # with an id for the schema.
    module MessagingSerialization
      extend ActiveSupport::Concern

      delegate :avro_messaging, to: :class
      private :avro_messaging

      module Encode
        def avro_message_value
          avro_messaging.encode(
            value_attributes_for_avro,
            schema_name: value_avro_schema.fullname
          )
        end

        def avro_message_key
          raise 'Model has no key schema' unless key_avro_schema
          avro_messaging.encode(
            key_attributes_for_avro,
            schema_name: key_avro_schema.fullname
          )
        end
      end
      include Encode

      # This module provides methods to decode an Avro-encoded value and
      # an optional Avro-encoded key as a new model instance.
      module Decode

        # If two arguments are specified then the first is interpreted as the
        # message key and the second is the message value. If there is only one
        # arg then it is used as the message value.
        def avro_message_decode(*args)
          message_key, message_value = args.size > 1 ? args : [nil, args.first]
          key_attributes = message_key &&
            avro_messaging.decode(message_key, schema_name: key_avro_schema.fullname)
          value_attributes = avro_messaging
            .decode(message_value, schema_name: avro_schema.fullname)

          new(value_attributes.merge!(key_attributes || {}))
        end
      end

      module ClassMethods
        # The messaging object acts as an intermediary talking to the schema
        # registry and using returned/specified schemas to decode/encode.
        def avro_messaging
          Avromatic.messaging
        end

        include Decode
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
avromatic-0.12.0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.11.2 lib/avromatic/model/messaging_serialization.rb
avromatic-0.11.1 lib/avromatic/model/messaging_serialization.rb
avromatic-0.11.0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.10.0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.10.0.rc1 lib/avromatic/model/messaging_serialization.rb
avromatic-0.10.0.rc0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0.rc7 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0.rc6 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0.rc4 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0.rc3 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0.rc2 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0.rc1 lib/avromatic/model/messaging_serialization.rb
avromatic-0.9.0.rc0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.8.0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.7.1 lib/avromatic/model/messaging_serialization.rb
avromatic-0.7.0 lib/avromatic/model/messaging_serialization.rb
avromatic-0.6.2 lib/avromatic/model/messaging_serialization.rb
avromatic-0.6.1 lib/avromatic/model/messaging_serialization.rb