Class: Avro::Builder::Types::RecordType

Inherits:
NamedType show all
Includes:
AnonymousTypes
Defined in:
lib/avro/builder/types/record_type.rb

Overview

This class represents a record in an Avro schema. Records may be defined at the top-level or as the type for a field in a record.

Direct Known Subclasses

Record

Constant Summary collapse

DSL_METHODS =
%i(required optional extends).to_set.freeze

Constants included from Avro::Builder::TypeFactory

Avro::Builder::TypeFactory::BUILTIN_TYPES, Avro::Builder::TypeFactory::COMPLEX_TYPES, Avro::Builder::TypeFactory::NAMED_TYPES

Instance Attribute Summary

Attributes inherited from Type

#avro_type_name

Instance Method Summary collapse

Methods included from AnonymousTypes

#array, #map, #type, #union

Methods inherited from NamedType

#cache!, #name, #name_fragment, #namespace, #validate!

Methods included from NamedErrorHandling

#type_name, #type_namespace

Methods included from Aliasable

included

Methods included from Namespaceable

#fullname

Methods included from ComplexType

included, #namespace

Methods inherited from Type

#abstract?, #cache!, #configure_options, #dsl_respond_to?, #namespace, union_with_null, #validate!

Methods included from DslAttributes

#dsl_attribute?, included

Methods included from DslOptions

#dsl_option?, included

Constructor Details

#initialize(name = nil, options: {}, cache:, field: nil, &block) ⇒ RecordType

Returns a new instance of RecordType.



14
15
16
17
18
19
20
21
22
# File 'lib/avro/builder/types/record_type.rb', line 14

def initialize(name = nil, options: {}, cache:, field: nil, &block)
  @avro_type_name = :record
  @name = name
  @cache = cache
  @field = field

  configure_options(options)
  instance_eval(&block) if block_given?
end

Instance Method Details

#dsl_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/avro/builder/types/record_type.rb', line 24

def dsl_method?(name)
  DSL_METHODS.include?(name)
end

#extends(name, options = {}) ⇒ Object

Adds fields from the record with the specified name to the current record.



56
57
58
# File 'lib/avro/builder/types/record_type.rb', line 56

def extends(name, options = {})
  fields.merge!(cache.lookup_named_type(name, options.delete(:namespace) || namespace).duplicated_fields)
end

#optional(name, avro_type_or_name, options = {}, &block) ⇒ Object

Add an optional field to the record. In Avro this is represented as a union of null and the type specified here.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/avro/builder/types/record_type.rb', line 42

def optional(name, avro_type_or_name, options = {}, &block)
  new_field = Avro::Builder::Field.new(name: name,
                                       avro_type_or_name: avro_type_or_name,
                                       record: self,
                                       cache: cache,
                                       internal: { type_namespace: namespace,
                                                   optional_field: true },
                                       options: options,
                                       &block)
  add_field(new_field)
end

#required(name, avro_type_or_name, options = {}, &block) ⇒ Object

Add a required field to the record



29
30
31
32
33
34
35
36
37
38
# File 'lib/avro/builder/types/record_type.rb', line 29

def required(name, avro_type_or_name, options = {}, &block)
  new_field = Avro::Builder::Field.new(name: name,
                                       avro_type_or_name: avro_type_or_name,
                                       record: self,
                                       cache: cache,
                                       internal: { type_namespace: namespace },
                                       options: options,
                                       &block)
  add_field(new_field)
end

#to_h(reference_state = SchemaSerializerReferenceState.new) ⇒ Object Also known as: serialize



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/avro/builder/types/record_type.rb', line 60

def to_h(reference_state = SchemaSerializerReferenceState.new)
  reference_state.definition_or_reference(fullname) do
    {
      type: :record,
      name: name,
      namespace: namespace,
      doc: doc,
      aliases: aliases,
      logicalType: logical_type,
      fields: fields.values.map { |field| field.serialize(reference_state) }
    }.reject { |_, v| v.nil? }
  end
end