avro-builder 0.3.1 → 0.3.2

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: 7ad527314da93e91b53852dc9ca99d58170db02b
4
- data.tar.gz: 2c21f0fc2eb009ce181288ee367ff898016a2e5c
3
+ metadata.gz: 12f5d129e7cec1851302f1f7e12213dfef883b95
4
+ data.tar.gz: 215c3ed97b5a9cf92f10358a79b0e4f857839f38
5
5
  SHA512:
6
- metadata.gz: 84d0a142b89fdc3bb831350708a92d39ca96f78233bdae4e62873d1f897d23329a16da60efa3fca3b9643772698e03ff5a0d9eb0ff4f592f68508eac3b35c8bb
7
- data.tar.gz: be8d9e8135b2cd68b90820545de07028f68500c7592d8e09b08a2acf9c333a73c22be42e0df2baccb54fb2fa6076732cbe7acad3d25d4274684491d874e879e5
6
+ metadata.gz: 049df2685c267dae090617f14208ac986b32040b4dd514a8f5eb86ecfe9e749d685ae9a84c561d23e4421bde15cf6615da0473589fe78bb98d26290eedd51fa6
7
+ data.tar.gz: b918ca818611bf1762d980a8a7eae900788b83f280d38d6a98bd7090fb5ae4b6585d4bc51a045c16b74820ba7ee4822e15f4f07e0c36dc0e7b5269d9f3aa2332
@@ -1,5 +1,12 @@
1
1
  # avro-builder changelog
2
2
 
3
+ ## v0.3.2
4
+ - Fix a bug that allowed the partial matching of filenames.
5
+ - Fix a bug that prevented namespace from being specified as an option on
6
+ records.
7
+ - Fix a bug that prevented loading references qualified by namespace.
8
+ - Do not attempt to import schema files for builtin types.
9
+
3
10
  ## v0.3.1
4
11
  - A `null` default should automatically be added for optional fields to match
5
12
  the `:null` first member of the union.
@@ -39,26 +39,16 @@ module Avro
39
39
  ## DSL methods for Types
40
40
 
41
41
  def enum(name, *symbols, **options, &block)
42
- type(name, :enum, { symbols: symbols }.merge(options), &block)
42
+ create_named_type(name, :enum, { symbols: symbols }.merge(options), &block)
43
43
  end
44
44
 
45
45
  def fixed(name, size = nil, options = {}, &block)
46
46
  size_option = size.is_a?(Hash) ? size : { size: size }
47
- type(name, :fixed, size_option.merge(options), &block)
48
- end
49
-
50
- def type(name, type_name, options = {}, &block)
51
- build_type(type_name,
52
- builder: self,
53
- internal: { name: name, namespace: namespace },
54
- options: options,
55
- &block).tap do |type|
56
- add_schema_object(type)
57
- end
47
+ create_named_type(name, :fixed, size_option.merge(options), &block)
58
48
  end
59
49
 
60
50
  # Lookup an Avro schema object by name, possibly fully qualified by namespace.
61
- def lookup(key, required: true)
51
+ def lookup_named_type(key)
62
52
  key_str = key.to_s
63
53
  object = schema_objects[key_str]
64
54
 
@@ -67,11 +57,8 @@ module Avro
67
57
  object = schema_objects[key_str]
68
58
  end
69
59
 
70
- raise "Schema object #{key} not found" if required && !object
60
+ raise "Schema object #{key} not found" unless object
71
61
  object
72
- rescue
73
- raise if required
74
- nil
75
62
  end
76
63
 
77
64
  # Return the last schema object processed as a Hash representing
@@ -111,9 +98,19 @@ module Avro
111
98
  schema_objects[object.fullname] = object if object.namespace
112
99
  end
113
100
 
101
+ def create_named_type(name, type_name, options = {}, &block)
102
+ create_and_configure_builtin_type(type_name,
103
+ builder: self,
104
+ internal: { name: name, namespace: namespace },
105
+ options: options,
106
+ &block).tap do |type|
107
+ add_schema_object(type)
108
+ end
109
+ end
110
+
114
111
  def build_record(name, options, &block)
115
112
  Avro::Builder::Types::RecordType
116
- .new(name, options.merge(namespace: namespace)).tap do |record|
113
+ .new(name, { namespace: namespace }.merge(options)).tap do |record|
117
114
  record.builder = builder
118
115
  record.instance_eval(&block)
119
116
  end
@@ -30,8 +30,14 @@ module Avro
30
30
  send(key, value) if has_dsl_attribute?(key)
31
31
  end
32
32
 
