lib/avro/builder/field.rb in avro-builder-0.4.0 vs lib/avro/builder/field.rb in avro-builder-0.5.0
- old
+ new
@@ -5,22 +5,19 @@
# This class represents a field in a record.
# A field must be initialized with a type.
class Field
include Avro::Builder::DslAttributes
- include Avro::Builder::Namespaceable
include Avro::Builder::TypeFactory
INTERNAL_ATTRIBUTES = Set.new(%i(optional_field)).freeze
- attr_accessor :type, :optional_field, :builder, :record
-
# These attributes may be set as options or via a block in the DSL
dsl_attributes :doc, :aliases, :default, :order
- def initialize(name:, type_name:, record:, builder:, internal: {}, options: {}, &block)
- @builder = builder
+ def initialize(name:, type_name:, record:, cache:, internal: {}, options: {}, &block)
+ @cache = cache
@record = record
@name = name.to_s
internal.each do |key, value|
send("#{key}=", value) if INTERNAL_ATTRIBUTES.include?(key)
@@ -31,19 +28,22 @@
end
@type = if builtin_type?(type_name)
create_and_configure_builtin_type(type_name,
field: self,
+ cache: cache,
internal: internal,
options: options)
else
- builder.lookup_named_type(type_name)
+ named_type = true
+ cache.lookup_named_type(type_name, namespace)
end
# DSL calls must be evaluated after the type has been constructed
instance_eval(&block) if block_given?
@type.validate!
+ @type.cache! unless named_type
end
## Delegate additional DSL calls to the type
def respond_to_missing?(id, include_all = false)
@@ -56,10 +56,20 @@
def name_fragment
record.name_fragment
end
+ # Delegate setting namespace explicitly via DSL to type
+ # and return the namespace value from the enclosing record.
+ def namespace(value = nil)
+ if value
+ type.namespace(value)
+ else
+ record.namespace
+ end
+ end
+
# Delegate setting name explicitly via DSL to type
def name(value = nil)
if value
type.name(value)
else
@@ -79,9 +89,11 @@
result.merge!(default: nil) if optional_field
end
end
private
+
+ attr_accessor :type, :optional_field, :cache, :record
# Optional fields must be serialized as a union -- an array of types.
def serialized_type(reference_state)
result = type.serialize(reference_state)
optional_field ? type.class.union_with_null(result) : result