class Dry::Types::Compiler
@api private
Attributes
registry[R]
Public Class Methods
new(registry)
click to toggle source
# File lib/dry/types/compiler.rb, line 13 def initialize(registry) @registry = registry end
Public Instance Methods
call(ast)
click to toggle source
# File lib/dry/types/compiler.rb, line 17 def call(ast) visit(ast) end
compile_fn(fn)
click to toggle source
# File lib/dry/types/compiler.rb, line 118 def compile_fn(fn) type, *node = fn case type when :id Dry::Types::FnContainer[node.fetch(0)] when :callable node.fetch(0) when :method target, method = node target.method(method) else raise ArgumentError, "Cannot build callable from #{fn.inspect}" end end
visit(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 21 def visit(node) type, body = node send(:"visit_#{type}", body) end
visit_any(meta)
click to toggle source
# File lib/dry/types/compiler.rb, line 114 def visit_any(meta) registry["any"].meta(meta) end
visit_array(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 63 def visit_array(node) member, meta = node member = member.is_a?(Class) ? member : visit(member) registry["nominal.array"].of(member).meta(meta) end
visit_constrained(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 26 def visit_constrained(node) nominal, rule = node type = visit(nominal) type.constrained_type.new(type, rule: visit_rule(rule)) end
visit_constructor(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 32 def visit_constructor(node) nominal, fn = node primitive = visit(nominal) primitive.constructor(compile_fn(fn)) end
visit_enum(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 104 def visit_enum(node) type, mapping = node Enum.new(visit(type), mapping: mapping) end
visit_hash(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 69 def visit_hash(node) opts, meta = node registry["nominal.hash"].with(**opts, meta: meta) end
visit_json_array(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 84 def visit_json_array(node) member, meta = node registry["json.array"].of(visit(member)).meta(meta) end
visit_json_hash(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 79 def visit_json_hash(node) keys, meta = node registry["json.hash"].schema(keys.map { |key| visit(key) }, meta) end
visit_key(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 99 def visit_key(node) name, required, type = node Schema::Key.new(visit(type), name, required: required) end
visit_lax(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 38 def visit_lax(node) Types::Lax.new(visit(node)) end
visit_map(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 109 def visit_map(node) key_type, value_type, meta = node registry["nominal.hash"].map(visit(key_type), visit(value_type)).meta(meta) end
visit_nominal(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 43 def visit_nominal(node) type, meta = node nominal_name = "nominal.#{Types.identifier(type)}" if registry.registered?(nominal_name) registry[nominal_name].meta(meta) else Nominal.new(type, meta: meta) end end
visit_params_array(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 94 def visit_params_array(node) member, meta = node registry["params.array"].of(visit(member)).meta(meta) end
visit_params_hash(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 89 def visit_params_hash(node) keys, meta = node registry["params.hash"].schema(keys.map { |key| visit(key) }, meta) end
visit_rule(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 54 def visit_rule(node) Dry::Types.rule_compiler.([node])[0] end
visit_schema(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 74 def visit_schema(node) keys, options, meta = node registry["nominal.hash"].schema(keys.map { |key| visit(key) }).with(**options, meta: meta) end
visit_sum(node)
click to toggle source
# File lib/dry/types/compiler.rb, line 58 def visit_sum(node) *types, meta = node types.map { |type| visit(type) }.reduce(:|).meta(meta) end