lib/avro/schema.rb in avro-salsify-fork-1.9.0.2 vs lib/avro/schema.rb in avro-salsify-fork-1.9.0.3.pre1

- old
+ new

@@ -162,10 +162,22 @@ def sha256_fingerprint parsing_form = SchemaNormalization.to_parsing_form(self) Digest::SHA256.hexdigest(parsing_form).to_i(16) end + def read?(writers_schema) + SchemaCompatibility.can_read?(writers_schema, self) + end + + def be_read?(other_schema) + other_schema.read?(self) + end + + def mutual_read?(other_schema) + SchemaCompatibility.mutual_read?(other_schema, self) + end + def ==(other, seen=nil) other.is_a?(Schema) && type_sym == other.type_sym end def hash(seen=nil) @@ -226,11 +238,11 @@ field_objects, field_names = [], Set.new field_data.each_with_index do |field, i| if field.respond_to?(:[]) # TODO(jmhodges) wtffffff type = field['type'] name = field['name'] - default = field['default'] + default = field.key?('default') ? field['default'] : :no_default order = field['order'] new_field = Field.new(type, name, default, order, names, namespace) # make sure field name has not been used yet if field_names.include?(new_field.name) raise SchemaParseError, "Field name #{new_field.name.inspect} is already in use" @@ -249,11 +261,15 @@ @type_sym = schema_type.to_sym @namespace = namespace else super(schema_type, name, namespace, names) end - @fields = RecordSchema.make_field_objects(fields, names, self.namespace) + @fields = if fields + RecordSchema.make_field_objects(fields, names, self.namespace) + else + {} + end end def fields_hash @fields_hash ||= fields.inject({}){|hsh, field| hsh[field.name] = field; hsh } end @@ -300,12 +316,11 @@ attr_reader :schemas def initialize(schemas, names=nil, default_namespace=nil) super(:union) - schema_objects = [] - schemas.each_with_index do |schema, i| + @schemas = schemas.each_with_object([]) do |schema, schema_objects| new_schema = subparse(schema, names, default_namespace) ns_type = new_schema.type_sym if VALID_TYPES_SYM.include?(ns_type) && !NAMED_TYPES_SYM.include?(ns_type) && @@ -314,11 +329,10 @@ elsif ns_type == :union raise SchemaParseError, "Unions cannot contain other unions" else schema_objects << new_schema end - @schemas = schema_objects end end def to_avro(names=Set.new) schemas.map {|schema| schema.to_avro(names) } @@ -378,19 +392,23 @@ end class Field < Schema attr_reader :type, :name, :default, :order - def initialize(type, name, default=nil, order=nil, names=nil, namespace=nil) + def initialize(type, name, default=:no_default, order=nil, names=nil, namespace=nil) @type = subparse(type, names, namespace) @name = name @default = default @order = order end + def default? + @default != :no_default + end + def to_avro(names=Set.new) {'name' => name, 'type' => type.to_avro(names)}.tap do |avro| - avro['default'] = default if default + avro['default'] = default if default? avro['order'] = order if order end end end end