Class: CSV2Avro::Schema
- Inherits:
-
Object
- Object
- CSV2Avro::Schema
- Defined in:
- lib/csv2avro/schema.rb
Instance Attribute Summary collapse
-
#avro_schema ⇒ Object
readonly
Returns the value of attribute avro_schema.
-
#schema_string ⇒ Object
readonly
Returns the value of attribute schema_string.
Instance Method Summary collapse
-
#aliases ⇒ Object
TODO: Change this when the avro gem starts to support aliases.
- #defaults ⇒ Object
-
#initialize(schema) ⇒ Schema
constructor
A new instance of Schema.
- #types ⇒ Object
Constructor Details
#initialize(schema) ⇒ Schema
Returns a new instance of Schema.
7 8 9 10 |
# File 'lib/csv2avro/schema.rb', line 7 def initialize(schema) @schema_string = schema.read @avro_schema = Avro::Schema.parse(schema_string) end |
Instance Attribute Details
#avro_schema ⇒ Object (readonly)
Returns the value of attribute avro_schema.
5 6 7 |
# File 'lib/csv2avro/schema.rb', line 5 def avro_schema @avro_schema end |
#schema_string ⇒ Object (readonly)
Returns the value of attribute schema_string.
5 6 7 |
# File 'lib/csv2avro/schema.rb', line 5 def schema_string @schema_string end |
Instance Method Details
#aliases ⇒ Object
TODO: Change this when the avro gem starts to support aliases
34 35 36 37 38 39 40 41 42 |
# File 'lib/csv2avro/schema.rb', line 34 def aliases schema_as_json = JSON.parse(schema_string) Hash[ schema_as_json['fields'].select{ |field| field['aliases'] }.flat_map do |field| field['aliases'].map { |one_alias| [one_alias, field['name']]} end ] end |
#defaults ⇒ Object
12 13 14 15 16 |
# File 'lib/csv2avro/schema.rb', line 12 def defaults Hash[ avro_schema.fields.map{ |field| [field.name, field.default] unless field.default.nil? }.compact ] end |
#types ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/csv2avro/schema.rb', line 18 def types Hash[ avro_schema.fields.map do |field| type = if field.type.type_sym == :union # use the primary type field.type.schemas[0].type_sym else field.type.type_sym end [field.name, type] end ] end |