Skip to content

Instantly share code, notes, and snippets.

@brunto
Created July 27, 2020 19:55
Show Gist options
  • Save brunto/c09772ae64a8aa9e30259f52f58217b7 to your computer and use it in GitHub Desktop.
Save brunto/c09772ae64a8aa9e30259f52f58217b7 to your computer and use it in GitHub Desktop.
struct Daterange
def self.adapter
Lucky
end
module Lucky
alias ColumnType = UInt8
include Avram::Type
def parse(value : UInt8)
SuccessfulCast(UInt8).new(value)
end
def parse(values : Array(UInt8))
SuccessfulCast(Array(UInt8)).new values
end
def parse(value : String)
SuccessfulCast(UInt8).new(UInt8.new(value))
end
def parse(values : Array(String))
SuccessfulCast(Array(UInt8)).new values
end
def to_db(value : UInt8)
value.to_s
end
class Criteria(T, V) < Avram::Criteria(T, V)
end
end
end
@brunto
Copy link
Author

brunto commented Jul 27, 2020

web          | Called macro defined in lib/avram/src/avram/save_operation.cr:250:3
web          |
web          |  250 | macro add_cast_value_methods(columns)
web          |
web          | Which expanded to:
web          |
web          |  > 36 | # `value` is passed to `PQ::Param` to properly encode `[true]` to `{t}`, etc...
web          |  > 37 | private def cast_value(value : Daterange)
web          |  > 38 |   value.not_nil!.class.adapter.to_db(value.as(Daterange))
web          |                                        ^----
web          | Error: no overload matches 'Daterange::Lucky.to_db' with type Daterange
web          |
web          | Overloads are:
web          |  - Daterange::Lucky#to_db(value : UInt8)
web          |  - Avram::Type#to_db(value : Nil)

@brunto
Copy link
Author

brunto commented Jul 27, 2020

Instead of SuccessfulCast(UInt8).new(UInt8.new(value))
I could also write the line like this SuccessfulCast(UInt8).new value.to_u8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment