avromatic 0.27.0 → 0.28.1

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: 5a60c2a9d61072d44905624ea8310ed0ca91d5c0
4
- data.tar.gz: 40bd23b5fbf401cb46ce58970b687e2dd914404a
3
+ metadata.gz: 361781d979e68fb4b48f288153368b9d2f00d064
4
+ data.tar.gz: d761359438529abe3ca428582c68d47a73de7587
5
5
  SHA512:
6
- metadata.gz: 1a7848c367f7bce862b342d6127438d8f9edc4b69c015700699fb0a8e3fbdacc86efe9f7df605225d336c6cc9930782183f4cf49ad2d337b9506226de7971b46
7
- data.tar.gz: 8d345cf24135af8f4fc9712b6a6d48f9bb9aa7696b276945653cc528c93fc3e550bb984fdf788e5b8846741da015b7de443951a7851dfcf73af45d85599ccaab
6
+ metadata.gz: 15122e83c1a2f3301d25873f56a668b1bd545bb5e3763bc0d56d2c1135ed812961ef786da27ca16eaaef1d3864cea2f2f6d481fc36f5829f9347e692fac91f1e
7
+ data.tar.gz: 10e242e0cc4f755992d1b955ea9e5ab6746be6c30b60d79277f96e121e44b807a96bb1a79b1a22290fd1d525a73932b48dd64b54b348ad99a4d09ff3d866fbf1
@@ -1,5 +1,13 @@
1
1
  # avromatic changelog
2
2
 
3
+ ## v0.28.1
4
+ - Fix a bug that raised an error when encoding a cached model containing optional
5
+ field(s). With this change, immutable model caching now enabled only when
6
+ `avro-patches` is present.
7
+
8
+ ## v0.28.0
9
+ - Add support for caching avro encodings for immutable models
10
+
3
11
  ## v0.27.0
4
12
  - Patches avromatic model classes to cache `Virtus::ValueObject::AllowedWriterMethods#allowed_writer_methods`
5
13
  - Support Rails 5.1
data/README.md CHANGED
@@ -99,10 +99,11 @@ end
99
99
 
100
100
  #### Encoding
101
101
  * **use_custom_datum_writer**: `Avromatic` includes a modified subclass of
102
- `Avro::IO::DatumWriter`. This subclass uses additional information about
103
- the index of union members to optimize the encoding of Avro messages.
104
- By default this information is included in the hash passed to the encoder
105
- but can be omitted by setting this option to `false`.
102
+ `Avro::IO::DatumWriter`. This subclass supports caching avro encodings for
103
+ immutable models and uses additional information about the index of union
104
+ members to optimize the encoding of Avro messages. By default this
105
+ information is included in the hash passed to the encoder but can be omitted
106
+ by setting this option to `false`.
106
107
 
107
108
 
108
109
  ### Models
@@ -4,6 +4,7 @@ require 'avromatic/model'
4
4
  require 'avromatic/model_registry'
5
5
  require 'avromatic/messaging'
6
6
  require 'active_support/core_ext/string/inflections'
7
+ require 'avromatic/patches'
7
8
 
8
9
  module Avromatic
9
10
  class << self
@@ -27,6 +28,10 @@ module Avromatic
27
28
  eager_load_models!
28
29
  end
29
30
 
31
+ def self.use_encoding_providers?
32
+ use_custom_datum_writer && defined?(Avromatic::Patches::SchemaValidatorPatch)
33
+ end
34
+
30
35
  def self.build_schema_registry
31
36
  raise 'Avromatic must be configured with a registry_url' unless registry_url
32
37
  if use_schema_fingerprint_lookup
@@ -1,6 +1,7 @@
1
1
  module Avromatic
2
2
  module IO
3
3
  UNION_MEMBER_INDEX = '__avromatic_member_index'.freeze
4
+ ENCODING_PROVIDER = '__avromatic_encoding_provider'.freeze
4
5
  end
5
6
  end
6
7
 
