-
Notifications
You must be signed in to change notification settings - Fork 33
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
Unable to use custom converter #197
Comments
Some more explication of this issue:
alias Any = Array(PG::BoolArray) | Array(PG::CharArray) | Array(PG::Float32Array) |
Array(PG::Float64Array) | Array(PG::Int16Array) | Array(PG::Int32Array) |
Array(PG::Int64Array) | Array(PG::StringArray) | Array(PG::TimeArray) |
Array(PG::NumericArray) |
Bool | Char | Float32 | Float64 | Int8 | Int16 | Int32 | Int64 | BigDecimal | JSON::Any | JSON::Any::Type | PG::Geo::Box | PG::Geo::Circle |
PG::Geo::Line | PG::Geo::LineSegment | PG::Geo::Path | PG::Geo::Point |
PG::Geo::Polygon | PG::Numeric | PG::Interval | Slice(UInt8) | String | Time |
UInt8 | UInt16 | UInt32 | UInt64 | Clear::Expression::UnsafeSql | UUID |
Nil | Hash(String, Int32) |
Bug investigation update:
src/clear/model/model.cr:45:3
> 11 | def initialize(h : Hash(String, _), @cache : Clear::Model::QueryCache? = nil, @persisted = false, fetch_columns = false )
> 12 | @attributes.merge!(h) if fetch_columns
> 13 |
> 14 | reset(h)
> 15 | end
specifically @attributes.merge!(h) if fetch_columns than it is with the Clear::SQL::Any or the Converter module.
it "should create a new model with a field from a new converter from NamedTuple", focus: true do
body = {
rooms: {
"bed" => 4,
"bath" => 3,
"kitchen" => 1,
},
available: true,
}
house = House.new(body)
house.rooms.should eq(body["rooms"])
house.available.should eq(true)
end
|
Constructor by hash should not be used; it is used internally with recordset but should not be used outside to feed the model like you do. The problem lay in the fact we don't have as much information about the value for each key in comparison to a NamedTuple. For NamedTuple I can get each column type easily, as NamedTuple are defined like I'm not sure there's anything I can do, as I think this is a limit to Crystal language itself. I would say: don't use Hash in this case, but use from/to_json serialization for your model. You could also create a static method def self.from_hash(hash)
mdl = new(rooms: hash[:rooms].as(Hash(String, Int32)) # < this is the casting Clear is not aware of when you pass a hash.
end |
I have the following example which implements custom converter.
However the following error is thrown (here is the full stack trace)
I think this error is due to Clear::SQL::Any not including the custom converter type.
The text was updated successfully, but these errors were encountered: