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

Inherits:
NamedType show all
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

Instance Attribute Summary

Attributes inherited from Type

#avro_type_name

Instance Method Summary collapse

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 ConfigurableType

#configure_options

Methods included from Namespaceable

#fullname

Methods included from ComplexType

included, #namespace

Methods inherited from Type

#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.



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

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)


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

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

#extends(name) ⇒ Object

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



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

def extends(name)
  fields.merge!(cache.lookup_named_type(name, namespace).duplicated_fields)
end

#optional(name, avro_type_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.



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

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

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

Add a required field to the record



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

def required(name, avro_type_name, options = {}, &block)
  new_field = Avro::Builder::Field.new(name: name,
                                       avro_type_name: avro_type_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



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

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