class Avro::SchemaNormalization
Public Class Methods
new()
click to toggle source
# File lib/avro/schema_normalization.rb 23 def initialize 24 @processed_names = [] 25 end
to_parsing_form(schema)
click to toggle source
# File lib/avro/schema_normalization.rb 19 def self.to_parsing_form(schema) 20 new.to_parsing_form(schema) 21 end
Public Instance Methods
to_parsing_form(schema)
click to toggle source
# File lib/avro/schema_normalization.rb 27 def to_parsing_form(schema) 28 MultiJson.dump(normalize_schema(schema)) 29 end
Private Instance Methods
normalize_field(field)
click to toggle source
# File lib/avro/schema_normalization.rb 70 def normalize_field(field) 71 { 72 name: field.name, 73 type: normalize_schema(field.type) 74 } 75 end
normalize_named_type(schema, attributes = {})
click to toggle source
# File lib/avro/schema_normalization.rb 77 def normalize_named_type(schema, attributes = {}) 78 name = Name.make_fullname(schema.name, schema.namespace) 79 80 { name: name, type: schema.type_sym.to_s }.merge(attributes) 81 end
normalize_schema(schema)
click to toggle source
# File lib/avro/schema_normalization.rb 33 def normalize_schema(schema) 34 type = schema.type_sym.to_s 35 36 if Schema::NAMED_TYPES.include?(type) 37 if @processed_names.include?(schema.name) 38 return schema.name 39 else 40 @processed_names << schema.name 41 end 42 end 43 44 case type 45 when *Schema::PRIMITIVE_TYPES 46 type 47 when "record" 48 fields = schema.fields.map {|field| normalize_field(field) } 49 50 normalize_named_type(schema, fields: fields) 51 when "enum" 52 normalize_named_type(schema, symbols: schema.symbols) 53 when "fixed" 54 normalize_named_type(schema, size: schema.size) 55 when "array" 56 { type: type, items: normalize_schema(schema.items) } 57 when "map" 58 { type: type, values: normalize_schema(schema.values) } 59 when "union" 60 if schema.schemas.nil? 61 [] 62 else 63 schema.schemas.map {|s| normalize_schema(s) } 64 end 65 else 66 raise "unknown type #{type}" 67 end 68 end