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 +4 -4
- data/CHANGELOG.md +1 -0
- data/lib/avromatic/model/attribute/record.rb +27 -0
- data/lib/avromatic/model/attributes.rb +6 -1
- data/lib/avromatic/model/builder.rb +1 -0
- data/lib/avromatic/model/raw_serialization.rb +19 -21
- data/lib/avromatic/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8850a5d6dce59c5dd91c2476b3b541ca6d258b94
|
4
|
+
data.tar.gz: eb4d4df178cf748408d7346251c7b0989eab7d8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7feeb97078619ec38b8092fde8f01d5f9f7e209f7040b9dd6252cc7766f7ac700da35681c9a95ca4ce1d88e6477f68e1bf41c09414fff3cd854e97c9cdb3707b
|
7
|
+
data.tar.gz: 3d2f3e5b0da52a97fcb5c555cda2eeda65767b4840e2ade715449e7a48df57c42a770d1ac8a56aadb45be8924e50aa060534e1c342f0bbc44c04c077573a3b29
|
data/CHANGELOG.md
CHANGED
@@ -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] =
|
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
|
|
data/lib/avromatic/version.rb
CHANGED
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.
|
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-
|
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
|