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”

Defined Under Namespace

Classes: Arguments

Constant Summary

Constants included from Errors

Errors::TClassCtx, Errors::TClassValueCtx, Errors::TErrorMessageBuilder, Errors::TErrorMessages, Errors::TFormatCtx, Errors::TSetCtx, Errors::TTemporal, Errors::TWithinCtx, Errors::TWithinTemporalCtx

Constants inherited from Type

Type::TArguments, Type::TTypeParams, Type::TTypeResult

Instance Attribute Summary

Attributes inherited from Type

#type_args

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

Constructor Details

This class inherits a constructor from DataModel::Type

Instance Method Details

#childrenObject



41
42
43
44
45
46
47
# File 'lib/data_model/builtin/hash.rb', line 41

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

	return @children
end

#configure(params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/data_model/builtin/hash.rb', line 17

def configure(params)
	result = T.let({}, T::Hash[Symbol, Type])
	@children = T.let(result, T.nilable(T::Hash[Symbol, Type]))

	log.debug("configuring hash children")

	for child in T.cast(params, T::Array[T::Array[Object]])
		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.serialize}")

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

#read(val, coerce: false) ⇒ Object



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
87
88
89
90
91
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
# File 'lib/data_model/builtin/hash.rb', line 52

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(Hash))
		return [val, errors]
	end

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

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

	hash = T.cast(val, T::Hash[Symbol, Object])

	# detect extra keys then what is defined in the schema
	if !args.open
		keys = children.keys
		extra = hash.keys - keys

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

	# process children
	log.debug("processing hash children")
	for (name, child) in children
		hash[name], child_errors = child.read(hash[name], coerce:)
		log.debug("child #{name} -> #{hash[name].inspect} #{child_errors.inspect}")

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

		errors.merge_child(name, child_errors)

		return [val, errors]
	end

	log.debug("hash check successful")

	# done
	return [val, errors]
end