Class: Avro::Schema::RecordSchema

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

Constant Summary

Constants inherited from Avro::Schema

CRC_EMPTY, DECIMAL_LOGICAL_TYPE, DEFAULT_VALIDATE_OPTIONS, INT_MAX_VALUE, INT_MIN_VALUE, LONG_MAX_VALUE, LONG_MIN_VALUE, NAMED_TYPES, NAMED_TYPES_SYM, NAME_REGEX, PRIMITIVE_TYPES, PRIMITIVE_TYPES_SYM, SINGLE_OBJECT_MAGIC_NUMBER, VALID_TYPES, VALID_TYPES_SYM

Instance Attribute Summary collapse

Attributes inherited from NamedSchema

#aliases, #name, #namespace

Attributes inherited from Avro::Schema

#logical_type, #type_sym

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NamedSchema

#fullname, #fullname_aliases, #match_fullname?, #match_schema?

Methods inherited from Avro::Schema

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

Constructor Details

#initialize(name, namespace, fields, names = nil, schema_type = :record, doc = nil, aliases = nil) ⇒ RecordSchema

Returns a new instance of RecordSchema.



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/avro/schema.rb', line 330

def initialize(name, namespace, fields, names=nil, schema_type=:record, doc=nil, aliases=nil)
  if schema_type == :request || schema_type == 'request'
    @type_sym = schema_type.to_sym
    @namespace = namespace
    @name = nil
    @doc = nil
  else
    super(schema_type, name, namespace, names, doc, nil, aliases)
  end
  @fields = if fields
              RecordSchema.make_field_objects(fields, names, self.namespace)
            else
              {}
            end
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



299
300
301
# File 'lib/avro/schema.rb', line 299

def doc
  @doc
end

#fieldsObject (readonly)

Returns the value of attribute fields.



299
300
301
# File 'lib/avro/schema.rb', line 299

def fields
  @fields
end

Class Method Details

.make_field_objects(field_data, names, namespace = nil) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/avro/schema.rb', line 301

def self.make_field_objects(field_data, names, namespace=nil)
  field_objects, field_names, alias_names = [], Set.new, Set.new
  field_data.each do |field|
    if field.respond_to?(:[]) # TODO(jmhodges) wtffffff
      type = field['type']
      name = field['name']
      default = field.key?('default') ? field['default'] : :no_default
      order = field['order']
      doc = field['doc']
      aliases = field['aliases']
      new_field = Field.new(type, name, default, order, names, namespace, doc, aliases)
      # make sure field name has not been used yet
      if field_names.include?(new_field.name)
        raise SchemaParseError, "Field name #{new_field.name.inspect} is already in use"
      end
      field_names << new_field.name
      # make sure alias has not be been used yet
      if new_field.aliases && alias_names.intersect?(new_field.aliases.to_set)
        raise SchemaParseError, "Alias #{(alias_names & new_field.aliases).to_a} already in use"
      end
      alias_names.merge(new_field.aliases) if new_field.aliases
    else
      raise SchemaParseError, "Not a valid field: #{field}"
    end
    field_objects << new_field
  end
  field_objects
end

Instance Method Details

#fields_by_aliasObject



350
351
352
353
354
355
356
357
358
# File 'lib/avro/schema.rb', line 350

def fields_by_alias
  @fields_by_alias ||= fields.each_with_object({}) do |field, hash|
    if field.aliases
      field.aliases.each do |a|
        hash[a] = field
      end
    end
  end
end

#fields_hashObject



346
347
348
# File 'lib/avro/schema.rb', line 346

def fields_hash
  @fields_hash ||= fields.inject({}){|hsh, field| hsh[field.name] = field; hsh }
end

#to_avro(names = Set.new) ⇒ Object



360
361
362
363
364
365
366
367
368
369
# File 'lib/avro/schema.rb', line 360

def to_avro(names=Set.new)
  hsh = super
  return hsh unless hsh.is_a?(Hash)
  hsh['fields'] = @fields.map {|f| f.to_avro(names) }
  if type_sym == :request
    hsh['fields']
  else
    hsh
  end
end