Class: Avromatic::Model::Types::UnionType

Inherits:
AbstractType show all
Defined in:
lib/avromatic/model/types/union_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(member_types:) ⇒ UnionType

Returns a new instance of UnionType.



12
13
14
15
16
17
# File 'lib/avromatic/model/types/union_type.rb', line 12

def initialize(member_types:)
  super()
  @member_types = member_types
  @value_classes = member_types.flat_map(&:value_classes)
  @input_classes = member_types.flat_map(&:input_classes).uniq
end

Instance Attribute Details

#input_classesObject (readonly)

Returns the value of attribute input_classes.



10
11
12
# File 'lib/avromatic/model/types/union_type.rb', line 10

def input_classes
  @input_classes
end

#member_typesObject (readonly)

Returns the value of attribute member_types.



10
11
12
# File 'lib/avromatic/model/types/union_type.rb', line 10

def member_types
  @member_types
end

#value_classesObject (readonly)

Returns the value of attribute value_classes.



10
11
12
# File 'lib/avromatic/model/types/union_type.rb', line 10

def value_classes
  @value_classes
end

Instance Method Details

#coerce(input) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/avromatic/model/types/union_type.rb', line 23

def coerce(input)
  return input if coerced?(input)

  result = nil
  if input.is_a?(Avromatic::IO::UnionDatum)
    result = member_types[input.member_index].coerce(input.datum)
  else
    member_types.find do |member_type|
      result = safe_coerce(member_type, input)
    end
  end

  raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}") if result.nil?

  result
end

#coerced?(value) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/avromatic/model/types/union_type.rb', line 40

def coerced?(value)
  return false if value.is_a?(Avromatic::IO::UnionDatum)

  value.nil? || member_types.any? do |member_type|
    member_type.coerced?(value)
  end
end

#coercible?(input) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/avromatic/model/types/union_type.rb', line 48

def coercible?(input)
  return true if value.is_a?(Avromatic::IO::UnionDatum)

  coerced?(input) || member_types.any? do |member_type|
    member_type.coercible?(input)
  end
end

#nameObject



19
20
21
# File 'lib/avromatic/model/types/union_type.rb', line 19

def name
  "union[#{member_types.map(&:name).join(', ')}]"
end

#referenced_model_classesObject



72
73
74
# File 'lib/avromatic/model/types/union_type.rb', line 72

def referenced_model_classes
  member_types.flat_map(&:referenced_model_classes).tap(&:uniq!).freeze
end

#serialize(value, strict) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/avromatic/model/types/union_type.rb', line 56

def serialize(value, strict)
  # Avromatic does not treat the null of an optional field as part of the union
  return nil if value.nil?

  member_index = find_index(value)
  if member_index.nil?
    raise ArgumentError.new("Expected #{value.inspect} to be one of #{value_classes.map(&:name)}")
  end

  serialized_value = member_types[member_index].serialize(value, strict)
  if !strict && Avromatic.use_custom_datum_writer
    serialized_value = Avromatic::IO::UnionDatum.new(member_index, serialized_value)
  end
  serialized_value
end