@@ -20,6 +20,14 @@ module Avromatic
20
20
  encoder.write_long(index_of_schema)
21
21
  write_data(writers_schema.schemas[index_of_schema], datum, encoder)
22
22
  end
23
+
24
+ def write_record(writers_schema, datum, encoder)
25
+ if datum.is_a?(Hash) && datum.key?(Avromatic::IO::ENCODING_PROVIDER)
26
+ encoder.write(datum[Avromatic::IO::ENCODING_PROVIDER].avro_raw_value)
27
+ else
28
+ super
29
+ end
30
+ end
23
31
  end
24
32
  end
25
33
  end
@@ -29,6 +29,11 @@ module Avromatic
29
29
  if value.is_a?(Avromatic::Model::Attributes)
30
30
  hash = value.value_attributes_for_avro
31
31
  if Avromatic.use_custom_datum_writer
32
+ if Avromatic.use_encoding_providers? && !value.class.config.mutable
33
+ # n.b. Ideally we'd just return value here instead of wrapping it in a
34
+ # hash but then we'd have no place to stash the union member index...
35
+ hash = { Avromatic::IO::ENCODING_PROVIDER => value }
36
+ end
32
37
  member_index = member_types.index(value.class) if member_types.any?
33
38
  hash[Avromatic::IO::UNION_MEMBER_INDEX] = member_index if member_index
34
39
  end
@@ -67,7 +72,11 @@ module Avromatic
67
72
  end
68
73
 
69
74
  def avro_raw_value
70
- avro_raw_encode(value_attributes_for_avro, :value)
75
+ if self.class.config.mutable
76
+ avro_raw_encode(value_attributes_for_avro, :value)
77
+ else
78
+ @avro_raw_value ||= avro_raw_encode(value_attributes_for_avro, :value)
79
+ end
71
80
  end
72
81
 
73
82
  def avro_raw_key
@@ -0,0 +1,11 @@
1
+ loaded_avro_patches = begin
2
+ require 'avro-patches'
3
+ true
4
+ rescue LoadError
5
+ false
6
+ end
7
+
8
+ if loaded_avro_patches
9
+ require 'avromatic/patches/schema_validator_patch'
10
+ Avro::SchemaValidator.singleton_class.prepend(Avromatic::Patches::SchemaValidatorPatch)
11
+ end
@@ -0,0 +1,20 @@
1
+ module Avromatic
2
+ module Patches
3
+ module SchemaValidatorPatch
4
+ # This method replaces validate_recursive in AvroPatches::LogicalTypes::SchemaValidatorPatch
5
+ # to enable validating datums that contain an encoding provider.
6
+ def validate_recursive(expected_schema, logical_datum, path, result, encoded = false)
7
+ datum = resolve_datum(expected_schema, logical_datum, encoded)
8
+ case expected_schema.type_sym
9
+ when :record, :error, :request
10
+ if datum.is_a?(Hash) && datum.key?(Avromatic::IO::ENCODING_PROVIDER)
11
+ return if expected_schema.sha256_resolution_fingerprint ==
12
+ datum[Avromatic::IO::ENCODING_PROVIDER].value_avro_schema.sha256_resolution_fingerprint
13
+ raise Avro::SchemaValidator::ValidationError
14
+ end
15
+ end
16
+ super(expected_schema, logical_datum, path, result, encoded)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Avromatic
2
- VERSION = '0.27.0'.freeze
2
+ VERSION = '0.28.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avromatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.0
4
+ version: 0.28.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify Engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-02 00:00:00.000000000 Z
11
+ date: 2017-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -304,6 +304,8 @@ files:
304
304
  - lib/avromatic/model/validation.rb
305
305
  - lib/avromatic/model/value_object.rb
306
306
  - lib/avromatic/model_registry.rb
307
+ - lib/avromatic/patches.rb
308
+ - lib/avromatic/patches/schema_validator_patch.rb
307
309
  - lib/avromatic/railtie.rb
308
310
  - lib/avromatic/rspec.rb
309
311
  - lib/avromatic/version.rb