avro-builder 0.12.0 → 0.13.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: 21b042e8f1af75aa7438eca4e949b32a50d691a1
4
- data.tar.gz: eedcd4cb2b91b9336ad1688bb3c6bdbb3dec079e
3
+ metadata.gz: 8672dba84b4ee61d299c99f3fe0ced401b3817b4
4
+ data.tar.gz: dcdcf44d8c0f11c3b2d9b828738047a037df0caa
5
5
  SHA512:
6
- metadata.gz: f12ec9cd64725bae339a186f7db26f1c6b16a1c59cdc13d935bde7d62e9195bc2c976e7840e307432a3551ff59ea48d8566c990a882026c4f1970f8419bd250f
7
- data.tar.gz: 48a859dbb4d78c94db00220ecedb119e564cac42f16031b258accbc4619844ff6d92c87d8d0f2f28232d67b310a750835074b62afece2fd940af59467991f849
6
+ metadata.gz: e3f8f47f84aa98101454de16745b084e07f3342f0c97f69597cf378ef0906b1deeafe730a96562e478daac34b0f78f1ad47565eded84c2bc3dafaa72c6223408
7
+ data.tar.gz: 3116b64339e0afb980ced2790f2e71e626983fc976a1b519edf5656e4dabb09b2175dc9545bdba7942df2223920d78813cb10cb5688bc7f7880f6c5a51498adc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # avro-builder changelog
2
2
 
3
+ ## v0.13.0
4
+ - Add support for type macros.
5
+
3
6
  ## v0.12.0
4
7
  - Allow methods for complex and primitive types to be used at the top-level and
5
8
  anywhere that a type name is accepted.
data/README.md CHANGED
@@ -316,6 +316,29 @@ record :with_date_array
316
316
  end
317
317
  ```
318
318
 
319
+ ### Type Macros
320
+
321
+ `avro-builder` allows type macros to be defined that expand to types that
322
+ cannot normally be named in Avro schemas. These macro names are not retained
323
+ in generated schemas but allow definitions to be reused across DSL files:
324
+
325
+ ```ruby
326
+ type_macro :timestamp, long(logical_type: 'timestamp-millis')
327
+
328
+ record :user do
329
+ required :created_at, :timestamp
330
+ required :updated_at, :timestamp
331
+ end
332
+ ```
333
+
334
+ Type macros inherit the namespace from the context where they are defined or
335
+ an explicit namespace option may be specified:
336
+
337
+ ```ruby
338
+ type_macro :timestamp, long(logical_type: 'timestamp-millis'),
339
+ namespace: 'com.my_company'
340
+ ```
341
+
319
342
  ### Auto-loading and Imports
320
343
 
321
344
  Specify paths to search for definitions:
@@ -32,26 +32,34 @@ module Avro
32
32
  object || lookup_named_type(key, nil)
33
33
  end
34
34
 
35
+ # Add a type object directly with the specified name.
36
+ # The type_object may not have a name or namespace.
37
+ def add_type_by_name(type_object, name, namespace = nil)
38
+ fullname = Avro::Name.make_fullname(name.to_s, namespace && namespace.to_s)
39
+ name = fullname.split('.').last
40
+
41
+ store_by_name(type_object, name)
42
+ store_by_fullname(type_object, fullname) if name != fullname
43
+ end
44
+
35
45
  private
36
46
 
37
47
  # Schemas are stored by name, provided that the name is unique.
38
48
  # If the unqualified name is ambiguous then it is removed from the cache.
39
49
  # A set of unqualified names is kept to avoid reloading files for
40
50
  # ambiguous references.
41
- def store_by_name(object)
42
- key = object.name.to_s
43
- if schema_objects.key?(key)
44
- schema_objects.delete(key)
45
- elsif !schema_names.include?(key)
46
- schema_objects.store(key, object)
51
+ def store_by_name(object, name = object.name.to_s)
52
+ if schema_objects.key?(name)
53
+ schema_objects.delete(name)
54
+ elsif !schema_names.include?(name)
55
+ schema_objects.store(name, object)
47
56
  end
48
- schema_names.add(key)
57
+ schema_names.add(name)
49
58
  end
50
59
 
51
- def store_by_fullname(object)
52
- key = object.fullname
53
- raise DuplicateDefinitionError.new(key, object, schema_objects[key]) if schema_objects.key?(key)
54
- schema_objects.store(key, object)
60
+ def store_by_fullname(object, fullname = object.fullname)
61
+ raise DuplicateDefinitionError.new(fullname, object, schema_objects[fullname]) if schema_objects.key?(fullname)
62
+ schema_objects.store(fullname, object)
55
63
  end
56
64
 
57
65
  attr_reader :schema_objects, :schema_names, :builder
@@ -89,6 +89,12 @@ module Avro
89
89
  @last_object = super
90
90
  end
91
91
 
92
+ def type_macro(name, type_object, options = {})
93
+ raise "#{type_object.inspect} must be a type object" unless type_object.is_a?(Types::Type)
94
+ raise "namespace cannot be included in name: #{name}" if name.to_s.index('.')
95
+ cache.add_type_by_name(type_object, name, options[:namespace] || namespace)
96
+ end
97
+
92
98
  private
93
99
 
94
100
  def cache
@@ -1,5 +1,5 @@
1
1
  module Avro
2
2
  module Builder
3
- VERSION = '0.12.0'.freeze
3
+ VERSION = '0.13.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.12.0
4
+ version: 0.13.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-09-14 00:00:00.000000000 Z
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro