These are chat archives for dry-rb/chat

29th
May 2018
ojab
@ojab
May 29 2018 14:37
float = Dry::Types['coercible.float'].constrained(gteq: 0).constructor do |input|
  input == :x ? 1.0 : input
end

float2 = Dry::Types['coercible.float'].constructor do |input|
  input == :x ? 1.0 : input
end

float[:x] # => 1.0
float2[:x] # => `Float': can't convert Symbol into Float (TypeError)
is it a bug or a feature and why behaviour differs? dry-types-0.13.0 here.
ojab
@ojab
May 29 2018 16:07
 Dry::Types['bool'].enum('true' => true)['true']
# => Dry::Types::ConstraintError ("true" violates constraints ("true" must be an instance of FalseClass failed))
hmm..
Gustavo Caso
@GustavoCaso
May 29 2018 16:23
@ojab Dry::Types['string'].enum('true' => true)['true'] # => true
ojab
@ojab
May 29 2018 16:24
ouch
._.
my bad, thanks
Gustavo Caso
@GustavoCaso
May 29 2018 16:24
No problem
happy to help
ojab
@ojab
May 29 2018 16:26
hmm, actually it returns 'true' instead of true
Gustavo Caso
@GustavoCaso
May 29 2018 16:27
hmm actually it is
Gustavo Caso
@GustavoCaso
May 29 2018 16:33
checking code
ojab
@ojab
May 29 2018 16:37
also looks like 'bool' doesn't propertly handle .constructor:
bool = Dry::Types['bool'].constructor { |input| input }
p bool.valid?(true)
# => undefined method `primitive' for #<Dry::Types::Sum:0x0000000001fd1500> (NoMethodError)
# Did you mean?  primitive?
Gustavo Caso
@GustavoCaso
May 29 2018 16:41
Ok so for Enum is the expected behaviour based on the specs
spec following your example:
context 'with hash value' do
    subject(:enum) { Dry::Types['string'].enum('true' => true, 'false' => false) }

    it 'allows defining an enum from a specific type' do
      expect(enum['true']).to eql('true')
      expect(enum['false']).to eql('false')
      expect(enum[true]).to eql('true')
      expect(enum[false]).to eql('false')
    end
  end
The type Dry::Types['string'] is for making sure the keys are from that specific type
ojab
@ojab
May 29 2018 16:46
So enum returning Boolean is not possible by design or Dry::Types['bool'] in theory should work?
Gustavo Caso
@GustavoCaso
May 29 2018 16:46
That would require the keys being bool
let me check
Dry::Types['bool'].enum(true => 'true')['true']
# => true
Dry::Types['bool'].enum(true => 'true')[true]
# => true
ojab
@ojab
May 29 2018 16:50
looks like it actually mapping values to keys, not vise-a-versa, so it's impossible to create mappings to boolean values :/
looks like the proper way to map values to Booleans is constructor, but we can't check .valid? in that case
Gustavo Caso
@GustavoCaso
May 29 2018 16:51
What are you trying to achieve could you paste the code?
ojab
@ojab
May 29 2018 16:53
I'm trying to spec some code that does the same as Coercions::Params for bool with different BOOLEAN_MAP and the issue is specs:
    subject(:valid?) do
      begin
        coerced = described_class[input]
        described_class.valid?(coerced)
      rescue Dry::Types::ConstraintError
        false
      end
    end
and valid? fails with undefined method 'primitive'
I can compare it with expected value of course, but it's not so intuitive IMO
and also what if returned value is expected, but invalid itself because I wrote a shitty test?
Nikita Shilnikov
@flash-gordon
May 29 2018 16:59
it's a legitimate case to have a constructor on top of sum, so looks like a bug to me
Gustavo Caso
@GustavoCaso
May 29 2018 16:59
I just checking that
looks like works for call method
Nikita Shilnikov
@flash-gordon
May 29 2018 17:02
Gustavo Caso
@GustavoCaso
May 29 2018 17:02
Dry::Types['bool'].constructor { |input| input }.(true)
=> true
Dry::Types['bool'].constructor { |input| input }.(false)
=> false
Dry::Types['bool'].constructor { |input| input }.('hello')
=> "hello"
Dry::Types['bool'].constructor { |input| input }['hello']
Nikita Shilnikov
@flash-gordon
May 29 2018 17:02
@ojab pls report an issue
we'll take a look
Gustavo Caso
@GustavoCaso
May 29 2018 17:04
@flash-gordon bulls eye
ojab
@ojab
May 29 2018 17:04
done dry-rb/dry-types#256
Gustavo Caso
@GustavoCaso
May 29 2018 17:04
No need to call super because Definition valid? method is an alias of
def primitive?(value)
  value.is_a?(primitive)
end
Nikita Shilnikov
@flash-gordon
May 29 2018 17:07
it's from superclass but it's not relevant really for the constructor type
ojab
@ojab
May 29 2018 17:13
Is there any way to change type name without unfreezing it, btw? specs saying that I'm testing #<Dry::Types::Constructor:0x00000000031bef38> are not really descriptive.
Nikita Shilnikov
@flash-gordon
May 29 2018 17:15
nope, introspection to be improved, eventually