avro-builder 0.11.0 → 0.12.0

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: 927dca1e52261de526556bb1a692ac90406d10bf
4
- data.tar.gz: 89e990b81db93413c7f055c1ec95eb23a237b8f3
3
+ metadata.gz: 21b042e8f1af75aa7438eca4e949b32a50d691a1
4
+ data.tar.gz: eedcd4cb2b91b9336ad1688bb3c6bdbb3dec079e
5
5
  SHA512:
6
- metadata.gz: c69e346811dfbd2574602bd6724b83dc2421a822123c5973b8b6def6b8b9aa552231af78c7778cf2feaac9054ea72145263f2ce41ce5b43eb7351f099dfb480e
7
- data.tar.gz: aeb7b896dbe7146a7b4e8d0c4e7eb5533f48c7e15179ff3899ef74f733a91230ec4b9693268bad38792cca40739e7a68265abfcf91cae7c033ebda99d5d51f69
6
+ metadata.gz: f12ec9cd64725bae339a186f7db26f1c6b16a1c59cdc13d935bde7d62e9195bc2c976e7840e307432a3551ff59ea48d8566c990a882026c4f1970f8419bd250f
7
+ data.tar.gz: 48a859dbb4d78c94db00220ecedb119e564cac42f16031b258accbc4619844ff6d92c87d8d0f2f28232d67b310a750835074b62afece2fd940af59467991f849
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # avro-builder changelog
2
2
 
3
+ ## v0.12.0
4
+ - Allow methods for complex and primitive types to be used at the top-level and
5
+ anywhere that a type name is accepted.
6
+
3
7
  ## v0.11.0
4
8
  - Add support for generating schemas that contain logical types. The official
5
9
  Ruby `avro` gem does not yet support logical types. The `avro-salsify-fork` gem
data/README.md CHANGED
@@ -185,6 +185,16 @@ record :complex_types do
185
185
  end
