avromatic 0.9.0.rc0 → 0.9.0.rc1

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: 1a36670183dc6e3348395dcc336222da9ac1bd00
4
- data.tar.gz: b37d41366b6737fb4aa7d4793128c13d90860920
3
+ metadata.gz: 8850a5d6dce59c5dd91c2476b3b541ca6d258b94
4
+ data.tar.gz: eb4d4df178cf748408d7346251c7b0989eab7d8d
5
5
  SHA512:
6
- metadata.gz: d316d600c9e38743538abc7b1a598cb13cfa102c6181476ed7e2684ccf63000a6856998ebc9ad065bfb71c98a54adab72a8675aa06f67622f5d2eb415acfea73
7
- data.tar.gz: 86ac869dd270188af7f24f22ce1259ed2cf0b109e09d23ff3da9af4288deb191ab1fc7a05f4c2d81159335216b07e3e63e8618c86d3e0c4b09055246881b830f
6
+ metadata.gz: 7feeb97078619ec38b8092fde8f01d5f9f7e209f7040b9dd6252cc7766f7ac700da35681c9a95ca4ce1d88e6477f68e1bf41c09414fff3cd854e97c9cdb3707b
7
+ data.tar.gz: 3d2f3e5b0da52a97fcb5c555cda2eeda65767b4840e2ade715449e7a48df57c42a770d1ac8a56aadb45be8924e50aa060534e1c342f0bbc44c04c077573a3b29
data/CHANGELOG.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## v0.9.0 (unreleased)
4
4
  - Add support for more than one non-null type in a union.
5
+ - Fix the serialization of nested complex types.
5
6
 
6
7
  ## v0.8.0
7
8
  - Add support for logical types. Currently this requires using the
@@ -0,0 +1,27 @@
1
+ module Avromatic
2
+ module Model
3
+ module Attribute
4
+
5
+ # This subclass of Virtus::Attribute is defined to ensure that Avromatic
6
+ # generated models (identified by their inclusion of
7
+ # Avromatic::Model::Attributes) are always coerced by identifying an
8
+ # instance of the model or creating a new one.
9
+ # This is required to coerce models correctly with nested complex types
10
+ # with Virtus.
11
+ class Record < Virtus::Attribute
12
+ primitive Avromatic::Model::Attributes
13
+
14
+ def coerce(value)
15
+ return value if value.nil? || value_coerced?(value)
16
+
17
+ coerced = primitive.new(value)
18
+ coerced if coerced.valid?
19
+ end
20
+
21
+ def value_coerced?(value)
22
+ value.is_a?(primitive)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -128,7 +128,12 @@ module Avromatic
128
128
  when :record
129
129
  # TODO: This should add the generated model to a module.
130
130
  # A hash of generated models should be kept by name for reuse.
131
- Avromatic::Model.model(schema: field_type)
131
+ Avromatic::Model.model(schema: field_type).tap do |record_class|
132
+ # Register the generated model with Axiom to prevent it being
133
+ # treated as a BasicObject.
134
+ # See https://github.com/solnic/virtus/issues/284#issuecomment-56405137
135
+ Axiom::Types::Object.new { primitive(record_class) }
136
+ end
132
137
  else
133
138
  raise "Unsupported type #{field_type}"
134
139
  end
@@ -6,6 +6,7 @@ require 'avromatic/model/value_object'
6
6
  require 'avromatic/model/configurable'
7
7
  require 'avromatic/model/attribute/union'
8
8
  require 'avromatic/model/attributes'
9
+ require 'avromatic/model/attribute/record'
9
10
  require 'avromatic/model/raw_serialization'
10
11
  require 'avromatic/model/messaging_serialization'
11
12
 
@@ -9,10 +9,28 @@ module Avromatic
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  module Encode
12
+ extend ActiveSupport::Concern
13
+
12
14
  delegate :avro_serializer, :datum_writer, :datum_reader, :attribute_set,
13
15
  to: :class
14
16
  private :avro_serializer, :datum_writer, :datum_reader
15
17
 
18
+ module ClassMethods
19
+ def recursive_serialize(value, attribute_name = nil)
20
+ if value.is_a?(Avromatic::Model::Attributes)
21
+ value.value_attributes_for_avro
22
+ elsif value.is_a?(Array)
23
+ value.map { |v| recursive_serialize(v) }
24
+ elsif value.is_a?(Hash)
25
+ value.each_with_object({}) do |(k, v), hash|
26
+ hash[k] = recursive_serialize(v)
27
+ end
28
+ else
29
+ avro_serializer[attribute_name].call(value)
30
+ end
31
+ end
32
+ end
33
+
16
34
  def avro_raw_value
17
35
  avro_raw_encode(value_attributes_for_avro, :value)
18
36
  end
@@ -32,29 +50,9 @@ module Avromatic
32
50
  avro_hash(key_avro_field_names)
33
51
  end
34
52
 
35
- def array_of_models?(key)
36
- attribute_set[key].is_a?(Virtus::Attribute::Collection) &&
37
- attribute_set[key].member_type.primitive.include?(Avromatic::Model::Attributes)
38
- end
39
-
40
- def hash_of_models?(key)
41
- attribute_set[key].is_a?(Virtus::Attribute::Hash) &&
42
- attribute_set[key].value_type.primitive.include?(Avromatic::Model::Attributes)
43
- end
44
-
45
53
  def avro_hash(fields)
46
54
  attributes.slice(*fields).each_with_object(Hash.new) do |(key, value), result|
47
- result[key.to_s] = if value.is_a?(Avromatic::Model::Attributes)
48
- value.value_attributes_for_avro
49
- elsif array_of_models?(key)
50
- value.map(&:value_attributes_for_avro)
51
- elsif hash_of_models?(key)
52
- value.each_with_object({}) do |(k, v), hash|
53
- hash[k] = v.value_attributes_for_avro
54
- end
55
- else
56
- avro_serializer[key].call(value)
57
- end
55
+ result[key.to_s] = self.class.recursive_serialize(value, key)
58
56
  end
59
57
  end
60
58
 
@@ -1,3 +1,3 @@
1
1
  module Avromatic
2
- VERSION = '0.9.0.rc0'.freeze
2
+ VERSION = '0.9.0.rc1'.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.9.0.rc0
4
+ version: 0.9.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify Engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-01 00:00:00.000000000 Z
11
+ date: 2016-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -246,6 +246,7 @@ files:
246
246
  - gemfiles/rails4_2.gemfile
247
247
  - lib/avromatic.rb
248
248
  - lib/avromatic/model.rb
249
+ - lib/avromatic/model/attribute/record.rb
249
250
  - lib/avromatic/model/attribute/union.rb
250
251
  - lib/avromatic/model/attribute_type/union.rb
251
252
  - lib/avromatic/model/attributes.rb