class Avro::SchemaCompatibility::Checker

Constants

SIMPLE_CHECKS

Attributes

recursion_set[R]

Public Class Methods

new() click to toggle source
   # File lib/avro/schema_compatibility.rb
92 def initialize
93   @recursion_set = Set.new
94 end

Public Instance Methods

can_read?(writers_schema, readers_schema) click to toggle source
   # File lib/avro/schema_compatibility.rb
96 def can_read?(writers_schema, readers_schema)
97   full_match_schemas(writers_schema, readers_schema)
98 end
mutual_read?(writers_schema, readers_schema) click to toggle source
    # File lib/avro/schema_compatibility.rb
100 def mutual_read?(writers_schema, readers_schema)
101   can_read?(writers_schema, readers_schema) && can_read?(readers_schema, writers_schema)
102 end

Private Instance Methods

full_match_schemas(writers_schema, readers_schema) click to toggle source
    # File lib/avro/schema_compatibility.rb
106 def full_match_schemas(writers_schema, readers_schema)
107   return true if recursion_in_progress?(writers_schema, readers_schema)
108 
109   return false unless Avro::SchemaCompatibility.match_schemas(writers_schema, readers_schema)
110 
111   if writers_schema.type_sym != :union && SIMPLE_CHECKS.include?(readers_schema.type_sym)
112     return true
113   end
114 
115   case readers_schema.type_sym
116   when :record
117     match_record_schemas(writers_schema, readers_schema)
118   when :map
119     full_match_schemas(writers_schema.values, readers_schema.values)
120   when :array
121     full_match_schemas(writers_schema.items, readers_schema.items)
122   when :union
123     match_union_schemas(writers_schema, readers_schema)
124   when :enum
125     # reader's symbols must contain all writer's symbols or reader has default
126     (writers_schema.symbols - readers_schema.symbols).empty? || !readers_schema.default.nil?
127   else
128     if writers_schema.type_sym == :union && writers_schema.schemas.size == 1
129       full_match_schemas(writers_schema.schemas.first, readers_schema)
130     else
131       false
132     end
133   end
134 end
match_record_schemas(writers_schema, readers_schema) click to toggle source
    # File lib/avro/schema_compatibility.rb
147 def match_record_schemas(writers_schema, readers_schema)
148   return false if writers_schema.type_sym == :union
149 
150   writer_fields_hash = writers_schema.fields_hash
151   readers_schema.fields.each do |field|
152     if writer_fields_hash.key?(field.name)
153       return false unless full_match_schemas(writer_fields_hash[field.name].type, field.type)
154     else
155       names = writer_fields_hash.keys & field.alias_names
156       if names.size > 1
157         return false
158       elsif names.size == 1
159         return false unless full_match_schemas(writer_fields_hash[names.first].type, field.type)
160       else
161         return false unless field.default?
162       end
163     end
164   end
165 
166   return true
167 end
match_union_schemas(writers_schema, readers_schema) click to toggle source
    # File lib/avro/schema_compatibility.rb
136 def match_union_schemas(writers_schema, readers_schema)
137   raise 'readers_schema must be a union' unless readers_schema.type_sym == :union
138 
139   case writers_schema.type_sym
140   when :union
141     writers_schema.schemas.all? { |writer_type| full_match_schemas(writer_type, readers_schema) }
142   else
143     readers_schema.schemas.any? { |reader_type| full_match_schemas(writers_schema, reader_type) }
144   end
145 end
recursion_in_progress?(writers_schema, readers_schema) click to toggle source
    # File lib/avro/schema_compatibility.rb
169 def recursion_in_progress?(writers_schema, readers_schema)
170   key = [writers_schema.object_id, readers_schema.object_id]
171 
172   if recursion_set.include?(key)
173     true
174   else
175     recursion_set.add(key)
176     false
177   end
178 end