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

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/avro/schema_compatibility.rb', line 20

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

.match_schemas(writers_schema, readers_schema) ⇒ Object

Perform a basic check that a datum written with the writers_schema could be read using the readers_schema. This check only includes matching the types, including schema promotion, and matching the full name for named types. Aliases for named types are not supported here, and the ruby implementation of Avro in general does not include support for aliases.



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
73
74
75
76
77
78
79
80
# File 'lib/avro/schema_compatibility.rb', line 35

def self.match_schemas(writers_schema, readers_schema)
  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

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.

Returns:

  • (Boolean)


26
27
28
# File 'lib/avro/schema_compatibility.rb', line 26

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