avromatic 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +85 -0
  3. data/.rubocop.yml +3 -0
  4. data/.ruby-version +1 -1
  5. data/Appraisals +34 -22
  6. data/CHANGELOG.md +16 -0
  7. data/README.md +3 -3
  8. data/avromatic.gemspec +7 -6
  9. data/bin/console +4 -3
  10. data/gemfiles/{rails5_0.gemfile → avro1_10_rails5_2.gemfile} +3 -3
  11. data/gemfiles/avro1_10_rails6_0.gemfile +9 -0
  12. data/gemfiles/avro1_10_rails6_1.gemfile +9 -0
  13. data/gemfiles/{rails5_2.gemfile → avro1_8_rails5_2.gemfile} +0 -0
  14. data/gemfiles/{rails5_1.gemfile → avro1_9_rails5_2.gemfile} +3 -3
  15. data/gemfiles/{rails6_0.gemfile → avro1_9_rails6_0.gemfile} +1 -1
  16. data/gemfiles/{avro_patches_rails5_0.gemfile → avro1_9_rails6_1.gemfile} +3 -3
  17. data/gemfiles/avro_patches_rails5_2.gemfile +1 -1
  18. data/gemfiles/avro_patches_rails6_1.gemfile +9 -0
  19. data/lib/avromatic.rb +0 -5
  20. data/lib/avromatic/io.rb +1 -7
  21. data/lib/avromatic/io/datum_reader.rb +18 -68
  22. data/lib/avromatic/io/datum_writer.rb +5 -11
  23. data/lib/avromatic/io/union_datum.rb +25 -0
  24. data/lib/avromatic/messaging.rb +4 -2
  25. data/lib/avromatic/model/attributes.rb +6 -6
  26. data/lib/avromatic/model/configurable.rb +24 -0
  27. data/lib/avromatic/model/messaging_serialization.rb +2 -1
  28. data/lib/avromatic/model/raw_serialization.rb +14 -11
  29. data/lib/avromatic/model/types/abstract_timestamp_type.rb +1 -1
  30. data/lib/avromatic/model/types/abstract_type.rb +3 -1
  31. data/lib/avromatic/model/types/array_type.rb +2 -2
  32. data/lib/avromatic/model/types/boolean_type.rb +1 -1
  33. data/lib/avromatic/model/types/custom_type.rb +1 -1
  34. data/lib/avromatic/model/types/date_type.rb +1 -1
  35. data/lib/avromatic/model/types/enum_type.rb +1 -1
  36. data/lib/avromatic/model/types/fixed_type.rb +1 -1
  37. data/lib/avromatic/model/types/float_type.rb +1 -1
  38. data/lib/avromatic/model/types/integer_type.rb +1 -1
  39. data/lib/avromatic/model/types/map_type.rb +2 -2
  40. data/lib/avromatic/model/types/null_type.rb +1 -1
  41. data/lib/avromatic/model/types/record_type.rb +1 -5
  42. data/lib/avromatic/model/types/string_type.rb +1 -1
  43. data/lib/avromatic/model/types/timestamp_micros_type.rb +4 -4
  44. data/lib/avromatic/model/types/timestamp_millis_type.rb +4 -4
  45. data/lib/avromatic/model/types/union_type.rb +11 -8
  46. data/lib/avromatic/version.rb +1 -1
  47. metadata +62 -47
  48. data/.travis.yml +0 -23
  49. data/gemfiles/avro_patches_rails5_1.gemfile +0 -9
  50. data/lib/avromatic/patches.rb +0 -18
  51. data/lib/avromatic/patches/schema_validator_patch.rb +0 -39
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cba28133aa9a1a1ce5bb9a03c54fc4aa01f90d6dd3a72b98d74e5eb47276925e
4
- data.tar.gz: 4fc8e33b860c6213fdda238946799164f8f52655b8cd3d5995f2c095e63aa2e1
3
+ metadata.gz: e695eda252fc8374491c000581104f96383a397a118097e9b9c87b256823b267
4
+ data.tar.gz: 792f85d1fde7b223491265ff9a82024373063aeb942daa8818510e4fdbfb4d65
5
5
  SHA512:
