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 +4 -4
- data/.travis.yml +2 -1
- data/CHANGELOG.md +5 -0
- data/README.md +37 -1
- data/lib/avro/builder/dsl.rb +2 -1
- data/lib/avro/builder/field.rb +2 -2
- data/lib/avro/builder/types.rb +3 -0
- data/lib/avro/builder/types/array_type.rb +0 -3
- data/lib/avro/builder/types/map_type.rb +0 -3
- data/lib/avro/builder/types/type.rb +5 -0
- data/lib/avro/builder/types/union_type.rb +30 -0
- data/lib/avro/builder/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: a72124e0979079150b62e4ca3f8f6c727b7bb39c
|
4
|
+
data.tar.gz: 158fc532ae1587737c415069bdcbf3ae136deeaa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f40595449fa8ce0db3ef04d35d96bd3dd133484a383ff1873bdb3c5f1061124c5398b17d31f881e479df603f940c7b3068e9112dbf2156b1a8659e537669b09
|
7
|
+
data.tar.gz: 7d363c07489a17ca4142c34e8a7928f5f737fac535553326d6a2c63007d1c3d0f3b6c6034f1879c9eded1281a2d08d5a51cbac3ef0a7057978edf847ba8ef340
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
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
|
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:
|
data/lib/avro/builder/dsl.rb
CHANGED
@@ -44,7 +44,8 @@ module Avro
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def fixed(name, size = nil, options = {}, &block)
|
47
|
-
|
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)
|
data/lib/avro/builder/field.rb
CHANGED
@@ -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 ?
|
74
|
+
optional ? type.class.union_with_null(result) : result
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
data/lib/avro/builder/types.rb
CHANGED
@@ -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'
|
@@ -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
|
data/lib/avro/builder/version.rb
CHANGED
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.
|
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-
|
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:
|