Module: Avro::SchemaCompatibility

Defined in:
lib/avro-patches/schema_compatibility/schema_compatibility.rb

Overview

Defined Under Namespace

Classes: Checker

Class Method Summary collapse

Class Method Details

.can_read?(writers_schema, readers_schema) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/avro-patches/schema_compatibility/schema_compatibility.rb', line 5

def self.can_read?(writers_schema, readers_schema)
  Checker.new.can_read?(writers_schema, readers_schema)
end

.match_schemas(writers_schema, readers_schema) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/avro-patches/schema_compatibility/schema_compatibility.rb', line 13

def self.match_schemas(writers_schema, readers_schema)
  # Note: this does not support aliases!
  w_type = writers_schema.type_sym
  r_type = readers_schema.type_sym

  # This conditional is begging for some OO love.
  if w_type == :union || r_type == :union
    return true
  end

  if w_type == r_type
    return true if Avro::Schema::PRIMITIVE_TYPES_SYM.include?(r_type)

    case r_type
    when :record
      return writers_schema.fullname == readers_schema.fullname
    when :error
      return writers_schema.fullname == readers_schema.fullname
    when :request
      return true
    when :fixed
      return writers_schema.fullname == readers_schema.fullname &&
        writers_schema.size == readers_schema.size
    when :enum
      return writers_schema.fullname == readers_schema.fullname
    when :map
      return match_schemas(writers_schema.values, readers_schema.values)
    when :array
      return match_schemas(writers_schema.items, readers_schema.items)
    end
  end

  # Handle schema promotion
  if w_type == :int && [:long, :float, :double].include?(r_type)
    return true
  elsif w_type == :long && [:float, :double].include?(r_type)
    return true
  elsif w_type == :float && r_type == :double
    return true
  elsif w_type == :string && r_type == :bytes
    return true
  elsif w_type == :bytes && r_type == :string
    return true
  end

  return false
end

.mutual_read?(writers_schema, readers_schema) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/avro-patches/schema_compatibility/schema_compatibility.rb', line 9

def self.mutual_read?(writers_schema, readers_schema)
  Checker.new.mutual_read?(writers_schema, readers_schema)
end