avro-builder 0.3.2 → 0.4.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: 12f5d129e7cec1851302f1f7e12213dfef883b95
4
- data.tar.gz: 215c3ed97b5a9cf92f10358a79b0e4f857839f38
3
+ metadata.gz: a2caec0734e6bf6ba5d83e453fe9d067f9f47fa6
4
+ data.tar.gz: 79fca4e7bf6c5bc9ad9bbcb2425afb902e1b9026
5
5
  SHA512:
6
- metadata.gz: 049df2685c267dae090617f14208ac986b32040b4dd514a8f5eb86ecfe9e749d685ae9a84c561d23e4421bde15cf6615da0473589fe78bb98d26290eedd51fa6
7
- data.tar.gz: b918ca818611bf1762d980a8a7eae900788b83f280d38d6a98bd7090fb5ae4b6585d4bc51a045c16b74820ba7ee4822e15f4f07e0c36dc0e7b5269d9f3aa2332
6
+ metadata.gz: 73e3846ea11202adb26505a47bc3ec7a7772f10f1ff7560593788926922a9c9b45ece9c49bc2fecb0df48627923b44dcc23603cad25020d7c45314f64fcd191f
7
+ data.tar.gz: ae4ffe269bbd87c8fdc6f548898eb3fbf3749781604744f8f79deb38133670a5cdc4a5f2dec0347ff94fb568c0e2e2ae5a1411b4d69efecd32db0f4ceff8bd57
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -1,5 +1,10 @@
1
1
  # avro-builder changelog
2
2
 
3
+ ## v0.4.0
4
+ - Add validation for required DSL attributes that are not specified.
5
+ - Allow name to be configured via a block for top-level record, enum, and fixed
6
+ types.
7
+
3
8
  ## v0.3.2
4
9
  - Fix a bug that allowed the partial matching of filenames.
5
10
  - Fix a bug that prevented namespace from being specified as an option on
data/README.md CHANGED
@@ -24,7 +24,7 @@ This DSL was created because:
24
24
  * Only Avro Schemas, not Protocols are supported.
