avro-builder 0.1.0 → 0.2.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: fc0e206e5b0cfe57f356bcc17de31819fc2db7ea
4
- data.tar.gz: c7ebe9cd48e6ea8a55762d71d1349405e78d022a
3
+ metadata.gz: a72124e0979079150b62e4ca3f8f6c727b7bb39c
4
+ data.tar.gz: 158fc532ae1587737c415069bdcbf3ae136deeaa
5
5
  SHA512:
6
- metadata.gz: b02e6bf7877bb667d2602c871aceda94b5943adbbe356ca80b7479044dab2dbdaa05d130d9f4732dee2314aa87484159195a845c1af354db0855105259fe2e06
7
- data.tar.gz: b22b39dae5253d9b021fe4deabd8b50ef3757dff483cf0a10ef28612045c32f8ed149db761b68427eb191c8df7751bdabf5e8e98dce88e0b2ccac5180ad9d2a4
6
+ metadata.gz: 7f40595449fa8ce0db3ef04d35d96bd3dd133484a383ff1873bdb3c5f1061124c5398b17d31f881e479df603f940c7b3068e9112dbf2156b1a8659e537669b09
7
+ data.tar.gz: 7d363c07489a17ca4142c34e8a7928f5f737fac535553326d6a2c63007d1c3d0f3b6c6034f1879c9eded1281a2d08d5a51cbac3ef0a7057978edf847ba8ef340
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.3
3
+ - 2.2.4
4
+ - 2.3.0
4
5
  before_install: gem install bundler -v 1.11.2
@@ -1,4 +1,9 @@
1
1
  # avro-builder changelog
2
2
 
3
+ ## v0.2.0
4
+ - Add support for `:union` type.
5
+ - Make `fixed` syntax more flexible. Both `fixed :f, 7` and `fixed :f, size: 7`
6
+ are now supported and equivalent.
7
+
3
8
  ## v0.1.0
4
9
  - Initial release
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Avro::Builder
2
2
 
