These are chat archives for dry-rb/chat

27th
May 2018
ojab
@ojab
May 27 2018 16:56
Is there any way to wrap dry-struct constructor into custom code? I want to pass XML from Nokogiri instead of Hash (which would be converted to Hash) to .new and doing this outside of constructor is quite unwieldy, since there can be nested structs. I can hack up some module/class that will return Dry::Struct instance on .new, but I'm sure that there is better way.
Nikita Shilnikov
@flash-gordon
May 27 2018 17:17
this doesn't sound like a job for dry-struct
but you can create your own class on top of Dry::Struct and override .new, I don't think there's anything better
and imo shouldn't be
transformation should handled separately. Probably, if you have something super simple then overriding .new is fine
ojab
@ojab
May 27 2018 17:23
Why overriding/wrapping constructor is wrong? It's just a XML -> Hash coercion IMO, and coercions are handled by constructors.
XML is basically a Hash with many values that can be primitive/complex types, complex type is basically a hash. So it maps [almost] directly to dry-types definitions.
Nikita Shilnikov
@flash-gordon
May 27 2018 17:28
coercions is not for structs either, it usually indicates a missing abstraction. Coercion data is a separate concern which can be done occasionally by structs
there's transproc for data transformations and dry-validation for ... validation
ojab
@ojab
May 27 2018 17:30
will take a look at transproc, didn't knew about it
Nikita Shilnikov
@flash-gordon
May 27 2018 17:31
yeah, we'll pull it into dry-rb eventually, I guess
ojab
@ojab
May 27 2018 17:44
anyway, I don't understand why it's not dry-struct (or dry-types to be exact) job. I have a data with some type, type definition via dry-types and I want to check that data conforms to that type. If I have nested hashes, it becomes quite tedious to convert it from source format to ruby hashes and, basically, I should redo dry-types job (where should I expect hashes in source data? What if there is a String instead of Hash in expected place?).
so I should recursively go through the type definition and convert where data should be converted in advance or duplicate type definitions in other code, I don't see a better way.
…and also I should do it only for Hashes, since other types are magically converted via coercion.
Nikita Shilnikov
@flash-gordon
May 27 2018 17:46
dry-validation is aimed to solve this, even dry-v alone is too big to hadle this because there's a problem of generating nice error messages etc
btw I don't think dry-v supports recursive definition, there's probably an issue about that
however, I can't promise it will be supported in 1.0 because it's a rare case, sort of
it will also affect AST which can be a problem
i.e. it can become too complex
ojab
@ojab
May 27 2018 18:02
ouch, looks like my issue is dry-rb/dry-struct#82
Jonah
@jonahx
May 27 2018 22:02
dry rubyists, what are your thoughts on refinements? do you ever use them? do you hate them? inquiring minds what to know.