These are chat archives for dry-rb/chat

4th
May 2018
Damien Timewell
@idlefingers
May 04 2018 09:39
Hey guys, I'm upgrading a project I'm working on to the most recent dry-types and dry-struct and trying to understand how to deal with the removal of constructor_type. Can someone point me in the direction of how to make all attributes omittable, without explicitly having to set .meta(omittable: true)? I used constructor_type :schema to get around it before..
Gustavo Caso
@GustavoCaso
May 04 2018 09:44
Hi @idlefingers
I think this will do the trick
class User < Dry::Struct
  transform_types do |type|
    type.meta(omittable: true)
  end

  attribute :name, Types::Strict::String
  attribute :age,  Types::Strict::Integer.default(18)
end
Please let me know if that solved the problem
Damien Timewell
@idlefingers
May 04 2018 09:49
Ahaaaa, I get it. It works! Perfect, thanks a ton for your help @GustavoCaso :)
Gustavo Caso
@GustavoCaso
May 04 2018 09:50
My pleasure :smile:
Damien Timewell
@idlefingers
May 04 2018 09:56
Hmmm, but now the meta is included in Hash values :/ ...
class User < Dry::Struct
  transform_types do |type|
    type.meta(omittable: true)
  end

  attribute :foo, Types::Hash.default(Hash.new)
end
User.new.foo # => {:meta=>{:omittable=>true}}
Gustavo Caso
@GustavoCaso
May 04 2018 09:58
class User < Dry::Struct
  transform_types do |type|
    type.optional
  end

  attribute :name, Types::Strict::String
  attribute :age,  Types::Strict::Integer.default(18)
end
Could you try that one?
Damien Timewell
@idlefingers
May 04 2018 10:00
That just means it can be nil, but the key is still required, right?
Dry::Struct::Error: [User.new] :name is missing in Hash input
Gustavo Caso
@GustavoCaso
May 04 2018 10:02
You can set a deafult value if what you are looking is for avoid the meta
Does it helps you? @idlefingers
Damien Timewell
@idlefingers
May 04 2018 10:04
I'm not sure I follow. In the example class I pasted above, I set the default to be a new hash, but when the class is initialized, that hash includes became {:meta=>{:omittable=>true}}
so instead of User.new.foo # => {:meta=>{:omittable=>true}} I'm trying to get User.new.foo # => {} (which is what constructor_type :schema did before)
Gustavo Caso
@GustavoCaso
May 04 2018 10:07
All your attributes are hashes?
class User < Dry::Struct
  transform_types do |type|
    type.optional.deafult({})
  end

  attribute :foo, Types::Hash
end
Damien Timewell
@idlefingers
May 04 2018 10:08
No, they could be anything
Gustavo Caso
@GustavoCaso
May 04 2018 10:08
I think this will do trick for that case you mentioned
Damien Timewell
@idlefingers
May 04 2018 10:10
I don't think that will work :( It needs to be able to handle any type
Gustavo Caso
@GustavoCaso
May 04 2018 10:28
@idlefingers how about this to avoid the extra meta attribute
class User < Dry::Struct
  transform_types do |type|
    if type.default?
      type
    else
      type.meta(omittable: true)
    end
  end
  attribute :foo, Types::Hash.default({})
  attribute :bar, Types::String
end

User.new
=> #<User foo={} bar=nil>
Damien Timewell
@idlefingers
May 04 2018 10:31
nice! Seems to work. Will test around some more
Gustavo Caso
@GustavoCaso
May 04 2018 10:31
Sure let me know if you need anything
Damien Timewell
@idlefingers
May 04 2018 10:31
thanks
Gustavo Caso
@GustavoCaso
May 04 2018 10:31
your welcome
Nikita Shilnikov
@flash-gordon
May 04 2018 11:24
this looks like a bug to me, something's not quite right with defaults where
Gustavo Caso
@GustavoCaso
May 04 2018 11:25
@flash-gordon I'm happy to have a look
Gustavo Caso
@GustavoCaso
May 04 2018 12:30
@idlefingers I have open a PR fixing the issue dry-rb/dry-types#250
Damien Timewell
@idlefingers
May 04 2018 12:32
Awesome @GustavoCaso. So with that change, this should work?
transform_types do |type|
  type.meta(omittable: true)
end
Gustavo Caso
@GustavoCaso
May 04 2018 12:33
It should work I will try running more test with dry-struct
Damien Timewell
@idlefingers
May 04 2018 12:33
:thumbsup:
Gustavo Caso
@GustavoCaso
May 04 2018 12:33
but if you want to try and let me know that would be great
Nikita Shilnikov
@flash-gordon
May 04 2018 12:35
I wouldn't call meta on defaults anyway, it doesn't make sense to me
@idlefingers don't forget, you can create a base class if you don't want to call transform_types many times
Damien Timewell
@idlefingers
May 04 2018 12:38
@flash-gordon that's exactly what led me down this path :)
since it's an api wrapper I'm building, it's very hard to be sure all expected attributes are in responses, so I needed to make it a little less strict (constructor_type :schema style :P )
Nikita Shilnikov
@flash-gordon
May 04 2018 12:42
yeah, it was always hard to control the desired behavior with constructor_type, now it's crystal clear
Damien Timewell
@idlefingers
May 04 2018 12:43
:+1: it was a little confusing before. Much prefer the new approach, even if my implementation was a bit of a rocky road
Daniel Sandbecker
@daniels
May 04 2018 13:05
In Dry::Struct, can a default value depend on the value of another attribute?
Nikita Shilnikov
@flash-gordon
May 04 2018 13:06
no
however, you can override the constructor
Daniel Sandbecker
@daniels
May 04 2018 13:07
Ok. Thanks, that was quick.