Class: Avro::IO::DatumWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/avro/io.rb

Overview

DatumWriter for generic ruby objects

Constant Summary collapse

VALIDATION_OPTIONS =
{ recursive: false, encoded: true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writers_schema = nil) ⇒ DatumWriter

Returns a new instance of DatumWriter.



517
518
519
# File 'lib/avro/io.rb', line 517

def initialize(writers_schema=nil)
  @writers_schema = writers_schema
end

Instance Attribute Details

#writers_schemaObject

Returns the value of attribute writers_schema.



516
517
518
# File 'lib/avro/io.rb', line 516

def writers_schema
  @writers_schema
end

Instance Method Details

#write(datum, encoder) ⇒ Object



521
522
523
# File 'lib/avro/io.rb', line 521

def write(datum, encoder)
  write_data(writers_schema, datum, encoder)
end

#write_array(writers_schema, datum, encoder) ⇒ Object

Raises:



562
563
564
565
566
567
568
569
570
571
# File 'lib/avro/io.rb', line 562

def write_array(writers_schema, datum, encoder)
  raise AvroTypeError.new(writers_schema, datum) unless datum.is_a?(Array)
  if datum.size > 0
    encoder.write_long(datum.size)
    datum.each do |item|
      write_data(writers_schema.items, item, encoder)
    end
  end
  encoder.write_long(0)
end

#write_data(writers_schema, logical_datum, encoder) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/avro/io.rb', line 525

def write_data(writers_schema, logical_datum, encoder)
  datum = writers_schema.type_adapter.encode(logical_datum)

  unless Schema.validate(writers_schema, datum, VALIDATION_OPTIONS)
    raise AvroTypeError.new(writers_schema, datum)
  end

  # function dispatch to write datum
  case writers_schema.type_sym
  when :null;    encoder.write_null(datum)
  when :boolean; encoder.write_boolean(datum)
  when :string;  encoder.write_string(datum)
  when :int;     encoder.write_int(datum)
  when :long;    encoder.write_long(datum)
  when :float;   encoder.write_float(datum)
  when :double;  encoder.write_double(datum)
  when :bytes;   encoder.write_bytes(datum)
  when :fixed;   write_fixed(writers_schema, datum, encoder)
  when :enum;    write_enum(writers_schema, datum, encoder)
  when :array;   write_array(writers_schema, datum, encoder)
  when :map;     write_map(writers_schema, datum, encoder)
  when :union;   write_union(writers_schema, datum, encoder)
  when :record, :error, :request;  write_record(writers_schema, datum, encoder)
  else
    raise AvroError.new("Unknown type: #{writers_schema.type}")
  end
end

#write_enum(writers_schema, datum, encoder) ⇒ Object



557
558
559
560
# File 'lib/avro/io.rb', line 557

def write_enum(writers_schema, datum, encoder)
  index_of_datum = writers_schema.symbols.index(datum)
  encoder.write_int(index_of_datum)
end

#write_fixed(_writers_schema, datum, encoder) ⇒ Object



553
554
555
# File 'lib/avro/io.rb', line 553

def write_fixed(_writers_schema, datum, encoder)
  encoder.write(datum)
end

#write_map(writers_schema, datum, encoder) ⇒ Object

Raises:



573
574
575
576
577
578
579
580
581
582
583
# File 'lib/avro/io.rb', line 573

def write_map(writers_schema, datum, encoder)
  raise AvroTypeError.new(writers_schema, datum) unless datum.is_a?(Hash)
  if datum.size > 0
    encoder.write_long(datum.size)
    datum.each do |k,v|
      encoder.write_string(k)
      write_data(writers_schema.values, v, encoder)
    end
  end
  encoder.write_long(0)
end

#write_record(writers_schema, datum, encoder) ⇒ Object

Raises:



599
600
601
602
603
604
# File 'lib/avro/io.rb', line 599

def write_record(writers_schema, datum, encoder)
  raise AvroTypeError.new(writers_schema, datum) unless datum.is_a?(Hash)
  writers_schema.fields.each do |field|
    write_data(field.type, datum.key?(field.name) ? datum[field.name] : datum[field.name.to_sym], encoder)
  end
end

#write_union(writers_schema, datum, encoder) ⇒ Object



585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/avro/io.rb', line 585

def write_union(writers_schema, datum, encoder)
  index_of_schema = writers_schema.schemas.find_index do |schema|
    # Optimize away expensive validation calls for the common null type
    schema.type_sym == :null ? datum.nil? : Schema.validate(schema, datum)
  end

  unless index_of_schema
    raise AvroTypeError.new(writers_schema, datum)
  end

  encoder.write_long(index_of_schema)
  write_data(writers_schema.schemas[index_of_schema], datum, encoder)
end