186
186
  ```
187
187
 
188
+ Methods may also be used for complex types instead of separately specifying the
189
+ type name and options:
190
+
191
+ ```ruby
192
+ record :complex_types do
193
+ required :array_of_unions, array(union(:int, :string))
194
+ required :array_or_map, union(array(:int), map(:int))
195
+ end
196
+ ```
197
+
188
198
  For more on unions see [below](#unions).
189
199
 
190
200
  ### Nested Records
@@ -254,6 +264,16 @@ end
254
264
  For an optional union, `null` is automatically added as the first type for
255
265
  the union and the field defaults to `null`.
256
266
 
267
+ Unions may also be defined using the `union` method instead of specifying the
268
+ `:union` type and member types separately:
269
+
270
+ ```ruby
271
+ record :my_record_with_unions do
272
+ required :req_union, union(:string, :int)
273
+ optional :opt_union, union(:float, :long)
274
+ end
275
+ ```
276
+
257
277
  ### Logical Types
258
278
 
259
279
  The DSL supports setting a logical type on any type except a union. The logical
@@ -22,7 +22,7 @@ module Avro
22
22
  include Avro::Builder::DslOptions
23
23
  include Avro::Builder::DslAttributes
24
24
  include Avro::Builder::FileHandler
25
- include Avro::Builder::TypeFactory
25
+ include Avro::Builder::AnonymousTypes
26
26
 
27
27
  dsl_attribute :namespace
28
28
 
@@ -83,6 +83,12 @@ module Avro
83
83
  Avro::Schema.parse(to_json(validate: false))
84
84
  end
85
85
 
86
+ # Override the type method from AnonymousTypes to store a reference to the
87
+ # last type defined.
88
+ def type(*)
89
+ @last_object = super
90
+ end
91
+
86
92
  private
87
93
 
88
94
  def cache
@@ -17,7 +17,7 @@ module Avro
17
17
  # These attributes may be set as options or via a block in the DSL
18
18
  dsl_attributes :doc, :default, :order
19
19
 
20
- def initialize(name:, avro_type_name:, record:, cache:, internal: {}, options: {}, &block)
20
+ def initialize(name:, avro_type_or_name:, record:, cache:, internal: {}, options: {}, &block)
21
21
  @cache = cache
22
22
  @record = record
23
23
  @name = name.to_s
@@ -31,19 +31,16 @@ module Avro
31
31
  send(key, type_options.delete(key)) if dsl_attribute?(key)
32
32
  end
33
33
 
34
- @field_type = if builtin_type?(avro_type_name)
35
- create_and_configure_builtin_type(avro_type_name,
36
- field: self,
37
- cache: cache,
38
- internal: internal,
39
- validate_type: false,
40
- options: type_options)
41
- elsif avro_type_name.is_a?(Avro::Builder::Types::Type)
42
- raise 'Type name must be an Avro builtin type '\
43
- "or a previously defined type name. Got #{avro_type_name}"
44
- else
45
- cache.lookup_named_type(avro_type_name, namespace)
46
- end
34
+ # Find existing Type or build a new instance of a builtin Type using
35
+ # the supplied block
36
+ @field_type = type_lookup(avro_type_or_name, namespace) do |avro_type_name|
37
+ create_and_configure_builtin_type(avro_type_name,
38
+ field: self,
39
+ cache: cache,
40
+ internal: internal,
41
+ validate_type: false,
42
+ options: type_options)
43
+ end
47
44
 
48
45
  # DSL calls must be evaluated after the type has been constructed
49
46
  instance_eval(&block) if block_given?
@@ -10,6 +10,21 @@ module Avro
10
10
 
11
11
  private
12
12
 
13
+ # Determine if avro_type_or_name is an existing Type, the name of a builtin
14
+ # type or a previously defined named type.
15
+ # If avro_type_or_name is the name of a builtin type, then that type name
16
+ # is yielded to build the type using a block provided by the caller using,
17
+ # for example, create_builtin_type or create_and_configure_builtin_type.
18
+ def type_lookup(avro_type_or_name, namespace = nil)
19
+ if avro_type_or_name.is_a?(Avro::Builder::Types::Type)
20
+ avro_type_or_name
21
+ elsif builtin_type?(avro_type_or_name)
22
+ yield(avro_type_or_name)
23
+ else
24
+ cache.lookup_named_type(avro_type_or_name, namespace)
25
+ end
26
+ end
27
+
13
28
  # Return a new Type instance
14
29
  def create_builtin_type(avro_type_name, field:, cache:)
15
30
  name = avro_type_name.to_s.downcase
@@ -7,7 +7,7 @@ module Avro
7
7
 
8
8
  dsl_attribute :items do |items_type = nil|
9
9
  if items_type
10
- @items = create_builtin_or_lookup_named_type(items_type)
10
+ @items = create_builtin_or_lookup_type(items_type)
11
11
  else
12
12
  @items
13
13
  end
@@ -26,6 +26,10 @@ module Avro
26
26
  }.merge(overrides).reject { |_, v| v.nil? }
27
27
  end
28
28
 
29
+ def to_h(reference_state)
30
+ serialize(reference_state)
31
+ end
32
+
29
33
  module ClassMethods
30
34
 
31
35
  # Infer avro_type_name based on class
@@ -7,7 +7,7 @@ module Avro
7
7
 
8
8
  dsl_attribute :values do |value_type = nil|
9
9
  if value_type
10
- @values = create_builtin_or_lookup_named_type(value_type)
10
+ @values = create_builtin_or_lookup_type(value_type)
11
11
  else
12
12
  @values
13
13
  end
@@ -26,9 +26,9 @@ module Avro
26
26
  end
27
27
 
28
28
  # Add a required field to the record
29
- def required(name, avro_type_name, options = {}, &block)
29
+ def required(name, avro_type_or_name, options = {}, &block)
30
30
  new_field = Avro::Builder::Field.new(name: name,
31
- avro_type_name: avro_type_name,
31
+ avro_type_or_name: avro_type_or_name,
32
32
  record: self,
33
33
  cache: cache,
34
34
  internal: { type_namespace: namespace },
@@ -39,9 +39,9 @@ module Avro
39
39
 
40
40
  # Add an optional field to the record. In Avro this is represented
41
41
  # as a union of null and the type specified here.
42
- def optional(name, avro_type_name, options = {}, &block)
42
+ def optional(name, avro_type_or_name, options = {}, &block)
43
43
  new_field = Avro::Builder::Field.new(name: name,
44
- avro_type_name: avro_type_name,
44
+ avro_type_or_name: avro_type_or_name,
45
45
  record: self,
46
46
  cache: cache,
47
47
  internal: { type_namespace: namespace,
@@ -26,6 +26,11 @@ module Avro
26
26
  end
27
27
  end
28
28
 
29
+ def to_h(_reference_state)
30
+ { type: avro_type_name, logicalType: logical_type }
31
+ .reject { |_, v| v.nil? }
32
+ end
33
+
29
34
  def namespace
30
35
  nil
31
36
  end
@@ -8,13 +8,13 @@ module Avro
8
8
  module TypeReferencer
9
9
  include Avro::Builder::TypeFactory
10
10
 
11
- def create_builtin_or_lookup_named_type(avro_type_or_name)
12
- if avro_type_or_name.is_a?(Avro::Builder::Types::Type)
13
- avro_type_or_name
14
- elsif builtin_type?(avro_type_or_name)
15
- create_builtin_type(avro_type_or_name, field: field, cache: cache)
16
- else
17
- cache.lookup_named_type(avro_type_or_name)
11
+ private
12
+
13
+ def create_builtin_or_lookup_type(avro_type_or_name)
14
+ # Find existing Type or build a new instance of a builtin Type using
15
+ # the supplied block
16
+ type_lookup(avro_type_or_name) do |avro_type_name|
17
+ create_builtin_type(avro_type_name, field: field, cache: cache)
18
18
  end
19
19
  end
20
20
  end
@@ -9,7 +9,7 @@ module Avro
9
9
 
10
10
  dsl_attribute :types do |*types|
11
11
  if !types.empty?
12
- @types = types.flatten.map { |type| create_builtin_or_lookup_named_type(type) }
12
+ @types = types.flatten.map { |type| create_builtin_or_lookup_type(type) }
13
13
  else
14
14
  @types
15
15
  end
@@ -1,5 +1,5 @@
1
1
  module Avro
2
2
  module Builder
3
- VERSION = '0.11.0'.freeze
3
+ VERSION = '0.12.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avro-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
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-08-26 00:00:00.000000000 Z
11
+ date: 2016-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro