This commit is contained in:
Jamie Gaskins 2024-07-08 07:37:45 -07:00 committed by GitHub
commit 58c4c2ad6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View file

@ -104,6 +104,23 @@ struct ModelWithEnum
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)
with_dummy do |db|
rs = db.query({{ query }})
@ -206,6 +223,13 @@ describe "DB::Serializable" do
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
with_dummy do |db|
db.query("1,a 2,b") do |rs|

View file

@ -109,7 +109,11 @@ module DB
{% unless ann && ann[:ignore] %}
{%
properties[ivar.id] = {
type: ivar.type,
type: if ivar.type.union?
"Union(#{ivar.type.union_types.map { |t| "::#{t}".id }.join(" | ").id})".id
else
"::#{ivar.type}".id
end,
key: ((ann && ann[:key]) || ivar).id.stringify,
default: ivar.default_value,
nilable: ivar.type.nilable?,