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

MissingAttributeError when access optional field with hash syntax

Open pricelessrabbit opened this issue 3 years ago • 0 comments

I all :) hope it is the right place to post.

Describe the bug

i tried to access struct attributes using the hash syntax and i found out different behaviour when access optional field in struct using method (struct.field) and hash (struct[:field]). Is this the intended behaviour or a bug?

To Reproduce

class MyStuct < Dry::Struct
   transform_keys(&:to_sym)
   attribute? :my_attribute, Dry::Types::Integer.optional
end

Expected behavior

s = MyStruct.new
s.my_attribute # nil
s[:my_attribute] # nil

Actual behavior

s = MyStruct.new
s.my_attribute # nil
s[:my_attribute] #MissingAttributeError

pricelessrabbit avatar Jul 27 '21 14:07 pricelessrabbit