Skip to content

DB::Serializable deserialization breaks at compile time if a property type includes a DB type #205

@jgaskins

Description

@jgaskins

I have the concept of a Transaction in my app, and when I added a property whose type included that name (in my case it was a Transaction::Status enum), it stopped compiling with this error message:

 > 40 |                   __temp_2301 =
 > 41 |
 > 42 |                       rs.read(Transaction::Status)
                                      ^------------------
Error: undefined constant Transaction::Status

But it very clearly exists in my model file. It turns out, the issue is that DB::Serializable sees Transaction and the Crystal compiler interprets that as DB::Transaction, but the error message doesn't make that clear. I assume this would also be the case if you had top-level types like Error or Driver.

require "pg"

pg = DB.open("postgres:///")

pg.query_all <<-SQL, as: Transaction
  SELECT
    gen_random_uuid() id,
    0 status
  FROM generate_series(1, 10) AS transactions
  SQL

struct Transaction
  include DB::Serializable

  getter id : UUID
  getter status : Status

  enum Status
    Pending
    Complete
    Canceled
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions