module Avro::SchemaCompatibility

Constants

INT_COERCIBLE_TYPES_SYM
LONG_COERCIBLE_TYPES_SYM

Public Class Methods

can_read?(writers_schema, readers_schema) click to toggle source

Perform a full, recursive check that a datum written using the writers_schema can be read using the readers_schema.

   # File lib/avro/schema_compatibility.rb
24 def self.can_read?(writers_schema, readers_schema)
25   Checker.new.can_read?(writers_schema, readers_schema)
26 end
match_schemas(writers_schema, readers_schema) click to toggle source

Perform a basic check that a datum written with the writers_schema could be read using the readers_schema. This check includes matching the types, including schema promotion, and matching the full name (including aliases) for named types.

   # File lib/avro/schema_compatibility.rb
37 def self.match_schemas(writers_schema, readers_schema)
38   # Bypass deeper checks if the schemas are the same Ruby objects
39   return true if writers_schema.equal?(readers_schema)
40 
41   w_type = writers_schema.type_sym
42   r_type = readers_schema.type_sym
43 
44   # This conditional is begging for some OO love.
45   if w_type == :union || r_type == :union
46     return true
47   end
48 
49   if w_type == r_type
50     return readers_schema.match_schema?(writers_schema) if Schema::PRIMITIVE_TYPES_SYM.include?(r_type)
51 
52     case r_type
53     when :request
54       return true
55     when :map
56       return match_schemas(writers_schema.values, readers_schema.values)
57     when :array
58       return match_schemas(writers_schema.items, readers_schema.items)
59     else
60       return readers_schema.match_schema?(writers_schema)
61     end
62   end
63 
64   # Handle schema promotion
65   # rubocop:disable Lint/DuplicateBranch
66   if w_type == :int && INT_COERCIBLE_TYPES_SYM.include?(r_type)
67     return true
68   elsif w_type == :long && LONG_COERCIBLE_TYPES_SYM.include?(r_type)
69     return true
70   elsif w_type == :float && r_type == :double
71     return true
72   elsif w_type == :string && r_type == :bytes
73     return true
74   elsif w_type == :bytes && r_type == :string
75     return true
76   end
77   # rubocop:enable Lint/DuplicateBranch
78 
79   if readers_schema.respond_to?(:match_schema?)
80     readers_schema.match_schema?(writers_schema)
81   else
82     false
83   end
84 end
mutual_read?(writers_schema, readers_schema) click to toggle source

Perform a full, recursive check that a datum written using either the writers_schema or the readers_schema can be read using the other schema.

   # File lib/avro/schema_compatibility.rb
30 def self.mutual_read?(writers_schema, readers_schema)
31   Checker.new.mutual_read?(writers_schema, readers_schema)
32 end