Class: Avro::Schema::UnionSchema

Inherits:
Avro::Schema show all
Defined in:
lib/avro/schema.rb

Constant Summary

Constants inherited from Avro::Schema

INT_MAX_VALUE, INT_MIN_VALUE, LONG_MAX_VALUE, LONG_MIN_VALUE, NAMED_TYPES, NAMED_TYPES_SYM, PRIMITIVE_TYPES, PRIMITIVE_TYPES_SYM, VALID_TYPES, VALID_TYPES_SYM

Instance Attribute Summary collapse

Attributes inherited from Avro::Schema

#logical_type, #type_sym

Instance Method Summary collapse

Methods inherited from Avro::Schema

#==, #be_read?, #hash, #md5_fingerprint, #mutual_read?, parse, #read?, real_parse, #sha256_fingerprint, #subparse, #to_s, #type, #type_adapter, validate

Constructor Details

#initialize(schemas, names = nil, default_namespace = nil) ⇒ UnionSchema

Returns a new instance of UnionSchema.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/avro/schema.rb', line 283

def initialize(schemas, names=nil, default_namespace=nil)
  super(:union)

  @schemas = schemas.each_with_object([]) do |schema, schema_objects|
    new_schema = subparse(schema, names, default_namespace)
    ns_type = new_schema.type_sym

    if VALID_TYPES_SYM.include?(ns_type) &&
        !NAMED_TYPES_SYM.include?(ns_type) &&
        schema_objects.any?{|o| o.type_sym == ns_type }
      raise SchemaParseError, "#{ns_type} is already in Union"
    elsif ns_type == :union
      raise SchemaParseError, "Unions cannot contain other unions"
    else
      schema_objects << new_schema
    end
  end
end

Instance Attribute Details

#schemasObject (readonly)

Returns the value of attribute schemas.



281
282
283
# File 'lib/avro/schema.rb', line 281

def schemas
  @schemas
end

Instance Method Details

#to_avro(names = Set.new) ⇒ Object



302
303
304
# File 'lib/avro/schema.rb', line 302

def to_avro(names=Set.new)
  schemas.map {|schema| schema.to_avro(names) }
end