6
- metadata.gz: a027c37208e834dee9772ea0fc2cfdd0f0a65699426e4e89b00e32268f3938a2842d1b4101fdd50f04f0b61d151f8ea9a2dd08bba168875b47b39636053eebe6
7
- data.tar.gz: 7568eb10634ab6dc9e80236c8cedccd18a7259858c4a4834797e91f3349f43cf34d0dec81b5cf3fec4c2cb9c8400002d4066c9240a27eca6d0751f470a1faffa
6
+ metadata.gz: e0fbee30489961e56e3ce6a7026beb7e687a38de373c72fd0cc6c39a0afa02ef1618f2c730e4647a3339059591ad2252744a0f1a760a1f5282dafeb0cfd93ff8
7
+ data.tar.gz: ef315a61ba34cefec8508a20d64eb6eb4fe530837c9ade67ac8a6075e146c3516a9dbc0a9cfc34ec71265b333865f93a4d36c588d9c9703f80b5a455e1bad408
@@ -0,0 +1,85 @@
1
+ version: 2.1
2
+ jobs:
3
+ lint:
4
+ docker:
5
+ - image: salsify/ruby_ci:2.5.8
6
+ working_directory: ~/avromatic
7
+ steps:
8
+ - checkout
9
+ - restore_cache:
10
+ keys:
11
+ - v2-gems-ruby-2.5.8-{{ checksum "avromatic.gemspec" }}-{{ checksum "Gemfile" }}
12
+ - v2-gems-ruby-2.5.8-
13
+ - run:
14
+ name: Install Gems
15
+ command: |
16
+ if ! bundle check --path=vendor/bundle; then
17
+ bundle install --path=vendor/bundle --jobs=4 --retry=3
18
+ bundle clean
19
+ fi
20
+ - save_cache:
21
+ key: v2-gems-ruby-2.5.8-{{ checksum "avromatic.gemspec" }}-{{ checksum "Gemfile" }}
22
+ paths:
23
+ - "vendor/bundle"
24
+ - "gemfiles/vendor/bundle"
25
+ - run:
26
+ name: Run Rubocop
27
+ command: bundle exec rubocop
28
+ test:
29
+ parameters:
30
+ gemfile:
31
+ type: string
32
+ ruby-version:
33
+ type: string
34
+ docker:
35
+ - image: salsify/ruby_ci:<< parameters.ruby-version >>
36
+ environment:
37
+ CIRCLE_TEST_REPORTS: "test-results"
38
+ BUNDLE_GEMFILE: << parameters.gemfile >>
39
+ working_directory: ~/avromatic
40
+ steps:
41
+ - checkout
42
+ - restore_cache:
43
+ keys:
44
+ - v2-gems-ruby-<< parameters.ruby-version >>-{{ checksum "avromatic.gemspec" }}-{{ checksum "<< parameters.gemfile >>" }}
45
+ - v2-gems-ruby-<< parameters.ruby-version >>-
46
+ - run:
47
+ name: Install Gems
48
+ command: |
49
+ if ! bundle check --path=vendor/bundle; then
50
+ bundle install --path=vendor/bundle --jobs=4 --retry=3
51
+ bundle clean
52
+ fi
53
+ - save_cache:
54
+ key: v2-gems-ruby-<< parameters.ruby-version >>-{{ checksum "avromatic.gemspec" }}-{{ checksum "<< parameters.gemfile >>" }}
55
+ paths:
56
+ - "vendor/bundle"
57
+ - "gemfiles/vendor/bundle"
58
+ - run:
59
+ name: Run Tests
60
+ command: |
61
+ bundle exec rspec --format RspecJunitFormatter --out $CIRCLE_TEST_REPORTS/rspec/junit.xml --format progress spec
62
+ - store_test_results:
63
+ path: "test-results"
64
+ workflows:
65
+ build:
66
+ jobs:
67
+ - lint
68
+ - test:
69
+ matrix:
70
+ parameters:
71
+ gemfile:
72
+ - "gemfiles/avro1_8_rails5_2.gemfile"
73
+ - "gemfiles/avro1_9_rails5_2.gemfile"
74
+ - "gemfiles/avro1_10_rails5_2.gemfile"
75
+ - "gemfiles/avro1_9_rails6_0.gemfile"
76
+ - "gemfiles/avro1_10_rails6_0.gemfile"
77
+ - "gemfiles/avro_patches_rails5_2.gemfile"
78
+ - "gemfiles/avro_patches_rails6_0.gemfile"
79
+ - "gemfiles/avro1_10_rails6_1.gemfile"
80
+ - "gemfiles/avro1_9_rails6_1.gemfile"
81
+ - "gemfiles/avro_patches_rails6_1.gemfile"
82
+ ruby-version:
83
+ - "2.5.8"
84
+ - "2.6.6"
85
+ - "2.7.1"
@@ -3,6 +3,9 @@ inherit_gem:
3
3
 
