module Avromatic::Model::RawSerialization::Encode

Constants

UNSPECIFIED

Public Instance Methods

avro_key_datum(validate: UNSPECIFIED) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 75
def avro_key_datum(validate: UNSPECIFIED)
  unless validate == UNSPECIFIED
    ActiveSupport::Deprecation.warn("The 'validate' argument to #{__method__} is deprecated.")
  end

  avro_hash(key_avro_field_references, strict: true)
end
avro_raw_key(validate: UNSPECIFIED) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 33
def avro_raw_key(validate: UNSPECIFIED)
  unless validate == UNSPECIFIED
    ActiveSupport::Deprecation.warn("The 'validate' argument to #{__method__} is deprecated.")
  end

  raise 'Model has no key schema' unless key_avro_schema

  avro_raw_encode(key_attributes_for_avro, :key)
end
avro_raw_value(validate: UNSPECIFIED) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 21
def avro_raw_value(validate: UNSPECIFIED)
  unless validate == UNSPECIFIED
    ActiveSupport::Deprecation.warn("The 'validate' argument to #{__method__} is deprecated.")
  end

  if self.class.recursively_immutable?
    @avro_raw_value ||= avro_raw_encode(value_attributes_for_avro, :value)
  else
    avro_raw_encode(value_attributes_for_avro, :value)
  end
end
avro_value_datum(validate: UNSPECIFIED) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 63
def avro_value_datum(validate: UNSPECIFIED)
  unless validate == UNSPECIFIED
    ActiveSupport::Deprecation.warn("The 'validate' argument to #{__method__} is deprecated.")
  end

  if self.class.recursively_immutable?
    @avro_value_datum ||= avro_hash(value_avro_field_references, strict: true)
  else
    avro_hash(value_avro_field_references, strict: true)
  end
end
key_attributes_for_avro(validate: UNSPECIFIED) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 55
def key_attributes_for_avro(validate: UNSPECIFIED)
  unless validate == UNSPECIFIED
    ActiveSupport::Deprecation.warn("The 'validate' argument to #{__method__} is deprecated.")
  end

  avro_hash(key_avro_field_references)
end
value_attributes_for_avro(validate: UNSPECIFIED) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 43
def value_attributes_for_avro(validate: UNSPECIFIED)
  unless validate == UNSPECIFIED
    ActiveSupport::Deprecation.warn("The 'validate' argument to #{__method__} is deprecated.")
  end

  if self.class.recursively_immutable?
    @value_attributes_for_avro ||= avro_hash(value_avro_field_references)
  else
    avro_hash(value_avro_field_references)
  end
end

Private Instance Methods

avro_hash(field_references, strict: false) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 85
def avro_hash(field_references, strict: false)
  field_references.each_with_object(Hash.new) do |field_reference, result|
    attribute_definition = self.class.attribute_definitions[field_reference.name_sym]
    value = _attributes[field_reference.name_sym]

    if value.nil? && !attribute_definition.nullable?
      # We're missing a required attribute so perform an explicit validation to generate
      # a more complete error message
      avro_validate!
    elsif _attributes.include?(field_reference.name_sym)
      begin
        result[field_reference.name] = attribute_definition.serialize(value, strict)
      rescue Avromatic::Model::ValidationError
        # Perform an explicit validation to generate a more complete error message
        avro_validate!
        # We should never get here but just in case...
        raise
      end
    end
  end
end
avro_raw_encode(data, key_or_value = :value) click to toggle source
# File lib/avromatic/model/raw_serialization.rb, line 107
def avro_raw_encode(data, key_or_value = :value)
  stream = StringIO.new
  encoder = Avro::IO::BinaryEncoder.new(stream)
  datum_writer[key_or_value].write(data, encoder)
  stream.string
end