25
25
  * See [Issues](https://github.com/salsify/avro-builder/issues) for functionality
26
26
  that has yet to be implemented.
27
- * This is alpha quality code. There may be breaking changes until version 1.0 is
27
+ * This is beta quality code. There may be breaking changes until version 1.0 is
28
28
  released.
29
29
 
30
30
  ## Installation
@@ -60,6 +60,8 @@ record :user do
60
60
  required :type, :user_type, default: :REGULAR
61
61
  required :pw, :password
62
62
  optional :full_name, :string
63
+ required :nicknames, :array, items: :string
64
+ required :permissions, :map, values: :bytes
63
65
  end
64
66
  ```
65
67
 
@@ -110,6 +112,20 @@ This generates the following Avro JSON schema:
110
112
  "string"
111
113
  ],
112
114
  "default": null
115
+ },
116
+ {
117
+ "name": "nicknames",
118
+ "type": {
119
+ "type": "array",
120
+ "items": "string"
121
+ }
122
+ },
123
+ {
124
+ "name": "permissions",
125
+ "type": {
126
+ "type": "map",
127
+ "values": "bytes"
128
+ }
113
129
  }
114
130
  ]
115
131
  }
@@ -243,4 +259,3 @@ Issues and pull requests are welcome on GitHub at https://github.com/salsify/avr
243
259
  ## License
244
260
 
245
261
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
246
-
@@ -1,4 +1,5 @@
1
1
  require 'avro'
2
+ require 'avro/builder/errors'
2
3
  require 'avro/builder/dsl_attributes'
3
4
  require 'avro/builder/namespaceable'
4
5
  require 'avro/builder/type_factory'
@@ -25,8 +26,8 @@ module Avro
25
26
  end
26
27
 
27
28
  # Define an Avro schema record
28
- def record(name, options = {}, &block)
29
- add_schema_object(build_record(name, options, &block))
29
+ def record(name = nil, options = {}, &block)
30
+ create_named_type(name, :record, options, &block)
30
31
  end
31
32
 
32
33
  # Imports from the file with specified name fragment.
@@ -38,11 +39,11 @@ module Avro
38
39
 
39
40
  ## DSL methods for Types
40
41
 
41
- def enum(name, *symbols, **options, &block)
42
+ def enum(name = nil, *symbols, **options, &block)
42
43
  create_named_type(name, :enum, { symbols: symbols }.merge(options), &block)
43
44
  end
44
45
 
45
- def fixed(name, size = nil, options = {}, &block)
46
+ def fixed(name = nil, size = nil, options = {}, &block)
46
47
  size_option = size.is_a?(Hash) ? size : { size: size }
47
48
  create_named_type(name, :fixed, size_option.merge(options), &block)
48
49
  end
@@ -104,18 +105,11 @@ module Avro
104
105
  internal: { name: name, namespace: namespace },
105
106
  options: options,
106
107
  &block).tap do |type|
108
+ type.validate!
107
109
  add_schema_object(type)
108
110
  end
109
111
  end
110
112
 
111
- def build_record(name, options, &block)
112
- Avro::Builder::Types::RecordType
113
- .new(name, { namespace: namespace }.merge(options)).tap do |record|
114
- record.builder = builder
115
- record.instance_eval(&block)
116
- end
117
- end
118
-
119
113
  def eval_file(name)
120
114
  instance_eval(read_file(name))
121
115
  end
@@ -0,0 +1,15 @@
1
+ module Avro
2
+ module Builder
3
+
4
+ class RequiredAttributeError < StandardError
5
+ def initialize(type:, attribute:, field: nil, name: nil)
6
+ location = if field
7
+ "field '#{field}' of "
8
+ elsif name
9
+ "'#{name}' of "
10
+ end
11
+ super("attribute :#{attribute} missing for #{location}type :#{type}")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -41,6 +41,7 @@ module Avro
41
41
 
42
42
  # DSL calls must be evaluated after the type has been constructed
43
43
  instance_eval(&block) if block_given?
44
+ @type.validate!
44
45
  end
45
46
 
46
47
  ## Delegate additional DSL calls to the type
@@ -14,6 +14,10 @@ module Avro
14
14
  end
15
15
  end
16
16
 
17
+ def validate!
18
+ validate_required_attribute!(:items)
19
+ end
20
+
17
21
  def serialize(referenced_state)
18
22
  {
19
23
  type: type_name,
@@ -22,6 +22,11 @@ module Avro
22
22
  super(reference_state, overrides: serialized_attributes)
23
23
  end
24
24
 
25
+ def validate!
26
+ super
27
+ validate_required_attribute!(:symbols)
28
+ end
29
+
25
30
  private
26
31
 
27
32
  def serialized_attributes
@@ -13,6 +13,11 @@ module Avro
13
13
  super(reference_state, overrides: serialized_attributes)
14
14
  end
15
15
 
16
+ def validate!
17
+ super
18
+ validate_required_attribute!(:size)
19
+ end
20
+
16
21
  private
17
22
 
18
23
  def serialized_attributes
@@ -20,6 +20,10 @@ module Avro
20
20
  values: values.serialize(referenced_state)
21
21
  }
22
22
  end
23
+
24
+ def validate!
25
+ validate_required_attribute!(:values)
26
+ end
23
27
  end
24
28
  end
25
29
  end
@@ -22,6 +22,10 @@ module Avro
22
22
  end
23
23
  end
24
24
 
25
+ def validate!
26
+ required_attribute_error!(:name) if field.nil? && @name.nil?
27
+ end
28
+
25
29
  # Named types that do not have an explicit name are assigned
26
30
  # a named based on the field and its nesting.
27
31
  def name_fragment
@@ -30,6 +30,27 @@ module Avro
30
30
  def self.union_with_null(serialized)
31
31
  [:null, serialized]
32
32
  end
33
+
34
+ # Subclasses should override this method to check for the presence
35
+ # of required DSL attributes.
36
+ def validate!
37
+ end
38
+
39
+ private
40
+
41
+ def required_attribute_error!(attribute_name)
42
+ raise RequiredAttributeError.new(type: type_name,
43
+ attribute: attribute_name,
44
+ field: field && field.name,
45
+ name: @name)
46
+ end
47
+
48
+ def validate_required_attribute!(attribute_name)
49
+ value = public_send(attribute_name)
50
+ if value.nil? || value.respond_to?(:empty?) && value.empty?
51
+ required_attribute_error!(attribute_name)
52
+ end
53
+ end
33
54
  end
34
55
  end
35
56
  end
@@ -26,6 +26,10 @@ module Avro
26
26
  def self.union_with_null(serialized)
27
27
  serialized.reject { |type| type.to_s == NULL_TYPE }.unshift(:null)
28
28
  end
29
+
30
+ def validate!
31
+ validate_required_attribute!(:types)
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -1,5 +1,5 @@
1
1
  module Avro
2
2
  module Builder
3
- VERSION = "0.3.2"
3
+ VERSION = "0.4.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.3.2
4
+ version: 0.4.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-04-08 00:00:00.000000000 Z
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -102,6 +102,7 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
+ - ".rspec"
105
106
  - ".travis.yml"
106
107
  - CHANGELOG.md
107
108
  - Gemfile
@@ -114,6 +115,7 @@ files:
114
115
  - lib/avro/builder.rb
115
116
  - lib/avro/builder/dsl.rb
116
117
  - lib/avro/builder/dsl_attributes.rb
118
+ - lib/avro/builder/errors.rb
117
119
  - lib/avro/builder/field.rb
118
120
  - lib/avro/builder/file_handler.rb
119
121
  - lib/avro/builder/namespaceable.rb