avromatic 2.2.4 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cc3e17c99ffc3587dc747dbc97da2b63304e20e2bbc7f335a00878ae87bbcf2
4
- data.tar.gz: 47e581e34bf75f8ea6fd6775ab91c67c51566475e0d831452bd6ef922f185151
3
+ metadata.gz: bdd13d41971f1b38c3b1a9496ec698a14122b69ed5442b4cca7ac7e7d099c6b2
4
+ data.tar.gz: af4fbf880781abeffc7e0807491ca43a0fdabdff5714c60bfe46b365caa355a5
5
5
  SHA512:
6
- metadata.gz: 29d285151df19f4a3611889c2b805a3de2018c08120a6fca53a88234ab5028b63f83c0f499118a56ca23709071a8ef811713575e6d621c74c42ff6936cd038af
7
- data.tar.gz: 97bb648dd3e554219b5ecf81611c1dbc482467fabc50c1f219c0d2b52e31eb891903be92cb20b472f3c9e4ac07d2507d59a2b16dacbec374d22a712b19c4c50c
6
+ metadata.gz: 856111e8a1f7e5ec99cbbe20877095ff630182f27265617e7746437402e4146628c5ae2812f85a9f0cd5c234b748bd1b1c8abee8fddb92e21bc137537074a0d4
7
+ data.tar.gz: 275aa990b3a84aaa679141955a93800cb4764fc0bbf44a290cbaebd4d54e1cd443f2a3943431b2781bcc6165df06861c7a0d6f9c6bf4c754732737518935d896
@@ -1,5 +1,8 @@
1
1
  # avromatic changelog
2
2
 
3
+ ## v2.2.5
4
+ - Optimize memory usage when serializing, deserializing and instantiating models.
5
+
3
6
  ## v2.2.4
4
7
  - Compatibility with Avro v1.10.x.
5
8
 
data/README.md CHANGED
@@ -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:
@@ -25,7 +25,7 @@ module Avromatic
25
25
  optional = readers_schema.schemas.first.type_sym == :null
26
26
  union_info = if readers_schema.schemas.size == 2 && optional
27
27
  # Avromatic does not treat the union of null and 1 other type as a union
28
- {}
28
+ nil
29
29
  elsif optional
30
30
  # Avromatic does not treat the null of an optional field as part of the union
31
31
  { UNION_MEMBER_INDEX => rs_index - 1 }
@@ -5,6 +5,10 @@ module Avromatic
5
5
  # Subclass DatumWriter to use additional information about union member
6
6
  # index.
7
7
  class DatumWriter < Avro::IO::DatumWriter
8
+ class << self
9
+ attr_accessor :optimize
10
+ end
11
+
8
12
  def write_union(writers_schema, datum, encoder)
9
13
  optional = writers_schema.schemas.first.type_sym == :null
10
14
  if datum.is_a?(Hash) && datum.key?(Avromatic::IO::UNION_MEMBER_INDEX)
@@ -14,6 +18,8 @@ module Avromatic
14
18
  elsif optional && writers_schema.schemas.size == 2
15
19
  # Optimize for the common case of a union that's just an optional field
16
20
  index_of_schema = datum.nil? ? 0 : 1
21
+ elsif self.class.optimize && optional && datum.nil?
22
+ index_of_schema = 0
17
23
  else
18
24
  index_of_schema = writers_schema.schemas.find_index do |schema|
19
25
  Avro::Schema.validate(schema, datum)
@@ -74,23 +74,23 @@ module Avromatic
74
74
  def initialize(data = {})
75
75
  super()
76
76
 
77
- valid_keys = []
77
+ num_valid_keys = 0
78
78
  attribute_definitions.each do |attribute_name, attribute_definition|
79
79
  if data.include?(attribute_name)
80
- valid_keys << attribute_name
80
+ num_valid_keys += 1
81
81
  value = data.fetch(attribute_name)
82
82
  send(attribute_definition.setter_name, value)
83
83
  elsif data.include?(attribute_definition.name_string)
