Class: CSV2Avro::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/csv2avro/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_schemaObject (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_stringObject (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

#aliasesObject

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

#defaultsObject



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

#typesObject



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