33
- @type = builder.lookup(type_name, required: false) ||
34
- build_type(type_name, field: self, internal: internal, options: options)
33
+ @type = if builtin_type?(type_name)
34
+ create_and_configure_builtin_type(type_name,
35
+ field: self,
36
+ internal: internal,
37
+ options: options)
38
+ else
39
+ builder.lookup_named_type(type_name)
40
+ end
35
41
 
36
42
  # DSL calls must be evaluated after the type has been constructed
37
43
  instance_eval(&block) if block_given?
@@ -22,14 +22,18 @@ module Avro
22
22
  private
23
23
 
24
24
  def find_file(name)
25
- file_name = "#{name.to_s.sub(/\.rb$/, '')}.rb"
25
+ # Ensure that the file_name that is searched for begins with a slash (/)
26
+ # and ends with a .rb extension. Additionally, if the name contains
27
+ # a namespace then ensure that periods (.) are replaced by forward
28
+ # slashes. E.g. for 'test.example' search for '/test/example.rb'.
29
+ file_name = "/#{name.to_s.gsub('.', '/').sub(/^\//, '').sub(/\.rb$/, '')}.rb"
26
30
  matches = self.class.load_paths.flat_map do |load_path|
27
31
  Dir["#{load_path}/**/*.rb"].select do |file_path|
28
32
  file_path.end_with?(file_name)
29
33
  end
30
34
  end
31
35
  raise "Multiple matches: #{matches}" if matches.size > 1
32
- raise "File not found #{name}" if matches.empty?
36
+ raise "File not found #{file_name}" if matches.empty?
33
37
 
34
38
  matches.first
35
39
  end
@@ -4,23 +4,33 @@ module Avro
4
4
  # This concern is used by classes that create new Type instances.
5
5
  module TypeFactory
6
6
 
7
+ COMPLEX_TYPES = Set.new(%w(array enum fixed map record union).map(&:freeze)).freeze
8
+ BUILTIN_TYPES = Avro::Schema::PRIMITIVE_TYPES.union(COMPLEX_TYPES).freeze
9
+
7
10
  private
8
11
 
9
12
  # Return a new Type instance
10
- def create_type(type_name)
13
+ def create_builtin_type(type_name)
14
+ name = type_name.to_s.downcase
11
15
  case
12
- when Avro::Schema::PRIMITIVE_TYPES_SYM.include?(type_name.to_sym)
13
- Avro::Builder::Types::Type.new(type_name)
16
+ when Avro::Schema::PRIMITIVE_TYPES.include?(name)
17
+ Avro::Builder::Types::Type.new(name)
18
+ when COMPLEX_TYPES.include?(name)
19
+ Avro::Builder::Types.const_get("#{name.capitalize}Type").new
14
20
  else
15
- type_class_name = "#{type_name.to_s.capitalize}Type"
16
- Avro::Builder::Types.const_get(type_class_name).new
21
+ raise "Invalid builtin type: #{type_name}"
17
22
  end
18
23
  end
19
24
 
20
25
  # Return a new Type instance, including propagating internal state
21
26
  # and setting attributes via the DSL
22
- def build_type(type_name, field: nil, builder: nil, internal: {}, options: {}, &block)
23
- create_type(type_name).tap do |type|
27
+ def create_and_configure_builtin_type(type_name,
28
+ field: nil,
29
+ builder: nil,
30
+ internal: {},
31
+ options: {},
32
+ &block)
33
+ create_builtin_type(type_name).tap do |type|
24
34
  type.field = field
25
35
  type.builder = builder
26
36
  type.configure_options(internal.merge(options))
@@ -28,6 +38,9 @@ module Avro
28
38
  end
29
39
  end
30
40
 
41
+ def builtin_type?(type_name)
42
+ BUILTIN_TYPES.include?(type_name.to_s)
43
+ end
31
44
  end
32
45
  end
33
46
  end
@@ -1,5 +1,5 @@
1
1
  require 'avro/builder/types/type'
2
- require 'avro/builder/types/specific_type'
2
+ require 'avro/builder/types/complex_type'
3
3
  require 'avro/builder/types/configurable_type'
4
4
  require 'avro/builder/types/type_referencer'
5
5
  require 'avro/builder/types/named_type'
@@ -2,13 +2,13 @@ module Avro
2
2
  module Builder
3
3
  module Types
4
4
  class ArrayType < Type
5
- include Avro::Builder::Types::SpecificType
5
+ include Avro::Builder::Types::ComplexType
6
6
  include Avro::Builder::Types::ConfigurableType
7
7
  include Avro::Builder::Types::TypeReferencer
8
8
 
9
9
  dsl_attribute :items do |items_type = nil|
10
10
  if items_type