84
- valid_keys << attribute_name
84
+ num_valid_keys += 1
85
85
  value = data[attribute_definition.name_string]
86
86
  send(attribute_definition.setter_name, value)
87
- elsif !attributes.include?(attribute_name)
87
+ elsif !_attributes.include?(attribute_name)
88
88
  send(attribute_definition.setter_name, attribute_definition.default)
89
89
  end
90
90
  end
91
91
 
92
- unless Avromatic.allow_unknown_attributes || valid_keys.size == data.size
93
- unknown_attributes = (data.keys.map(&:to_s) - valid_keys.map(&:to_s)).sort
92
+ unless Avromatic.allow_unknown_attributes || num_valid_keys == data.size
93
+ unknown_attributes = (data.keys.map(&:to_s) - _attributes.keys.map(&:to_s)).sort
94
94
  allowed_attributes = attribute_definitions.keys.map(&:to_s).sort
95
95
  message = "Unexpected arguments for #{self.class.name}#initialize: #{unknown_attributes.join(', ')}. " \
96
96
  "Only the following arguments are allowed: #{allowed_attributes.join(', ')}. Provided arguments: #{data.inspect}"
@@ -55,8 +55,11 @@ module Avromatic
55
55
 
56
56
  def avro_hash(fields, strict: false, validate:)
57
57
  avro_validate! if validate
58
- attributes.slice(*fields).each_with_object(Hash.new) do |(key, value), result|
59
- result[key.to_s] = attribute_definitions[key].serialize(value, strict: strict)
58
+ fields.each_with_object(Hash.new) do |field, result|
59
+ next unless _attributes.include?(field)
60
+
61
+ value = _attributes[field]
62
+ result[field.to_s] = attribute_definitions[field].serialize(value, strict)
60
63
  end
61
64
  end
62
65
 
@@ -38,7 +38,7 @@ module Avromatic
38
38
  value.is_a?(::Time) && value.class != ActiveSupport::TimeWithZone && truncated?(value)
39
39
  end
40
40
 
41
- def serialize(value, **)
41
+ def serialize(value, _strict)
42
42
  value
43
43
  end
44
44
 
@@ -31,7 +31,9 @@ module Avromatic
31
31
  raise "#{__method__} must be overridden by #{self.class.name}"
32
32
  end
33
33
 
34
- def serialize(_value, **)
34
+ # Note we use positional args rather than keyword args to reduce
35
+ # memory allocations
36
+ def serialize(_value, _strict)
35
37
  raise "#{__method__} must be overridden by #{self.class.name}"
36
38
  end
37
39
 
@@ -40,11 +40,11 @@ module Avromatic
40
40
  value.nil? || (value.is_a?(::Array) && value.all? { |element| value_type.coerced?(element) })
41
41
  end
42
42
 
43
- def serialize(value, strict:)
43
+ def serialize(value, strict)
44
44
  if value.nil?
45
45
  value
46
46
  else
47
- value.map { |element| value_type.serialize(element, strict: strict) }
47
+ value.map { |element| value_type.serialize(element, strict) }
48
48
  end
49
49
  end
50
50
 
@@ -30,7 +30,7 @@ module Avromatic
30
30
 
31
31
  alias_method :coerced?, :coercible?
32
32
 
33
- def serialize(value, **)
33
+ def serialize(value, _strict)
34
34
  value
35
35
  end
36
36
 
@@ -55,7 +55,7 @@ module Avromatic
55
55
  false
56
56
  end
57
57
 
58
- def serialize(value, **)
58
+ def serialize(value, _strict)
59
59
  @serializer.call(value)
60
60
  end
61
61
 
@@ -32,7 +32,7 @@ module Avromatic
32
32
 
33
33
  alias_method :coerced?, :coercible?
34
34
 
35
- def serialize(value, **)
35
+ def serialize(value, _strict)
36
36
  value
37
37
  end
38
38
 
@@ -47,7 +47,7 @@ module Avromatic
47
47
  (input.is_a?(::Symbol) && allowed_values.include?(input.to_s))