4
4
  AllCops:
5
5
  TargetRubyVersion: 2.4
6
+ Exclude:
7
+ - 'vendor/**/*'
8
+ - 'gemfiles/vendor/**/*'
6
9
 
7
10
  Style/MultilineBlockChain:
8
11
  Enabled: false
@@ -1 +1 @@
1
- 2.5.1
1
+ 2.5.8
data/Appraisals CHANGED
@@ -1,43 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- appraise 'rails5_0' do
3
+ appraise 'avro1_8-rails5_2' do
4
4
  gem 'avro', '1.8.2'
5
- gem 'activesupport', '~> 5.0.6'
6
- gem 'activemodel', '~> 5.0.6'
5
+ gem 'activesupport', '~> 5.2.0'
6
+ gem 'activemodel', '~> 5.2.0'
7
7
  end
8
8
 
9
- appraise 'rails5_1' do
10
- gem 'avro', '1.8.2'
11
- gem 'activesupport', '~> 5.1.4'
12
- gem 'activemodel', '~> 5.1.4'
9
+ appraise 'avro1_9-rails5_2' do
10
+ gem 'avro', '1.9.2'
11
+ gem 'activesupport', '~> 5.2.0'
12
+ gem 'activemodel', '~> 5.2.0'
13
13
  end
14
14
 
15
- appraise 'rails5_2' do
16
- gem 'avro', '1.8.2'
15
+ appraise 'avro1_10-rails5_2' do
16
+ gem 'avro', '~> 1.10.0'
17
17
  gem 'activesupport', '~> 5.2.0'
18
18
  gem 'activemodel', '~> 5.2.0'
19
19
  end
20
20
 
21
- appraise 'rails6_0' do
22
- gem 'avro', '1.9.0'
21
+ appraise 'avro1_9-rails6_0' do
22
+ gem 'avro', '1.9.2'
23
23
  gem 'activesupport', '~> 6.0.0'
24
24
  gem 'activemodel', '~> 6.0.0'
25
25
  end
26
26
 
27
- appraise 'avro-patches-rails5_0' do
28
- gem 'avro-patches'
29
- gem 'activesupport', '~> 5.0.6'
30
- gem 'activemodel', '~> 5.0.6'
31
- end
32
-
33
- appraise 'avro-patches-rails5_1' do
34
- gem 'avro-patches', '>= 0.4.1'
35
- gem 'activesupport', '~> 5.1.4'
36
- gem 'activemodel', '~> 5.1.4'
27
+ appraise 'avro1_10-rails6_0' do
28
+ gem 'avro', '~> 1.10.0'
29
+ gem 'activesupport', '~> 6.0.0'
30
+ gem 'activemodel', '~> 6.0.0'
37
31
  end
38
32
 
39
33
  appraise 'avro-patches-rails5_2' do
40
- gem 'avro-patches', '>= 0.4.1'
34
+ gem 'avro-patches', '>= 0.4.1', '< 1.0'
41
35
  gem 'activesupport', '~> 5.2.0'
42
36
  gem 'activemodel', '~> 5.2.0'
43
37
  end
@@ -47,3 +41,21 @@ appraise 'avro-patches-rails6_0' do
47
41
  gem 'activesupport', '~> 6.0.0'
48
42
  gem 'activemodel', '~> 6.0.0'
49
43
  end
44
+
45
+ appraise 'avro1_9-rails6_1' do
46
+ gem 'avro', '1.9.2'
47
+ gem 'activesupport', '~> 6.1.0'
48
+ gem 'activemodel', '~> 6.1.0'
49
+ end
50
+
51
+ appraise 'avro1_10-rails6_1' do
52
+ gem 'avro', '~> 1.10.0'
53
+ gem 'activesupport', '~> 6.1.0'
54
+ gem 'activemodel', '~> 6.1.0'
55
+ end
56
+
57
+ appraise 'avro-patches-rails6_1' do
58
+ gem 'avro-patches', '>= 1.0.0'
59
+ gem 'activesupport', '~> 6.1.0'
60
+ gem 'activemodel', '~> 6.1.0'
61
+ end
@@ -1,5 +1,21 @@
1
1
  # avromatic changelog