11
- @items = find_or_create_type(items_type)
11
+ @items = create_builtin_or_lookup_named_type(items_type)
12
12
  else
13
13
  @items
14
14
  end
@@ -2,9 +2,9 @@ module Avro
2
2
  module Builder
3
3
  module Types
4
4
 
5
- # This module provides common functionality for Types with a specific
6
- # type name vs the generic Type class.
7
- module SpecificType
5
+ # This module provides common functionality for non-primitive types
6
+ # that do not require a name to be created.
7
+ module ComplexType
8
8
 
9
9
  def self.included(base)
10
10
  base.extend ClassMethods
@@ -2,13 +2,13 @@ module Avro
2
2
  module Builder
3
3
  module Types
4
4
  class MapType < Type
5
- include Avro::Builder::Types::SpecificType
5
+ include Avro::Builder::Types::ComplexType
6
6
  include Avro::Builder::Types::ConfigurableType
7
7
  include Avro::Builder::Types::TypeReferencer
8
8
 
9
9
  dsl_attribute :values do |value_type = nil|
10
10
  if value_type
11
- @values = find_or_create_type(value_type)
11
+ @values = create_builtin_or_lookup_named_type(value_type)
12
12
  else
13
13
  @values
14
14
  end
@@ -8,7 +8,7 @@ module Avro
8
8
  # This is an abstract class that represents a type that can be defined
9
9
  # with a name, outside a record.
10
10
  class NamedType < Type
11
- include Avro::Builder::Types::SpecificType
11
+ include Avro::Builder::Types::ComplexType
12
12
  include Avro::Builder::Namespaceable
13
13
  include Avro::Builder::Types::ConfigurableType
14
14
 
@@ -43,7 +43,7 @@ module Avro
43
43
  # Adds fields from the record with the specified name to the current
44
44
  # record.
45
45
  def extends(name)
46
- fields.merge!(builder.lookup(name).duplicated_fields)
46
+ fields.merge!(builder.lookup_named_type(name).duplicated_fields)
47
47
  end
48
48
 
49
49
  def to_h(reference_state = SchemaSerializerReferenceState.new)
@@ -12,8 +12,12 @@ module Avro
12
12
  (!field.nil? && field.builder) || super
13
13
  end
14
14
 
15
- def find_or_create_type(type_name)
16
- builder.lookup(type_name, required: false) || create_type(type_name)
15
+ def create_builtin_or_lookup_named_type(type_name)
16
+ if builtin_type?(type_name)
17
+ create_builtin_type(type_name)
18
+ else
19
+ builder.lookup_named_type(type_name)
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -2,13 +2,15 @@ module Avro
2
2
  module Builder
3
3
  module Types
4
4
  class UnionType < Type
5
- include Avro::Builder::Types::SpecificType
5
+ include Avro::Builder::Types::ComplexType
6
6
  include Avro::Builder::Types::ConfigurableType
7
7
  include Avro::Builder::Types::TypeReferencer
8
8
 
9
+ NULL_TYPE = 'null'.freeze
10
+
9
11
  dsl_attribute :types do |*types|
10
12
  if !types.empty?
11
- @types = types.flatten.map { |type| find_or_create_type(type) }
13
+ @types = types.flatten.map { |type| create_builtin_or_lookup_named_type(type) }
12
14
  else
13
15
  @types
14
16
  end
@@ -22,7 +24,7 @@ module Avro
22
24
  # serialized will be an array of types. If the array includes
23
25
  # :null then it is moved to the beginning of the array.
24
26
  def self.union_with_null(serialized)
25
- serialized.reject { |type| type == :null }.unshift(:null)
27
+ serialized.reject { |type| type.to_s == NULL_TYPE }.unshift(:null)
26
28
  end
27
29
  end
28
30
  end
@@ -1,5 +1,5 @@
1
1
  module Avro
2
2
  module Builder
3
- VERSION = "0.3.1"
3
+ VERSION = "0.3.2"
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.1
4
+ version: 0.3.2
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-24 00:00:00.000000000 Z
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -121,13 +121,13 @@ files:
121
121
  - lib/avro/builder/type_factory.rb
122
122
  - lib/avro/builder/types.rb
123
123
  - lib/avro/builder/types/array_type.rb
124
+ - lib/avro/builder/types/complex_type.rb
124
125
  - lib/avro/builder/types/configurable_type.rb
125
126
  - lib/avro/builder/types/enum_type.rb
126
127
  - lib/avro/builder/types/fixed_type.rb
127
128
  - lib/avro/builder/types/map_type.rb
128
129
  - lib/avro/builder/types/named_type.rb
129
130
  - lib/avro/builder/types/record_type.rb
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
133
  - lib/avro/builder/types/union_type.rb