mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Fix namespaced vs top-level type-name collisions
Allow top-level property types to have the same name as DB-namespaced ones.
This commit is contained in:
parent
3eaac85a5d
commit
133b78fc28
2 changed files with 26 additions and 2 deletions
|
@ -104,6 +104,23 @@ struct ModelWithEnum
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Ensure types that happen to have the same name as types within the `DB`
|
||||||
|
# namespace don't clash here
|
||||||
|
struct Transaction
|
||||||
|
include DB::Serializable
|
||||||
|
|
||||||
|
@[DB::Field(key: "c0")]
|
||||||
|
getter id : Int32
|
||||||
|
@[DB::Field(key: "c1")]
|
||||||
|
getter status : Status
|
||||||
|
|
||||||
|
enum Status
|
||||||
|
Pending
|
||||||
|
Complete
|
||||||
|
Canceled
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
macro from_dummy(query, type)
|
macro from_dummy(query, type)
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
rs = db.query({{ query }})
|
rs = db.query({{ query }})
|
||||||
|
@ -206,6 +223,13 @@ describe "DB::Serializable" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should compile when top-level names collide with DB-namespaced names" do
|
||||||
|
expect_model("1,Pending", Transaction, {
|
||||||
|
id: 1,
|
||||||
|
status: Transaction::Status::Pending,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
it "should initialize multiple instances from a single resultset" do
|
it "should initialize multiple instances from a single resultset" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
db.query("1,a 2,b") do |rs|
|
db.query("1,a 2,b") do |rs|
|
||||||
|
|
|
@ -136,7 +136,7 @@ module DB
|
||||||
{% elsif value[:nilable] || value[:default] != nil %}
|
{% elsif value[:nilable] || value[:default] != nil %}
|
||||||
rs.read(::Union({{value[:type]}} | Nil))
|
rs.read(::Union({{value[:type]}} | Nil))
|
||||||
{% else %}
|
{% else %}
|
||||||
rs.read({{value[:type]}})
|
rs.read(::{{value[:type]}})
|
||||||
{% end %}
|
{% end %}
|
||||||
rescue exc
|
rescue exc
|
||||||
::raise ::DB::MappingException.new(exc.message, self.class.to_s, {{name.stringify}}, cause: exc)
|
::raise ::DB::MappingException.new(exc.message, self.class.to_s, {{name.stringify}}, cause: exc)
|
||||||
|
@ -166,7 +166,7 @@ module DB
|
||||||
{% elsif value[:default] != nil %}
|
{% elsif value[:default] != nil %}
|
||||||
@{{key}} = %var{key}.is_a?(Nil) ? {{value[:default]}} : %var{key}
|
@{{key}} = %var{key}.is_a?(Nil) ? {{value[:default]}} : %var{key}
|
||||||
{% else %}
|
{% else %}
|
||||||
@{{key}} = %var{key}.as({{value[:type]}})
|
@{{key}} = %var{key}.as(::{{value[:type]}})
|
||||||
{% end %}
|
{% end %}
|
||||||
{% end %}
|
{% end %}
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue