Class: Avro::IO::DatumWriter

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

Overview

DatumWriter for generic ruby objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writers_schema = nil) ⇒ DatumWriter

Returns a new instance of DatumWriter.



535
536
537
# File 'lib/avro/io.rb', line 535

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

Instance Attribute Details

#writers_schemaObject

Returns the value of attribute writers_schema.



534
535
536
# File 'lib/avro/io.rb', line 534

def writers_schema
  @writers_schema
end

Instance Method Details

#write(datum, encoder) ⇒ Object



539
540
541
# File 'lib/avro/io.rb', line 539

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

#write_array(writers_schema, datum, encoder) ⇒ Object



580
581
582
583
584
585
586
587
588
# File 'lib/avro/io.rb', line 580

def write_array(writers_schema, datum, encoder)
  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



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/avro/io.rb', line 543

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

  unless Schema.validate(writers_schema, datum, encoded = true)
    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



575
576
577
578
# File 'lib/avro/io.rb', line 575

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



571
572
573
# File 'lib/avro/io.rb', line 571

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

#write_map(writers_schema, datum, encoder) ⇒ Object



590
591
592
593
594
595
596
597
598
599
# File 'lib/avro/io.rb', line 590

def write_map(writers_schema, datum, encoder)
  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



612
613
614
615
616
# File 'lib/avro/io.rb', line 612

def write_record(writers_schema, datum, encoder)
  writers_schema.fields.each do |field|
    write_data(field.type, datum[field.name], encoder)
  end
end

#write_union(writers_schema, datum, encoder) ⇒ Object



601
602
603
604
605
606
607
608
609
610
# File 'lib/avro/io.rb', line 601

def write_union(writers_schema, datum, encoder)
  index_of_schema = -1
  found = writers_schema.schemas.
    find{|e| index_of_schema += 1; found = Schema.validate(e, datum) }
  unless found  # Because find_index doesn't exist in 1.8.6
    raise AvroTypeError.new(writers_schema, datum)
  end
  encoder.write_long(index_of_schema)
  write_data(writers_schema.schemas[index_of_schema], datum, encoder)
end