48
48
  end
49
49
 
50
- def serialize(value, **)
50
+ def serialize(value, _strict)
51
51
  value
52
52
  end
53
53
 
@@ -36,7 +36,7 @@ module Avromatic
36
36
 
37
37
  alias_method :coerced?, :coercible?
38
38
 
39
- def serialize(value, **)
39
+ def serialize(value, _strict)
40
40
  value
41
41
  end
42
42
 
@@ -39,7 +39,7 @@ module Avromatic
39
39
  input.nil? || input.is_a?(::Float)
40
40
  end
41
41
 
42
- def serialize(value, **)
42
+ def serialize(value, _strict)
43
43
  value
44
44
  end
45
45
 
@@ -30,7 +30,7 @@ module Avromatic
30
30
 
31
31
  alias_method :coerced?, :coercible?
32
32
 
33
- def serialize(value, **)
33
+ def serialize(value, _strict)
34
34
  value
35
35
  end
36
36
 
@@ -59,12 +59,12 @@ module Avromatic
59
59
  end
60
60
  end
61
61
 
62
- def serialize(value, strict:)
62
+ def serialize(value, strict)
63
63
  if value.nil?
64
64
  value
65
65
  else
66
66
  value.each_with_object({}) do |(element_key, element_value), result|
67
- result[key_type.serialize(element_key, strict: strict)] = value_type.serialize(element_value, strict: strict)
67
+ result[key_type.serialize(element_key, strict)] = value_type.serialize(element_value, strict)
68
68
  end
69
69
  end
70
70
  end
@@ -30,7 +30,7 @@ module Avromatic
30
30
 
31
31
  alias_method :coerced?, :coercible?
32
32
 
33
- def serialize(_value, **)
33
+ def serialize(_value, _strict)
34
34
  nil
35
35
  end
36
36
 
@@ -39,7 +39,7 @@ module Avromatic
39
39
  value.nil? || value.is_a?(record_class)
40
40
  end
41
41
 
42
- def serialize(value, strict:)
42
+ def serialize(value, strict)
43
43
  if value.nil?
44
44
  value
45
45
  elsif !strict && Avromatic.use_custom_datum_writer && Avromatic.use_encoding_providers? && !record_class.config.mutable
@@ -39,7 +39,7 @@ module Avromatic
39
39
  value.nil? || value.is_a?(::String)
40
40
  end
41
41
 
42
- def serialize(value, **)
42
+ def serialize(value, _strict)
43
43
  value
44
44
  end
45
45
 
@@ -51,7 +51,7 @@ module Avromatic
51
51
  end
52
52
  end
53
53
 
54
- def serialize(value, strict:)
54
+ def serialize(value, strict)
55
55
  # Avromatic does not treat the null of an optional field as part of the union
56
56
  return nil if value.nil?
57
57
 
@@ -60,7 +60,7 @@ module Avromatic
60
60
  raise ArgumentError.new("Expected #{value.inspect} to be one of #{value_classes.map(&:name)}")
61
61
  end
62
62
 
63
- hash = member_types[member_index].serialize(value, strict: strict)
63
+ hash = member_types[member_index].serialize(value, strict)
64
64
  if !strict && Avromatic.use_custom_datum_writer && value.is_a?(Avromatic::Model::Attributes)
65
65
  hash[Avromatic::IO::UNION_MEMBER_INDEX] = member_index
66
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Avromatic
4
- VERSION = '2.2.4'
4
+ VERSION = '2.2.5'
5
5
  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: 2.2.4
4
+ version: 2.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify Engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-30 00:00:00.000000000 Z
11
+ date: 2020-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -349,8 +349,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
349
349
  - !ruby/object:Gem::Version
350
350
  version: '0'
351
351
  requirements: []
352
- rubyforge_project:
353
- rubygems_version: 2.7.6.2
352
+ rubygems_version: 3.0.8
354
353
  signing_key:
355
354
  specification_version: 4
356
355
  summary: Generate Ruby models from Avro schemas