Class: Avro::Builder::DSL

Inherits:
Object
  • Object
show all
Includes:
AnonymousTypes, DslAttributes, DslOptions, FileHandler
Defined in:
lib/avro/builder/dsl.rb

Overview

This class is used to construct Avro schemas (not protocols) using a ruby DSL

Constant Summary

Constants included from TypeFactory

TypeFactory::BUILTIN_TYPES, TypeFactory::COMPLEX_TYPES, TypeFactory::NAMED_TYPES

Constants included from FileHandler

FileHandler::FileNotFoundError

Instance Method Summary collapse

Methods included from AnonymousTypes

#array, #map, #union

Methods included from FileHandler

#find_file, included, #read_file

Methods included from DslAttributes

#dsl_attribute?, included

Methods included from DslOptions

#dsl_option?, included

Constructor Details

#initialize(str = nil, filename: nil, &block) ⇒ DSL

An instance of the DSL is initialized with a string or a block to evaluate to define Avro schema objects.



33
34
35
36
37
38
39
40
41
# File 'lib/avro/builder/dsl.rb', line 33

def initialize(str = nil, filename: nil, &block)
  if str
    instance_eval(*[str, filename].compact)
  elsif filename
    instance_eval(File.read(filename), filename)
  else
    instance_eval(&block)
  end
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/avro/builder/dsl.rb', line 43

def abstract?
  @last_object && @last_object.abstract?
end

#as_schemaObject



88
89
90
# File 'lib/avro/builder/dsl.rb', line 88

def as_schema
  Avro::Schema.parse(to_json(validate: false))
end

#enum(name = nil, *symbols, **options, &block) ⇒ Object

DSL methods for Types



62
63
64
# File 'lib/avro/builder/dsl.rb', line 62

def enum(name = nil, *symbols, **options, &block)
  create_named_type(name, :enum, { symbols: symbols }.merge(options), &block)
end

#fixed(name = nil, size = nil, options = {}, &block) ⇒ Object



66
67
68
69
# File 'lib/avro/builder/dsl.rb', line 66

def fixed(name = nil, size = nil, options = {}, &block)
  size_option = size.is_a?(Hash) ? size : { size: size }
  create_named_type(name, :fixed, size_option.merge(options), &block)
end

#import(name) ⇒ Object

Imports from the file with specified name fragment.



53
54
55
56
57
58
# File 'lib/avro/builder/dsl.rb', line 53

def import(name)
  previous_namespace = namespace
  result = eval_file(name)
  namespace(previous_namespace)
  result
end

#record(name = nil, options = {}, &block) ⇒ Object

Define an Avro schema record



48
49
50
# File 'lib/avro/builder/dsl.rb', line 48

def record(name = nil, options = {}, &block)
  create_named_type(name, :record, options, &block)
end

#to_hObject

Return the last schema object processed as a Hash representing the Avro schema.



73
74
75
# File 'lib/avro/builder/dsl.rb', line 73

def to_h
  @last_object.to_h(SchemaSerializerReferenceState.new)
end

#to_json(validate: true, pretty: true) ⇒ Object

Return the last schema object processed as an Avro JSON schema



78
79
80
81
82
83
84
85
86
# File 'lib/avro/builder/dsl.rb', line 78

def to_json(validate: true, pretty: true)
  hash = to_h
  (pretty ? JSON.pretty_generate(hash) : hash.to_json).tap do |json|
    # Uncomment the next line to debug:
    # puts json
    # Parse the schema to validate before returning
    ::Avro::Schema.parse(json) if validate
  end
end

#typeObject

Override the type method from AnonymousTypes to store a reference to the last type defined.



94
95
96
# File 'lib/avro/builder/dsl.rb', line 94

def type(*)
  @last_object = super
end

#type_macro(name, type_object, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/avro/builder/dsl.rb', line 98

def type_macro(name, type_object, options = {})
  raise "#{type_object.inspect} must be a type object" unless type_object.is_a?(Types::Type)
  raise "namespace cannot be included in name: #{name}" if name.to_s.index('.')

  type_clone = type_object.clone
  type_clone.send(:abstract=, true)
  cache.add_type_by_name(type_clone, name, options[:namespace] || namespace)
  @last_object = type_clone
end