Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix namespaced vs top-level type-name collisions #206

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/serializable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})
Expand Down Expand Up @@ -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|
Expand Down
6 changes: 5 additions & 1 deletion src/db/serializable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
Loading