avro-patches 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e27ae4a91d001f377ddce29a8b2b4c6b10fb143c
4
- data.tar.gz: f869f474d9baa39737b33d686bd50b6f08f20eae
3
+ metadata.gz: 4501694b4e822ab5d2f9b08cd4d2406474b7efa6
4
+ data.tar.gz: 7cb86acf0bb9cb38486d59263904e1eac8eb4be7
5
5
  SHA512:
6
- metadata.gz: d7a84490ba988ac5a9fb3c6146465f74578b383af85ae91d1d6a3580096216dc0e071c827be1d15e3b7065d2cdb71fffa659765a57c0c37667a6bf4988e3eee6
7
- data.tar.gz: 70740d128af9aed6bbab211477b7dd8c4cd359c71e6319249e8f5c3876f91e743986bbd93576e501e1e7f6ceabc0d1a664bc29fa4d9b3712397add89f425d73e
6
+ metadata.gz: 7d2aa4dc274fef6e4727ac3d3842c984223b0ab8ab2fdaed522490d4a4a28d15ae636fb7844aea8f5ff20f0983e698c9d8aec42c3e7e3ba7b2b08e9e1a692761
7
+ data.tar.gz: 03b0e275a36bfbf88e18638661bbc59bcad4a423a6767743a5560c9b2092e8a25a829128c941d8bb3b79693926b478361d78b47826e3b37939e0a94a7f2c172c
@@ -1,4 +1,7 @@
1
1
  # avro-patches
2
2
 
3
+ ## v0.2.0
4
+ - Performance improvements for `Avro::SchemaValidator`.
5
+
3
6
  ## v0.1.0
4
7
  - Initial version
@@ -20,7 +20,8 @@ module Avro
20
20
  PATH_SEPARATOR = '.'.freeze
21
21
  INT_RANGE = Schema::INT_MIN_VALUE..Schema::INT_MAX_VALUE
22
22
  LONG_RANGE = Schema::LONG_MIN_VALUE..Schema::LONG_MAX_VALUE
23
- COMPLEX_TYPES = [:array, :error, :map, :record, :request]
23
+ COMPLEX_TYPES = [:array, :error, :map, :record, :request].freeze
24
+ BOOLEAN_VALUES = [true, false].freeze
24
25
 
25
26
  class Result
26
27
  attr_reader :errors
@@ -76,7 +77,7 @@ module Avro
76
77
  when :null
77
78
  fail TypeMismatchError unless datum.nil?
78
79
  when :boolean
79
- fail TypeMismatchError unless [true, false].include?(datum)
80
+ fail TypeMismatchError unless BOOLEAN_VALUES.include?(datum)
80
81
  when :string, :bytes
81
82
  fail TypeMismatchError unless datum.is_a?(String)
82
83
  when :int
@@ -86,17 +87,15 @@ module Avro
86
87
  fail TypeMismatchError unless datum.is_a?(Integer)
87
88
  result.add_error(path, "out of bound value #{datum}") unless LONG_RANGE.cover?(datum)
88
89
  when :float, :double
89
- fail TypeMismatchError unless [Float, Integer].any?(&datum.method(:is_a?))
90
+ fail TypeMismatchError unless datum.is_a?(Float) || datum.is_a?(Integer)
90
91
  when :fixed
91
92
  if datum.is_a? String
92
- message = "expected fixed with size #{expected_schema.size}, got \"#{datum}\" with size #{datum.size}"
93
- result.add_error(path, message) unless datum.bytesize == expected_schema.size
93
+ result.add_error(path, fixed_string_message(expected_schema.size, datum)) unless datum.bytesize == expected_schema.size
94
94
  else
95
95
  result.add_error(path, "expected fixed with size #{expected_schema.size}, got #{actual_value_message(datum)}")
96
96
  end
97
97
  when :enum
98
- message = "expected enum with values #{expected_schema.symbols}, got #{actual_value_message(datum)}"
99
- result.add_error(path, message) unless expected_schema.symbols.include?(datum)
98
+ result.add_error(path, enum_message(expected_schema.symbols, datum)) unless expected_schema.symbols.include?(datum)
100
99
  when :array
101
100
  validate_array(expected_schema, datum, path, result)
102
101
  when :map
@@ -116,6 +115,14 @@ module Avro
116
115
  result.add_error(path, "expected type #{expected_schema.type_sym}, got #{actual_value_message(datum)}")
117
116
  end
118
117
 
118
+ def fixed_string_message(size, datum)
119
+ "expected fixed with size #{size}, got \"#{datum}\" with size #{datum.size}"
120
+ end
121
+
122
+ def enum_message(symbols, datum)
123
+ "expected enum with values #{symbols}, got #{actual_value_message(datum)}"
124
+ end
125
+
119
126
  def validate_array(expected_schema, datum, path, result)
120
127
  fail TypeMismatchError unless datum.is_a?(Array)
121
128
  datum.each_with_index do |d, i|
@@ -138,9 +145,10 @@ module Avro
138
145
  validate_recursive(expected_schema.schemas.first, datum, path, result)
139
146
  return
140
147
  end
141
- types_and_results = validate_possible_types(datum, expected_schema, path)
142
- failures, successes = types_and_results.partition { |r| r[:result].failure? }
143
- return if successes.any?
148
+ failures = []
149
+ compatible_type = first_compatible_type(datum, expected_schema, path, failures)
150
+ return unless compatible_type.nil?
151
+
144
152
  complex_type_failed = failures.detect { |r| COMPLEX_TYPES.include?(r[:type]) }
145
153
  if complex_type_failed
146
154
  complex_type_failed[:result].errors.each { |error| result << error }
@@ -150,11 +158,12 @@ module Avro
150
158
  end
151
159
  end
152
160
 
153
- def validate_possible_types(datum, expected_schema, path)
154
- expected_schema.schemas.map do |schema|
161
+ def first_compatible_type(datum, expected_schema, path, failures)
162
+ expected_schema.schemas.find do |schema|
155
163
  result = Result.new
156
164
  validate_recursive(schema, datum, path, result)
157
- { type: schema.type_sym, result: result }
165
+ failures << { type: schema.type_sym, result: result } if result.failure?
166
+ !result.failure?
158
167
  end
159
168
  end
160
169
 
@@ -162,8 +171,6 @@ module Avro
162
171
  "#{path}#{PATH_SEPARATOR}#{sub_key}".squeeze(PATH_SEPARATOR)
163
172
  end
164
173
 
165
- private
166
-
167
174
  def actual_value_message(value)
168
175
  avro_type = if value.is_a?(Integer)
169
176
  ruby_integer_to_avro_type(value)
@@ -1,3 +1,3 @@
1
1
  module AvroPatches
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avro-patches
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify, Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-31 00:00:00.000000000 Z
11
+ date: 2017-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler