Module: Avromatic::Model::Types::TypeFactory

Extended by:
TypeFactory
Included in:
TypeFactory
Defined in:
lib/avromatic/model/types/type_factory.rb

Constant Summary collapse

SINGLETON_TYPES =
{
  'date' => Avromatic::Model::Types::DateType.new,
  'timestamp-micros' => Avromatic::Model::Types::TimestampMicrosType.new,
  'timestamp-millis' => Avromatic::Model::Types::TimestampMillisType.new,
  'string' => Avromatic::Model::Types::StringType.new,
  'bytes' => Avromatic::Model::Types::StringType.new,
  'boolean' => Avromatic::Model::Types::BooleanType.new,
  'int' => Avromatic::Model::Types::IntegerType.new,
  'long' => Avromatic::Model::Types::BigIntType.new,
  'float' => Avromatic::Model::Types::FloatType.new,
  'double' => Avromatic::Model::Types::FloatType.new,
  'null' => Avromatic::Model::Types::NullType.new
}.deep_freeze

Instance Method Summary collapse

Instance Method Details

#create(schema:, nested_models:, use_custom_types: true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/avromatic/model/types/type_factory.rb', line 41

def create(schema:, nested_models:, use_custom_types: true)
  if use_custom_types && Avromatic.custom_type_registry.registered?(schema)
    custom_type_configuration = Avromatic.custom_type_registry.fetch(schema)
    default_type = create(
      schema: schema,
      nested_models: nested_models,
      use_custom_types: false
    )
    Avromatic::Model::Types::CustomType.new(
      custom_type_configuration: custom_type_configuration,
      default_type: default_type
    )
  elsif schema.respond_to?(:logical_type) && SINGLETON_TYPES.include?(schema.logical_type)
    SINGLETON_TYPES.fetch(schema.logical_type)
  elsif schema.respond_to?(:logical_type) && schema.logical_type == 'decimal'
    Avromatic::Model::Types::DecimalType.new(precision: schema.precision, scale: schema.scale || 0)
  elsif SINGLETON_TYPES.include?(schema.type)
    SINGLETON_TYPES.fetch(schema.type)
  else
    case schema.type_sym
    when :fixed
      Avromatic::Model::Types::FixedType.new(schema.size)
    when :enum
      Avromatic::Model::Types::EnumType.new(schema.symbols)
    when :array
      value_type = create(schema: schema.items, nested_models: nested_models,
                          use_custom_types: use_custom_types)
      Avromatic::Model::Types::ArrayType.new(value_type: value_type)
    when :map
      value_type = create(schema: schema.values, nested_models: nested_models,
                          use_custom_types: use_custom_types)
      Avromatic::Model::Types::MapType.new(
        key_type: Avromatic::Model::Types::StringType.new,
        value_type: value_type
      )
    when :union
      null_index = schema.schemas.index { |member_schema| member_schema.type_sym == :null }
      raise 'a null type in a union must be the first member' if null_index && null_index > 0

      member_schemas = schema.schemas.reject { |member_schema| member_schema.type_sym == :null }
      if member_schemas.size == 1
        create(schema: member_schemas.first, nested_models: nested_models)
      else
        member_types = member_schemas.map do |member_schema|
          create(schema: member_schema, nested_models: nested_models, use_custom_types: use_custom_types)
        end
        Avromatic::Model::Types::UnionType.new(member_types: member_types)
      end
    when :record
      record_class = build_nested_model(schema: schema, nested_models: nested_models)
      Avromatic::Model::Types::RecordType.new(record_class: record_class)
    else
      raise ArgumentError.new("Unsupported type #{schema.type_sym}")
    end
  end
end