dry-types icon indicating copy to clipboard operation
dry-types copied to clipboard

A bug with array, struct and sum of structs in `try` method

Open pyromaniac opened this issue 10 months ago • 0 comments

Describe the bug

Hi there, I found a couple of bugs working with structs and types in try method

To Reproduce

struct = Dry::Struct(id: Types::Integer)
type = Types::Array.of(struct)
# => #<Dry::Types[Constrained<Array<#<Class:0x0000000114d33398>> rule=[type?(Array)]>]>
type.try([{}])
# ArgumentError: error must be a CoercionError
# from /Users/pyromaniac/.rvm/gems/ruby-3.1.4@bookingsync/gems/dry-types-1.7.1/lib/dry/types/nominal.rb:122:in `failure'
# type.try([{id: 1}])
# => #<Dry::Types::Result::Success input=[#<#<Class:0x0000000114d33398> id=1>]>
type.try([struct.new(id: 1)])
# => #<Dry::Types::Result::Success input=[#<#<Class:0x0000000114d33398> id=1>]>
struct2 = Dry::Struct(id2: Types::Integer)
# => #<Class:0x0000000114f90c58>
type = Types::Array.of(struct | struct2)


# Btw, the inspection is also weird here
# => #<Dry::Types::Constrained:0x163b4>
type.try([{}])
# ArgumentError: error must be a CoercionError
# from /Users/pyromaniac/.rvm/gems/ruby-3.1.4@bookingsync/gems/dry-types-1.7.1/lib/dry/types/nominal.rb:122:in `failure'
type.try([{id: 1}])
# => #<Dry::Types::Result::Success input=[#<#<Class:0x0000000114d33398> id=1>]>
type.try([struct.new(id: 1)])
# NoMethodError: undefined method `input' for #<#<Class:0x0000000114d33398> id=1>
#
#               output << r.input unless Undefined.equal?(r.input)
#                                                         ^^^^^^
# from .rvm/gems/ruby-3.1.4/gems/dry-types-1.7.1/lib/dry/types/array/member.rb:79:in `block in try'

Expected behavior

No errors are expected for those 2 cases.

My environment

  • Affects my production application: YES
  • Ruby version: 3.1.4
  • OS: OS X/Ubuntu
  • Ruby: 3.1.4
  • dry-types: 1.7.1
  • dry-struct: 1.6.0

pyromaniac avatar Oct 04 '23 03:10 pyromaniac