Hi! Ultimately what I’m trying to do here is implement ActiveRecord serialization for dry-struct, as mentioned here: dry-struct and serializer
I have a struct with reference to another struct, as follows:
class InventoryPayload < Dry::Struct
attribute :items, Types::Array.of(Types.Instance(EventTypes::EventLineItem))
end
class EventLineItem < Dry::Struct
transform_keys(&:to_sym)
attribute :item_id, Types::Integer
attribute :item_value_in_cents, Types::Integer
end
When this is dumped to a hash, the initialization logic doesn’t seem to have a good way to turn the nested part of the hash back into a struct:
irb> EventTypes::InventoryPayload.new(items: [{item_id: 1, item_value_in_cents: 1}])
Dry::Struct::Error: [EventTypes::InventoryPayload.new] [{:item_id=>1, :item_value_in_cents=>1}] (Array) has invalid type for :items violates constraints (type?(EventTypes::EventLineItem, {:item_id=>1, :item_value_in_cents=>1}) failed)
The only thing I was able to get working is to add a method InventoryPayload
that maps the input hash to EventLineItem
s. But this seems kind of odd - there should be some way to automatically coerce this? If not, that’s fine, but I’m wondering if I’m missing something.