mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Merged branch bugfix/mapping-nil into master (#40)
This commit is contained in:
commit
96663d8d21
2 changed files with 37 additions and 4 deletions
|
@ -29,6 +29,20 @@ class MappingWithNilables
|
|||
})
|
||||
end
|
||||
|
||||
class MappingWithNilTypes
|
||||
DB.mapping({
|
||||
c0: {type: Int32?, default: 10},
|
||||
c1: String?,
|
||||
})
|
||||
end
|
||||
|
||||
class MappingWithNilUnionTypes
|
||||
DB.mapping({
|
||||
c0: {type: Int32 | Nil, default: 10},
|
||||
c1: Nil | String,
|
||||
})
|
||||
end
|
||||
|
||||
class MappingWithKeys
|
||||
DB.mapping({
|
||||
foo: {type: Int32, key: "c0"},
|
||||
|
@ -100,14 +114,24 @@ describe "DB.mapping" do
|
|||
expect_mapping("1,NULL", MappingWithDefaults, {c0: 1, c1: "c"})
|
||||
end
|
||||
|
||||
it "should initialize a mapping with nils if columns are missing" do
|
||||
it "should initialize a mapping with nilable set if columns are missing" do
|
||||
expect_mapping("1", MappingWithNilables, {c0: 1, c1: nil})
|
||||
end
|
||||
|
||||
it "should initialize a mapping with nils ignoring default value is type is nilable" do
|
||||
it "should initialize a mapping with nilable set ignoring default value if NULL" do
|
||||
expect_mapping("NULL,a", MappingWithNilables, {c0: nil, c1: "a"})
|
||||
end
|
||||
|
||||
it "should initialize a mapping with nilable types if columns are missing" do
|
||||
expect_mapping("1", MappingWithNilTypes, {c0: 1, c1: nil})
|
||||
expect_mapping("1", MappingWithNilUnionTypes, {c0: 1, c1: nil})
|
||||
end
|
||||
|
||||
it "should initialize a mapping with nilable types ignoring default value if NULL" do
|
||||
expect_mapping("NULL,a", MappingWithNilTypes, {c0: nil, c1: "a"})
|
||||
expect_mapping("NULL,a", MappingWithNilUnionTypes, {c0: nil, c1: "a"})
|
||||
end
|
||||
|
||||
it "should initialize a mapping with different keys" do
|
||||
expect_mapping("1,a", MappingWithKeys, {foo: 1, bar: "a"})
|
||||
end
|
||||
|
|
|
@ -64,6 +64,15 @@ 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) %}
|
||||
|
||||
{% if value[:type].is_a?(Call) && value[:type].name == "|" &&
|
||||
(value[:type].receiver.resolve == Nil || value[:type].args.map(&.resolve).any?(&.==(Nil))) %}
|
||||
{% value[:nilable] = true %}
|
||||
{% end %}
|
||||
{% end %}
|
||||
|
||||
{% for key, value in properties %}
|
||||
@{{key.id}} : {{value[:type]}} {{ (value[:nilable] ? "?" : "").id }}
|
||||
|
||||
|
@ -99,7 +108,7 @@ module DB
|
|||
{% if value[:converter] %}
|
||||
{{value[:converter]}}.from_rs(%rs)
|
||||
{% elsif value[:nilable] || value[:default] != nil %}
|
||||
%rs.read(Union({{value[:type]}} | Nil))
|
||||
%rs.read(::Union({{value[:type]}} | Nil))
|
||||
{% else %}
|
||||
%rs.read({{value[:type]}})
|
||||
{% end %}
|
||||
|
@ -131,7 +140,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
|
||||
|
|
Loading…
Reference in a new issue