Class: DataModel::Builtin::Hash

Inherits:
Type
  • Object
show all
Includes:
Errors, Logging
Defined in:
lib/data_model/builtin/hash.rb

Overview

Hash type has a concept of “child types”. They can either be specified as params of nested child arrays, or in a hash notation if specific keys are not being specified

This is by far the most complex built-in type

Example: [:hash, [:name, :string], [:email, :string]] # must have two specific keys [:hash, [symbol: :string] # key and values must be according to type [:hash, [symbol: [:string, true]]] # types can be specified long hand

Defined Under Namespace

Classes: Arguments

Instance Attribute Summary

Attributes inherited from Type

#type_args, #type_registry

Instance Method Summary collapse

Methods included from Logging

#log

Methods included from Errors

#blank_error, #blank_error_message, #coerce_error, #coerce_error_message, #earliest_error, #early_error_message, #error_messages, #exclusion_error, #exclusion_error_message, #extra_keys_error, #extra_keys_error_message, #format_error, #format_error_message, #inclusion_error, #inclusion_error_message, #late_error_message, #latest_error, #max_error, #max_error_message, #min_error, #min_error_message, #missing_error, #missing_error_message, #type_error, #type_error_message

Methods inherited from Type

#initialize, #instantiate, #invoke, #type_name

Constructor Details

This class inherits a constructor from DataModel::Type

Instance Method Details

#childrenHash

get the children types of this hash

Returns:

  • (Hash)

    the children types of this hash



89
90
91
92
93
94
95
# File 'lib/data_model/builtin/hash.rb', line 89

def children
  if @children.nil?
    raise "children not configured"
  end

  return @children
end

#configure(params) ⇒ void

This method returns an undefined value.

configure how hash children will be read

Parameters:

  • params (Array)

    the params to configure



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/data_model/builtin/hash.rb', line 26

def configure(params)
  result = @children = {}
  @key_value = false
  log.debug("configuring hash children")

  if params.length == 1 && params.first.length == 1 && params.first.first.is_a?(Hash)
    @key_value = true
    log.debug("configuring hash children with {key => value} notation")
    params = params.first.first

    if params.length != 1
      raise "expected only one key in the {key => value} notation, got #{params.length} for #{params.inspect}"
    end

    key = params.keys.first
    value = params.values.first

    if key.nil?
      raise "schema for key is missing"
    end

    if value.nil?
      raise "schema for value is missing"
    end

    key_node = Scanner.scan(Array(key), type_registry)
    value_node = Scanner.scan(Array(value), type_registry)

    result[:key] = type_registry.type(
      key_node.type,
      args: key_node.args,
      params: key_node.params,
    )

    result[:value] = type_registry.type(
      value_node.type,
      args: value_node.args,
      params: value_node.params,
    )

    return
  end

  log.debug("configuring hash children with array notation")
  for child in params
    name, *schema = child
    if !name.is_a?(Symbol)
      raise "expected name as a symbol for the first element of child schemas, got #{name.inspect} for #{child.inspect}"
    end

    if schema.nil? || schema.empty?
      raise "schema for #{name} is missing"
    end

    node = Scanner.scan(schema)
    log.debug("adding hash child -> #{name}: #{node.to_h}")

    result[name] = instantiate(node.type, args: node.args, params: node.params)
  end
end

#read(val, coerce: false) ⇒ Array(Object, Error)

read a value, and validate it

Parameters:

  • val (Object)

    the value to read

  • coerce (Boolean) (defaults to: false)

    whether to coerce the value

Returns:



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/data_model/builtin/hash.rb', line 103

def read(val, coerce: false)
  args = Arguments.new(type_args)
  errors = Error.new

  # early positive exit for optional & missing
  if args.optional && val.nil?
    return [val, errors]
  end

  if !args.optional && val.nil?
    errors.add(missing_error(type_name))
    return [val, errors]
  end

  # type error, early exit
  if !val.is_a?(Hash) && !coerce
    errors.add(type_error(type_name, val))
    return [val, errors]
  end

  # attempt coercion
  if !val.is_a?(Hash) && coerce
    if val.respond_to?(:to_h)
      val = val.to_h
    elsif val.respond_to?(:to_hash)
      val = Hash(val)
    else
      errors.add(coerce_error(type_name, val))
      return [val, errors]
    end
  end

  # detect extra keys then what is defined in the schema
  if !args.open
    found = []
    for child_key in children.keys
      for val_key in val.keys
        if val_key.to_s == child_key.to_s # doing it this way to support symbols and strings
          found << val_key
        end
      end
    end

    extra = val.keys - found

    if !extra.empty?
      errors.add(extra_keys_error(extra))
      return [val, errors]
    end
  end

  # process children
  if @key_value
    log.debug("processing hash children with {key => value} notation")
    key = children[:key]
    value = children[:value]

    for (k, v) in val.dup
      log.debug("processing #{k} -> #{v.inspect}")
      k, key_errors = key.read(k, coerce:)
      v, value_errors = value.read(v, coerce:)

      if !key_errors.any? && !value_errors.any?
        log.debug("no errors")
        val[k] = v
      end

      errors.merge_child(k, key_errors)
      errors.merge_child(k, value_errors)
    end
  else
    log.debug("processing hash children with array notation")
    for (name, child) in children
      val[name], child_errors = child.read(val[name], coerce:)
      log.debug("child #{name} -> #{val[name].inspect} #{child_errors.inspect}")

      if !child_errors.any?
        log.debug("no errors, skipping")
        next
      end

      errors.merge_child(name, child_errors)
    end
  end

  # done
  return [val, errors]
end