2
2
 
3
+ ## v2.3.0
4
+ - Add support for Rails 6.1.
5
+ - Optimize nested model serialization.
6
+
7
+ ## v2.2.6
8
+ - Optimize memory usage when serializing models.
9
+
10
+ ## v2.2.5
11
+ - Optimize memory usage when serializing, deserializing and instantiating models.
12
+
13
+ ## v2.2.4
14
+ - Compatibility with Avro v1.10.x.
15
+
16
+ ## v2.2.3
17
+ - Fix bug where method `#referenced_model_classes` was declared as private instead of public.
18
+
3
19
  ## v2.2.2
4
20
  - Fix missing models in the model registry when in development by loading the nested models of eager loaded models.
5
21
  - Fake schema registry support for stubbing URLs with usernames and passwords.
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Avromatic
2
2
 
3
- [![Build Status](https://travis-ci.org/salsify/avromatic.svg?branch=master)][travis]
3
+ [![Build Status](https://circleci.com/gh/salsify/avromatic.svg?style=svg)][circleci]
4
4
  [![Gem Version](https://badge.fury.io/rb/avromatic.svg)](https://badge.fury.io/rb/avromatic)
5
5
 
6
- [travis]: http://travis-ci.org/salsify/avromatic
6
+ [circleci]: https://circleci.com/gh/salsify/avromatic
7
7
 
8
8
  `Avromatic` generates Ruby models from [Avro](http://avro.apache.org/) schemas
9
9
  and provides utilities to encode and decode them.
@@ -142,7 +142,7 @@ instance.eql?(MyModel.new(id: 123, name: 'Tesla Model 3', enabled: true)) # => t
142
142
  instance.hash # => -1279155042741869898
143
143
 
144
144
  # Retrieve a hash of the model's attributes via to_h, to_hash or attributes
145
- instance .to_h # => {:id=>123, :name=>"Tesla Model 3", :enabled=>true}
145
+ instance.to_h # => {:id=>123, :name=>"Tesla Model 3", :enabled=>true}
146
146
  ```
147
147
 
148
148
  Or an `Avro::Schema` object can be specified directly:
@@ -22,22 +22,23 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.required_ruby_version = '>= 2.4'
24
24
 
25
- spec.add_runtime_dependency 'activemodel', '>= 5.0', '< 6.1'
26
- spec.add_runtime_dependency 'activesupport', '>= 5.0', '< 6.1'
27
- spec.add_runtime_dependency 'avro', '>= 1.7.7', '< 1.10'
25
+ spec.add_runtime_dependency 'activemodel', '>= 5.0', '< 6.2'
26
+ spec.add_runtime_dependency 'activesupport', '>= 5.0', '< 6.2'
27
+ spec.add_runtime_dependency 'avro', '>= 1.7.7', '< 1.11'
28
28
  spec.add_runtime_dependency 'avro_schema_registry-client', '>= 0.3.0'
29
29
  spec.add_runtime_dependency 'avro_turf'
30
30
  spec.add_runtime_dependency 'ice_nine'
31
31
 
32
+ spec.add_development_dependency 'appraisal'
32
33
  spec.add_development_dependency 'avro-builder', '>= 0.12.0'
33
34
  spec.add_development_dependency 'bundler', '>= 1.11'
35
+ spec.add_development_dependency 'overcommit', '0.35.0'
34
36
  spec.add_development_dependency 'rake', '~> 10.0'
35
37
  spec.add_development_dependency 'rspec', '~> 3.0'
38
+ spec.add_development_dependency 'rspec_junit_formatter'
39
+ spec.add_development_dependency 'salsify_rubocop', '~> 0.52.1.1'
36
40
  spec.add_development_dependency 'simplecov'
37
41
  spec.add_development_dependency 'webmock'
38
42
  # For AvroSchemaRegistry::FakeServer
39
- spec.add_development_dependency 'appraisal'
40
- spec.add_development_dependency 'overcommit', '0.35.0'
41
- spec.add_development_dependency 'salsify_rubocop', '~> 0.52.1.1'
42
43
  spec.add_development_dependency 'sinatra'
43
44
  end
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "avromatic"
4
+ require 'bundler/setup'
5
+ require 'avromatic'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "avromatic"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start
@@ -2,8 +2,8 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "avro", "1.8.2"
6
- gem "activesupport", "~> 5.0.6"
7
- gem "activemodel", "~> 5.0.6"
5
+ gem "avro", "~> 1.10.0"
6
+ gem "activesupport", "~> 5.2.0"
7
+ gem "activemodel", "~> 5.2.0"
8
8
 
9
9
  gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "avro", "~> 1.10.0"
6
+ gem "activesupport", "~> 6.0.0"
7
+ gem "activemodel", "~> 6.0.0"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "avro", "~> 1.10.0"
6
+ gem "activesupport", "~> 6.1.0"
7
+ gem "activemodel", "~> 6.1.0"
8
+
9
+ gemspec path: "../"
@@ -2,8 +2,8 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "avro", "1.8.2"
6
- gem "activesupport", "~> 5.1.4"
7
- gem "activemodel", "~> 5.1.4"
5
+ gem "avro", "1.9.2"
6
+ gem "activesupport", "~> 5.2.0"
7
+ gem "activemodel", "~> 5.2.0"
8
8
 
9
9
  gemspec path: "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "avro", "1.9.0"
5
+ gem "avro", "1.9.2"
6
6
  gem "activesupport", "~> 6.0.0"
7
7
  gem "activemodel", "~> 6.0.0"
8
8
 
@@ -2,8 +2,8 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "avro-patches"
6
- gem "activesupport", "~> 5.0.6"
7
- gem "activemodel", "~> 5.0.6"
5
+ gem "avro", "1.9.2"
6
+ gem "activesupport", "~> 6.1.0"
7
+ gem "activemodel", "~> 6.1.0"
8
8
 
9
9
  gemspec path: "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "avro-patches", ">= 0.4.1"
5
+ gem "avro-patches", ">= 0.4.1", "< 1.0"
6
6
  gem "activesupport", "~> 5.2.0"
7
7
  gem "activemodel", "~> 5.2.0"
8
8
 
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "avro-patches", ">= 1.0.0"
6
+ gem "activesupport", "~> 6.1.0"
7
+ gem "activemodel", "~> 6.1.0"
8
+
9
+ gemspec path: "../"
@@ -8,7 +8,6 @@ require 'avromatic/model'
8
8
  require 'avromatic/model_registry'
9
9
  require 'avromatic/messaging'
10
10
  require 'active_support/core_ext/string/inflections'
11
- require 'avromatic/patches'
12
11
 
13
12
  module Avromatic
14
13
  class << self
@@ -33,10 +32,6 @@ module Avromatic
33
32
  eager_load_models!
34
33
  end
35
34
 
36
- def self.use_encoding_providers?
37
- use_custom_datum_writer && defined?(Avromatic::Patches::SchemaValidatorPatch)
38
- end
39
-
40
35
  def self.build_schema_registry
41
36
  raise 'Avromatic must be configured with a registry_url' unless registry_url
42
37
  if use_schema_fingerprint_lookup
@@ -1,11 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Avromatic
4
- module IO
5
- UNION_MEMBER_INDEX = '__avromatic_member_index'
6
- ENCODING_PROVIDER = '__avromatic_encoding_provider'
7
- end
8
- end
9
-
10
3
  require 'avromatic/io/datum_reader'
11
4
  require 'avromatic/io/datum_writer'
5
+ require 'avromatic/io/union_datum'
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Style/WhenThen
4
3
  module Avromatic
5
4
  module IO
6
5
  # Subclass DatumReader to include additional information about the union
@@ -8,81 +7,32 @@ module Avromatic
8
7
  # branch 'salsify-master' with the tag 'v1.9.0.3'
9
8
  class DatumReader < Avro::IO::DatumReader
10
9
 
11
- UNION_MEMBER_INDEX = Avromatic::IO::UNION_MEMBER_INDEX
12
-
13
- def read_data(writers_schema, readers_schema, decoder, initial_record = nil)
14
- # schema matching
15
- unless self.class.match_schemas(writers_schema, readers_schema)
16
- raise Avro::IO::SchemaMatchException.new(writers_schema, readers_schema)
17
- end
18
-
10
+ def read_data(writers_schema, readers_schema, decoder)
19
11
  # schema resolution: reader's schema is a union, writer's schema is not
20
- if writers_schema.type_sym != :union && readers_schema.type_sym == :union
21
- rs_index = readers_schema.schemas.find_index do |s|
22
- self.class.match_schemas(writers_schema, s)
23
- end
12
+ return super unless writers_schema.type_sym != :union && readers_schema.type_sym == :union
24
13
 
25
- optional = readers_schema.schemas.first.type_sym == :null
26
- union_info = if readers_schema.schemas.size == 2 && optional
27
- # Avromatic does not treat the union of null and 1 other type as a union
28
- {}
29
- elsif optional
30
- # Avromatic does not treat the null of an optional field as part of the union
31
- { UNION_MEMBER_INDEX => rs_index - 1 }
32
- else
33
- { UNION_MEMBER_INDEX => rs_index }
34
- end
35
-
36
- return read_data(writers_schema, readers_schema.schemas[rs_index], decoder, union_info) if rs_index
37
- raise Avro::IO::SchemaMatchException.new(writers_schema, readers_schema)
14
+ rs_index = readers_schema.schemas.find_index do |s|
15
+ self.class.match_schemas(writers_schema, s)
38
16
  end
39
17
 
40
- # function dispatch for reading data based on type of writer's schema
41
- datum = case writers_schema.type_sym
42
- when :null; decoder.read_null
43
- when :boolean; decoder.read_boolean
44
- when :string; decoder.read_string
45
- when :int; decoder.read_int
46
- when :long; decoder.read_long
47
- when :float; decoder.read_float
48
- when :double; decoder.read_double
49
- when :bytes; decoder.read_bytes
50
- when :fixed; read_fixed(writers_schema, readers_schema, decoder)
51
- when :enum; read_enum(writers_schema, readers_schema, decoder)
52
- when :array; read_array(writers_schema, readers_schema, decoder)
53
- when :map; read_map(writers_schema, readers_schema, decoder)
54
- when :union; read_union(writers_schema, readers_schema, decoder)
55
- when :record, :error, :request; read_record(writers_schema, readers_schema, decoder, initial_record || {})
56
- else
57
- raise Avro::AvroError.new("Cannot read unknown schema type: #{writers_schema.type}")
58
- end
18
+ raise Avro::IO::SchemaMatchException.new(writers_schema, readers_schema) unless rs_index
59
19
 
60
- # Allow this code to be used with an official Avro release or the
61
- # avro-patches gem that includes logical_type support.
62
- if readers_schema.respond_to?(:logical_type)
63
- readers_schema.type_adapter.decode(datum)
64
- else
65
- datum
66
- end
67
- end
20
+ datum = read_data(writers_schema, readers_schema.schemas[rs_index], decoder)
21
+ optional = readers_schema.schemas.first.type_sym == :null
68
22
 
69
- # Override to specify an initial record that may contain union index
70
- def read_record(writers_schema, readers_schema, decoder, initial_record = {})
71
- readers_fields_hash = readers_schema.fields_hash
72
- read_record = Avromatic.use_custom_datum_reader ? initial_record : {}
73
- writers_schema.fields.each do |field|
74
- readers_field = readers_fields_hash[field.name]
75
- if readers_field
76
- field_val = read_data(field.type, readers_field.type, decoder)
77
- read_record[field.name] = field_val
78
- else
79
- skip_data(field.type, decoder)
80
- end
23
+ if readers_schema.schemas.size == 2 && optional
24
+ # Avromatic does not treat the union of null and 1 other type as a union
25
+ datum
26
+ elsif datum.nil?
27
+ # Avromatic does not treat the null of an optional field as part of the union
28
+ nil
29
+ else
30
+ # Avromatic does not treat the null of an optional field as part of the union so
31
+ # adjust the member index accordingly
32
+ member_index = optional ? rs_index - 1 : rs_index
33
+ Avromatic::IO::UnionDatum.new(member_index, datum)
81
34
  end
82
-
83
- read_record
84
35
  end
85
36
  end
86
37
  end
87
38
  end
88
- # rubocop:enable Style/WhenThen