Module: Avro::SchemaCompatibility

Defined in:
lib/avro/schema_compatibility.rb

Defined Under Namespace

Classes: Checker

Class Method Summary collapse

Class Method Details

.can_read?(writers_schema, readers_schema) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/avro/schema_compatibility.rb', line 18

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

.match_schemas(writers_schema, readers_schema) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/avro/schema_compatibility.rb', line 26

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 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)


22
23
24
# File 'lib/avro/schema_compatibility.rb', line 22

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