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.



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

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)


41
42
43
# File 'lib/avro/builder/dsl.rb', line 41

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

#as_schemaObject



86
87
88
# File 'lib/avro/builder/dsl.rb', line 86

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

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

DSL methods for Types



60
61
62
# File 'lib/avro/builder/dsl.rb', line 60

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



64
65
66
67
# File 'lib/avro/builder/dsl.rb', line 64

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.



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

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



46
47
48
# File 'lib/avro/builder/dsl.rb', line 46

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.



71
72
73
# File 'lib/avro/builder/dsl.rb', line 71

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



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

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.



92
93
94
# File 'lib/avro/builder/dsl.rb', line 92

def type(*)
  @last_object = super
end

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



96
97
98
99
100
101
102
103
# File 'lib/avro/builder/dsl.rb', line 96

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