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

#extract_type_arraysObject

Pull array-of-object entry fields out of each type’s flat field map. A key ‘arr[]` (whose value is a type reference) becomes an array with an entry type ref; keys `arr[].field` become an array whose item fields are those fields. Matching keys are removed from the type’s fields.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/odin/validation/schema_parser.rb', line 92

def extract_type_arrays
  @types.each do |type_name, type|
    builders = {}
    remaining_fields = {}

    type.fields.each do |key, field|
      m = key.match(/\A([^\[\]]+)\[\](?:\.(.+))?\z/)
      unless m
        remaining_fields[key] = field
        next
      end

      arr_name = m[1]
      item_path = m[2]
      b = builders[arr_name] ||= { item_fields: {}, item_type_ref: nil }

      if item_path.nil?
        # arr[] = @type  -> entry fields come from the referenced type
        ref = field.type_ref
        b[:item_type_ref] = ref if ref && ref != "array"
      else
        # arr[].item_path = field
        b[:item_fields][item_path] = field
      end
    end

    next if builders.empty?

    arrays = {}
    builders.each do |arr_name, b|
      arrays[arr_name] = Types::SchemaArray.new(
        path: arr_name,
        item_fields: b[:item_fields],
        unique: false,
        item_type_ref: b[:item_type_ref]
      )
    end

    @types[type_name] = Types::SchemaType.new(
      name: type.name,
      fields: remaining_fields,
      namespace: type.namespace,
      composition: type.composition,
      base_type: type.base_type,
      constraints: type.constraints,
      intersection_types: type.intersection_types,
      parent_types: type.parent_types,
      arrays: arrays
    )
  end
end

#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
85
86
# 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

  extract_type_arrays

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