solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Array of Array of FooClass #374

Closed ivanovaleksey closed 5 years ago

ivanovaleksey commented 7 years ago

Hi,

we use Virtus gem in telegram-bot-ruby gem. Some attributes have type Array[Array[FooClass]], for instance

module Telegram
  module Bot
    module Types
      class InlineKeyboardMarkup < Base
        attribute :inline_keyboard, Array[Array[InlineKeyboardButton]]
      end
    end
  end
end

module Telegram
  module Bot
    module Types
      class InlineKeyboardButton < Base
        attribute :text, String
        attribute :url, String
        ...
      end
    end
  end
end

But attribute inspection shows nothing about InlineKeyboardButton, just Array of Array:

Telegram::Bot::Types::InlineKeyboardMarkup.attribute_set.first
=> # Virtus::Attribute::Collection type=Axiom::Types::Array (Array) options={:accessor=>:public, :default=>#<Proc:0x007fbd1485ba28 ....

And it isn't actually coerced to InlineKeyboardButton (it is Array of Array of Hash):

Telegram::Bot::Types::InlineKeyboardMarkup.new inline_keyboard: [[{ text: '1st button text' }, { text: '2nd button text' }]]
=> #<Telegram::Bot::Types::InlineKeyboardMarkup:0x007ff906c66270
 @inline_keyboard=[[{:text=>"1st button text"}, {:text=>"2nd button text"}]]>

To be honest I don't know much about Virtus. Is Array[Array[FooClass]] a valid type for Virtus? Do we need to provide some additional instructions to Virtus in order to get inline_keyboard attribute correctly coerced?

UPDATE Should we switch to dry-rb/struct and dry-rb/types (those gems look like new alternative for Virtus for me)? I created simple gist. It works just fine, I can find out actual type of Markup#keyboard

Thank you.