Class: Avro::Schema::RecordSchema

Inherits:
NamedSchema 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 NamedSchema

#name, #namespace

Attributes inherited from Avro::Schema

#logical_type, #type_sym

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NamedSchema

#fullname

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(name, namespace, fields, names = nil, schema_type = :record, doc = nil) ⇒ RecordSchema

Returns a new instance of RecordSchema.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/avro/schema.rb', line 230

def initialize(name, namespace, fields, names=nil, schema_type=:record, doc=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)
  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.



205
206
207
# File 'lib/avro/schema.rb', line 205

def doc
  @doc
end

#fieldsObject (readonly)

Returns the value of attribute fields.



205
206
207
# File 'lib/avro/schema.rb', line 205

def fields
  @fields
end

Class Method Details

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/avro/schema.rb', line 207

def self.make_field_objects(field_data, names, namespace=nil)
  field_objects, field_names = [], Set.new
  field_data.each_with_index do |field, i|
    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']
      new_field = Field.new(type, name, default, order, names, namespace, doc)
      # 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
    else
      raise SchemaParseError, "Not a valid field: #{field}"
    end
    field_objects << new_field
  end
  field_objects
end

Instance Method Details

#fields_hashObject



246
247
248
# File 'lib/avro/schema.rb', line 246

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

#to_avro(names = Set.new) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/avro/schema.rb', line 250

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