-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
Configure how nullable values are treated by the struct compiler #519
Comments
I reported #528 which covers the part of configuring a custom struct compiler class. Could you update the description here and provide an example of the API where you define how nullable values should be treated? |
@solnic thanks, #528 looks great!
Actually, I'm not 100% sure about how we should tackle it. What I want is to be able to process how optional types should be treated, no mind if they are an optional attribute or a class MyStructCompiler < StructCompiler
do_with_optional -> (type) { type.maybe }
end But I wonder if this could be done at |
@waiting-for-dev I believe this is something that could be done via |
Thanks for your tip @solnic . However, I think it doesn't fulfill the feature: require 'dry/types'
require 'dry/struct'
require 'dry/monads/maybe'
class Foo < Dry::Struct
transform_types do |type|
if type.optional?
type.constructor do |value|
Dry::Monads::Maybe.coerce(value)
end
else
type
end
end
attribute :foo1, Dry::Types['strict.string'].optional
end
Foo.new(foo1: nil)
# => [Foo.new] nil (NilClass) has invalid type for :foo1 violates constraints (type?(String, None) failed) (Dry::Struct::Error) The type remains being a I first tried transforming the type itself, which seems the appropriate thing to do, but then I always get class Foo < Dry::Struct
transform_types do |type|
type.optional? ? type.right.maybe : type
end
attribute :foo1, Dry::Types['strict.string'].optional
end
puts Foo.new(foo1: nil).foo1.inspect
# => nil
puts Foo.new(foo1: "a").foo1.inspect
# => nil |
This is fixed by dry-rb/dry-types#329 |
Thanks @flash-gordon for your quick action. This is super neat! I don't need to configure anything in the struct compiler. I just have to create a parent struct with the Thanks for all your help. |
Be able to change the way that the struct compiler treats nullable values from the datastore. Right now, they end up being whether
nil
or an actual value. It would be nice to have it being configurable, for example, to translate them into theDry::Monads::Maybe
type.For that, I would:
Examples
Resources
The text was updated successfully, but these errors were encountered: