Allow nilable types in DB.mapping

Previously, using a Nilable type in DB.mapping instead of nilable: true would
fail with a nil assertion error.
This commit is contained in:
RX14 2017-02-14 21:51:45 +00:00
parent 3fa1eac6c5
commit 0797ce72a3
2 changed files with 22 additions and 3 deletions

View file

@ -64,6 +64,10 @@ module DB
{% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
{% end %}
{% for key, value in properties %}
{% value[:nilable] = true if value[:type].is_a?(Generic) && value[:type].type_vars.map(&.resolve).includes?(Nil) %}
{% end %}
{% for key, value in properties %}
@{{key.id}} : {{value[:type]}} {{ (value[:nilable] ? "?" : "").id }}
@ -131,7 +135,7 @@ module DB
{% elsif value[:default] != nil %}
@{{key.id}} = %var{key.id}.is_a?(Nil) ? {{value[:default]}} : %var{key.id}
{% else %}
@{{key.id}} = %var{key.id}.not_nil!
@{{key.id}} = %var{key.id}.as({{value[:type]}})
{% end %}
{% end %}
end