Class: Odin::Validation::SchemaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/odin/validation/schema_parser.rb

Constant Summary collapse

SCHEMA_META_KEYS =

Core schema metadata keys (always metadata, never field definitions)

%w[odin schema].freeze
KEYWORD_TYPES =
[
  ["timestamp", Types::SchemaFieldType::TIMESTAMP],
  ["datetime", Types::SchemaFieldType::TIMESTAMP],
  ["date", Types::SchemaFieldType::DATE],
  ["time", Types::SchemaFieldType::TIME],
  ["duration", Types::SchemaFieldType::DURATION],
  ["string", Types::SchemaFieldType::STRING],
  ["integer", Types::SchemaFieldType::INTEGER],
  ["number", Types::SchemaFieldType::NUMBER],
  ["boolean", Types::SchemaFieldType::BOOLEAN],
  ["currency", Types::SchemaFieldType::CURRENCY],
  ["percent", Types::SchemaFieldType::PERCENT],
  ["binary", Types::SchemaFieldType::BINARY],
  ["null", Types::SchemaFieldType::NULL],
].freeze

Instance Method Summary collapse

Constructor Details

#initializeSchemaParser

Returns a new instance of SchemaParser.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/odin/validation/schema_parser.rb', line 27

def initialize
  @metadata = {}
  @types = {}
  @fields = {}
  @arrays = {}
  @imports = []
  @object_constraints = {}

  @current_header = nil
  @current_header_kind = :root # :root, :metadata, :type, :array, :object
  @current_type_name = nil
  @current_array_path = nil

  # Relative-header context for {.sub} nesting
  @previous_header_path = ""
  @previous_header_type = ""
  @current_type_sub_path = ""
end

Instance Method Details

#parse_schema(text) ⇒ Object

Parse an ODIN schema document text into an OdinSchema



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/odin/validation/schema_parser.rb', line 47

def parse_schema(text)
  lines = text.split("\n")
  lines.each do |raw_line|
    line = raw_line.strip

    if line.empty?
      # Blank line resets metadata mode to root
      if @current_header_kind == :metadata
        @current_header_kind = :root
        @current_header = nil
      end
      next
    end

    next if line.start_with?(";")

    if line.start_with?("@import ")
      parse_import(line)
    elsif line.start_with?("{") && line.include?("}")
      parse_header(line)
    elsif line.start_with?("@") && !line.include?("=")
      parse_bare_type_line(line)
    elsif line.start_with?(":")
      parse_object_constraint(line)
    elsif line.include?("=")
      parse_field_definition(line)
    end
  end

  Types::OdinSchema.new(
    metadata: @metadata,
    types: @types,
    fields: @fields,
    arrays: @arrays,
    imports: @imports,
    object_constraints: @object_constraints
  )
end