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) ⇒ RecordSchema

Returns a new instance of RecordSchema.



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/avro/schema.rb', line 224

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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



200
201
202
# File 'lib/avro/schema.rb', line 200

def fields
  @fields
end

Class Method Details

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/avro/schema.rb', line 202

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']
      new_field = Field.new(type, name, default, order, names, namespace)
      # 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



238
239
240
# File 'lib/avro/schema.rb', line 238

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

#to_avro(names = Set.new) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/avro/schema.rb', line 242

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