Sha256: 792f865e6254a8688b0f346cbac07e456f5c6cb43ba8f05579465a37cc84a910

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

module Avro
  module Builder
    module Types
      class UnionType < Type
        include Avro::Builder::Types::ComplexType
        include Avro::Builder::Types::TypeReferencer

        NULL_TYPE = 'null'.freeze

        dsl_attribute :types do |*types|
          if !types.empty?
            @types = types.flatten.map { |type| create_builtin_or_lookup_type(type) }
          else
            @types
          end
        end

        # Unions are serialized as an array of types
        def serialize(referenced_state)
          types.map { |type| type.serialize(referenced_state) }
        end

        # serialized will be an array of types. If the array includes
        # :null then it is moved to the beginning of the array.
        def self.union_with_null(serialized)
          serialized.reject { |type| type.to_s == NULL_TYPE }.unshift(:null)
        end

        def validate!
          validate_required_attribute!(:types)
        end

        def logical_type=(value)
          raise AttributeError.new("Logical types are not supported for unions: #{value}.")
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
avro-builder-0.16.1 lib/avro/builder/types/union_type.rb
avro-builder-0.16.0 lib/avro/builder/types/union_type.rb
avro-builder-0.15.0 lib/avro/builder/types/union_type.rb
avro-builder-0.15.0.rc0 lib/avro/builder/types/union_type.rb
avro-builder-0.14.1 lib/avro/builder/types/union_type.rb
avro-builder-0.14.0 lib/avro/builder/types/union_type.rb
avro-builder-0.13.0 lib/avro/builder/types/union_type.rb
avro-builder-0.12.0 lib/avro/builder/types/union_type.rb