These are chat archives for dry-rb/chat

14th
Jul 2016
Tim Riley
@timriley
Jul 14 2016 05:22
And it’s merged into master.
Rob Jacoby
@robjacoby
Jul 14 2016 05:49
@timriley do you know much about the rom-rb ecosystem?
Tim Riley
@timriley
Jul 14 2016 06:59
@robjacoby I know some, yes :)
Rob Jacoby
@robjacoby
Jul 14 2016 07:02
I’m having loads of trouble with rom-http and rom-repository
They don’t seem to play nice
Andrew Kozin
@nepalez
Jul 14 2016 10:56

Hi folks!

afaik, structs for now do not support nesting like this:

class User < Dry::Types::Struct
  attribute :name, Types::Strict::String

  attribute :profile do
    attribute :email, Types::Coercible::Email # defined somewhere else
    attribute :phone, Types::Coercible::Phone # ^^

    attribute :preferences do
      attribute :food, Types::Array
    end
  end
end

User.new name: 'Joe',
         profile: {
           email: 'joe@doe.com',
           phone: '1(234)567-8900',
           preferences: {
             food: ['ice-cream']
           }
         }

I know, that I could define types for preferences and profile explicitly (and I do), but in many cases it doesn't have much sense, because they are parts of only one aggregate.

I wonder, (1) maybe I just missing something, (2) if it actually not supported, then should it be?

Tim Riley
@timriley
Jul 14 2016 11:05
@nepalez You’re not missing anything, no. AFAIK the only way to do this right now would be Hash types with their own schemas used as attributes.
we have the new dry-struct project where development on this would take place. Might be worth filing an issue there for discussion?
Andrew Kozin
@nepalez
Jul 14 2016 11:10
@timriley I've missed the new gem ;)
created an issue there
Tim Riley
@timriley
Jul 14 2016 11:11
Nice one, thanks :)
Piotr Solnica
@solnic
Jul 14 2016 11:21
yeah this is exactly why we’re extracting it…there’s SO MANY use cases here
originally I created Struct API exclusively to be used with rom-rb hence default behavior doesn’t work well for many people (all keys are expected to be present)
I actually plan to make it flexible eventually, I don’t think I want to define custom classes for rom….
the only missing bit is somehow inferring types, but that’s extremely tricky
so I’ll probably go with an explicit approach, so that relation view DSL would be extended with support for type annotations and that’s it
with that in place we can generate completely type-safe structs automatically
Andrew Kozin
@nepalez
Jul 14 2016 11:23
@solnic for now those structs and values is the part I use across the whole commercial project I'm dealing with. Thats why I'm VERY sensible to its flexibility :)
Piotr Solnica
@solnic
Jul 14 2016 11:23
I honestly don’t see any reason why I would have to define my custom structs all the time…
esp that types should be provided by relations
and if I need additional behavior, I’d just add decorators in places where I need them, that’s it
@nepalez yeah I know, there are LOTS of use cases for custom structs…we’re just getting started
Michał Pietrus
@blelump
Jul 14 2016 12:20
Hi @solnic , @mensfeld , perhaps you'd be interested: http://www.meetup.com/Elixir-Krakow/events/232467362/
Maciej Mensfeld
@mensfeld
Jul 14 2016 12:23
@blelump thanks! I will defienetely be there
Eger Andreas
@andreaseger
Jul 14 2016 12:52
is there a way to provide an error message for a custom predicate without a yaml file?
Ralf Schmitz Bongiolo
@mrbongiolo
Jul 14 2016 13:01
@andreaseger I don't think so, you would need it defined in your messages, you wanted something similar to the rails errors.add(:field, "my message")?
Eger Andreas
@andreaseger
Jul 14 2016 13:03
actually I don't care about the exact message of this custom predicate at all, just want to know if this specific attribute was valid
Piotr Solnica
@solnic
Jul 14 2016 13:05
if its not in the error messages hash, then it was valid
Eger Andreas
@andreaseger
Jul 14 2016 13:05
problem is if it's invalid I get an error telling me that it can't find a message for my predicate
maybe there is a better way to get all invalid attributes. right now I just do results.messages.keys
Eger Andreas
@andreaseger
Jul 14 2016 13:12
and having to wrap the validation in a begin/rescue block and having to know that Dry::Validation::MissingMessageError corresponds to one of my custom predicates seems less that ideal
Piotr Solnica
@solnic
Jul 14 2016 14:27
@andreaseger there is a way but it’s not a public API yet
Eger Andreas
@andreaseger
Jul 14 2016 14:29
It's not urgent I just added a yaml file and put something in there for my custom predicate. In my use_case just a bit pointless having to do that.
Piotr Solnica
@solnic
Jul 14 2016 14:30
@andreaseger result objects compile messages on-demand, when you call Result#messages. but you can inspect Result#errors which is an array with error objects
it’s gonna be simple to inspect for flat schemas and a bit more tricky for nested schemas
Eger Andreas
@andreaseger
Jul 14 2016 14:34
ok will have a look at that, thanks
Brent Jubinville
@utilityboy
Jul 14 2016 18:07
@solnic you mentioned you had a "better way" of how I've been fiddling with my struct inheritance? :)
John Backus
@backus
Jul 14 2016 18:43
Hey @solnic how could I get dry-struct to show up on the activity side bar here
Piotr Solnica
@solnic
Jul 14 2016 19:44
@backus just added it
@utilityboy yeah so a couple of things a) don’t generate that default-values schema in .build because it defines types all over again b) define types with defaults as constants somewhere ie DefaultUUID = Types::UUID.default { SecureRandom.uuid } etc. c) in .build just do new(EventDefaults[attrs].update(attrs) assuming EventDefaults holds the weak hash with default types
and I’m pretty sure dry-struct will soon provide this kind of functionality OOTB, at least I know that @backus needs this too :)
Brent Jubinville
@utilityboy
Jul 14 2016 20:13
@solnic Ah... ok. I think I get it.