3
+ [![Build Status](https://travis-ci.org/salsify/avro-builder.svg?branch=master)][travis]
4
+
5
+ [travis]: http://travis-ci.org/salsify/avro-builder
6
+
3
7
  `Avro::Builder` provides a Ruby DSL to create [Apache Avro](https://avro.apache.org/docs/current/) Schemas.
4
8
 
5
9
  This DSL was created because:
@@ -112,7 +116,7 @@ This generates the following Avro JSON schema:
112
116
 
113
117
  ### Required and Optional
114
118
 
115
- Fields for a record a specified as `required` or `optional`. Optional fields are
119
+ Fields for a record are specified as `required` or `optional`. Optional fields are
116
120
  implemented as a union in Avro, where `null` is the first type in the union.
117
121
 
118
122
  ### Named Types
@@ -120,6 +124,38 @@ implemented as a union in Avro, where `null` is the first type in the union.
120
124
  `fixed` and `enum` fields may be specified inline as part of a record
121
125
  or as standalone named types.
122
126
 
127
+ ```ruby
128
+ # Either syntax is supported for specifying the size
129
+ fixed :f, 4
130
+ fixed :g, size: 8
131
+
132
+ # Either syntax is supported for specifying symbols
133
+ enum :e, :X, :Y, :Z
134
+ enum :d, symbols: [:A, :B]
135
+
136
+ record :my_record_with_named do
137
+ required :f_ref, :f
138
+ required :fixed_inline, :fixed, size: 9
139
+ required :e_ref, :e
140
+ required :enum_inline, :enum, symbols: [:P, :Q]
141
+ end
142
+ ```
143
+
144
+ ### Unions
145
+
146
+ A union may be specified within a record using `required` and `optional` with
147
+ the `:union` type:
148
+
149
+ ```ruby
150
+ record :my_record_with_unions do
151
+ required :req_union, :union, types: [:string, :int]
152
+ optional :opt_union, :union, types: [:float, :long]
153
+ end
154
+ ```
155
+
156
+ For an optional union, `null` is automatically added as the first type for
157
+ the union.
158
+
123
159
  ### Auto-loading and Imports
124
160
 
125
161
  Specify paths to search for definitions:
@@ -44,7 +44,8 @@ module Avro
44
44
  end
45
45
 
46
46
  def fixed(name, size = nil, options = {}, &block)
47
- type(name, :fixed, { size: size }.merge(options), &block)
47
+ size_option = size.is_a?(Hash) ? size : { size: size }
48
+ type(name, :fixed, size_option.merge(options), &block)
48
49
  end
49
50
 
50
51
  def type(name, type_name, options = {}, &block)
@@ -68,10 +68,10 @@ module Avro
68
68
 
69
69
  private
70
70
 
71
- # Optional types must be serialized as an array.
71
+ # Optional types must be serialized as a union -- an array of types.
72
72
  def serialized_type(reference_state)
73
73
  result = type.serialize(reference_state)
74
- optional ? [:null, result] : result
74
+ optional ? type.class.union_with_null(result) : result
75
75
  end
76
76
  end
77
77
  end
@@ -1,7 +1,10 @@
1
1
  require 'avro/builder/types/type'
2
2
  require 'avro/builder/types/specific_type'
3
+ require 'avro/builder/types/configurable_type'
4
+ require 'avro/builder/types/type_referencer'
3
5
  require 'avro/builder/types/named_type'
4
6
  require 'avro/builder/types/enum_type'
5
7
  require 'avro/builder/types/fixed_type'
6
8
  require 'avro/builder/types/array_type'
7
9
  require 'avro/builder/types/map_type'
10
+ require 'avro/builder/types/union_type'
@@ -1,6 +1,3 @@
1
- require 'avro/builder/types/configurable_type'
2
- require 'avro/builder/types/type_referencer'
3
-
4
1
  module Avro
5
2
  module Builder
6
3
  module Types
@@ -1,6 +1,3 @@
1
- require 'avro/builder/types/configurable_type'
2
- require 'avro/builder/types/type_referencer'
3
-
4
1
  module Avro
5
2
  module Builder
6
3
  module Types
@@ -25,6 +25,11 @@ module Avro
25
25
  def configure_options(_options = {})
26
26
  # No-op
27
27
  end
28
+
29
+ # Optional fields are represented as a union of the type with :null.
30
+ def self.union_with_null(serialized)
31
+ [:null, serialized]
32
+ end
28
33
  end
29
34
  end
30
35
  end
@@ -0,0 +1,30 @@
1
+ module Avro
2
+ module Builder
3
+ module Types
4
+ class UnionType < Type
5
+ include Avro::Builder::Types::SpecificType
6
+ include Avro::Builder::Types::ConfigurableType
7
+ include Avro::Builder::Types::TypeReferencer
8
+
9
+ dsl_attribute :types do |*types|
10
+ if !types.empty?
11
+ @types = types.flatten.map { |type| find_or_create_type(type) }
12
+ else
13
+ @types
14
+ end
15
+ end
16
+
17
+ # Unions are serialized as an array of types
18
+ def serialize(referenced_state)
19
+ types.map { |type| type.serialize(referenced_state) }
20
+ end
21
+
22
+ # serialized will be an array of types. If the array includes
23
+ # :null then it is moved to the beginning of the array.
24
+ def self.union_with_null(serialized)
25
+ serialized.reject { |type| type == :null }.unshift(:null)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,5 @@
1
1
  module Avro
2
2
  module Builder
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
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.1.0
4
+ version: 0.2.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-03-16 00:00:00.000000000 Z
11
+ date: 2016-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -130,6 +130,7 @@ files:
130
130
  - lib/avro/builder/types/specific_type.rb
131
131
  - lib/avro/builder/types/type.rb
132
132
  - lib/avro/builder/types/type_referencer.rb
133
+ - lib/avro/builder/types/union_type.rb
133
134
  - lib/avro/builder/version.rb
134
135
  homepage: https://github.com/salsify/avro-builder.git
135
136
  licenses: