Support for dynamic initialization of nested classes#461
Support for dynamic initialization of nested classes#461mokuzon merged 5 commits intoline:masterfrom
Conversation
| def self.create(args) | ||
| klass = detect_class(args[:type]) | ||
| return klass.new(**args) if klass | ||
|
|
||
| return new(**args) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def self.detect_class(type) | ||
| { | ||
| accountLink: Line::Bot::V2::Webhook::AccountLinkEvent, | ||
| activated: Line::Bot::V2::Webhook::ActivatedEvent, | ||
| beacon: Line::Bot::V2::Webhook::BeaconEvent, | ||
| botResumed: Line::Bot::V2::Webhook::BotResumedEvent, | ||
| botSuspended: Line::Bot::V2::Webhook::BotSuspendedEvent, | ||
| deactivated: Line::Bot::V2::Webhook::DeactivatedEvent, | ||
| follow: Line::Bot::V2::Webhook::FollowEvent, | ||
| join: Line::Bot::V2::Webhook::JoinEvent, | ||
| leave: Line::Bot::V2::Webhook::LeaveEvent, | ||
| memberJoined: Line::Bot::V2::Webhook::MemberJoinedEvent, | ||
| memberLeft: Line::Bot::V2::Webhook::MemberLeftEvent, | ||
| membership: Line::Bot::V2::Webhook::MembershipEvent, | ||
| message: Line::Bot::V2::Webhook::MessageEvent, | ||
| module: Line::Bot::V2::Webhook::ModuleEvent, | ||
| delivery: Line::Bot::V2::Webhook::PnpDeliveryCompletionEvent, | ||
| postback: Line::Bot::V2::Webhook::PostbackEvent, | ||
| things: Line::Bot::V2::Webhook::ThingsEvent, | ||
| unfollow: Line::Bot::V2::Webhook::UnfollowEvent, | ||
| unsend: Line::Bot::V2::Webhook::UnsendEvent, | ||
| videoPlayComplete: Line::Bot::V2::Webhook::VideoPlayCompleteEvent, | ||
| }[type.to_sym] | ||
| end |
There was a problem hiding this comment.
This is an important change sample in the generated code.
There was a problem hiding this comment.
61c4a5a to
9cedfba
Compare
9cedfba to
91cb949
Compare
| @audience_groups = audience_groups | ||
| @audience_groups = audience_groups&.map do |item| | ||
| if item.is_a?(Hash) | ||
| Line::Bot::V2::ManageAudience::AudienceGroup.create(**item) | ||
| else | ||
| item | ||
| end | ||
| end |
There was a problem hiding this comment.
Here is another important sample of generated code differences.
| def self.detect_class(type) | ||
| { | ||
| camera: Line::Bot::V2::MessagingApi::CameraAction, | ||
| cameraRoll: Line::Bot::V2::MessagingApi::CameraRollAction, | ||
| clipboard: Line::Bot::V2::MessagingApi::ClipboardAction, | ||
| datetimepicker: Line::Bot::V2::MessagingApi::DatetimePickerAction, | ||
| location: Line::Bot::V2::MessagingApi::LocationAction, | ||
| message: Line::Bot::V2::MessagingApi::MessageAction, | ||
| postback: Line::Bot::V2::MessagingApi::PostbackAction, | ||
| richmenuswitch: Line::Bot::V2::MessagingApi::RichMenuSwitchAction, | ||
| uri: Line::Bot::V2::MessagingApi::URIAction, | ||
| }[type.to_sym] |
There was a problem hiding this comment.
There was a problem hiding this comment.
special point: richmenuswitch
| def self.detect_class(type) | ||
| { | ||
| buttons: Line::Bot::V2::MessagingApi::ButtonsTemplate, | ||
| carousel: Line::Bot::V2::MessagingApi::CarouselTemplate, | ||
| confirm: Line::Bot::V2::MessagingApi::ConfirmTemplate, | ||
| image_carousel: Line::Bot::V2::MessagingApi::ImageCarouselTemplate, | ||
| }[type.to_sym] | ||
| end |
There was a problem hiding this comment.
✅ Keys are correct https://github.com/line/line-openapi/blob/4e10c73df579b4c1821a305f0190dfbe8710d2de/messaging-api.yml#L3900-L3906
special point: image_carousel
| def self.create(args) | ||
| klass = detect_class(args[:type]) | ||
| return klass.new(**args) if klass | ||
|
|
||
| return new(**args) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def self.detect_class(type) | ||
| { | ||
| accountLink: Line::Bot::V2::Webhook::AccountLinkEvent, | ||
| activated: Line::Bot::V2::Webhook::ActivatedEvent, | ||
| beacon: Line::Bot::V2::Webhook::BeaconEvent, | ||
| botResumed: Line::Bot::V2::Webhook::BotResumedEvent, | ||
| botSuspended: Line::Bot::V2::Webhook::BotSuspendedEvent, | ||
| deactivated: Line::Bot::V2::Webhook::DeactivatedEvent, | ||
| follow: Line::Bot::V2::Webhook::FollowEvent, | ||
| join: Line::Bot::V2::Webhook::JoinEvent, | ||
| leave: Line::Bot::V2::Webhook::LeaveEvent, | ||
| memberJoined: Line::Bot::V2::Webhook::MemberJoinedEvent, | ||
| memberLeft: Line::Bot::V2::Webhook::MemberLeftEvent, | ||
| membership: Line::Bot::V2::Webhook::MembershipEvent, | ||
| message: Line::Bot::V2::Webhook::MessageEvent, | ||
| module: Line::Bot::V2::Webhook::ModuleEvent, | ||
| delivery: Line::Bot::V2::Webhook::PnpDeliveryCompletionEvent, | ||
| postback: Line::Bot::V2::Webhook::PostbackEvent, | ||
| things: Line::Bot::V2::Webhook::ThingsEvent, | ||
| unfollow: Line::Bot::V2::Webhook::UnfollowEvent, | ||
| unsend: Line::Bot::V2::Webhook::UnsendEvent, | ||
| videoPlayComplete: Line::Bot::V2::Webhook::VideoPlayCompleteEvent, | ||
| }[type.to_sym] | ||
| end |
There was a problem hiding this comment.
| @@ -35,13 +38,15 @@ | |||
| expect(events).not_to be_empty | |||
|
|
|||
| event = events.first | |||
| expect(event).to be_a(Struct) | |||
| expect(event).to be_a(Line::Bot::V2::Webhook::Event) | |||
There was a problem hiding this comment.
can you fix 2 files?
line-bot-sdk-ruby/sig/line/bot/v2/webhook_parser.rbs
Lines 10 to 13 in 9e34215
- now it always return
Line::Bot::V2::Webhook::Event
- now it always return
line-bot-sdk-ruby/lib/line/bot/v2/webhook_parser.rb
Lines 24 to 28 in 9e34215
- comment should be update(maybe we can update sentences more simply)
There was a problem hiding this comment.
👍 49e89af
The RBS file has already been updated.
| expect(body.sent_messages).to be_a(Array) | ||
| expect(body.sent_messages[0]).to be_a(Line::Bot::V2::MessagingApi::SentMessage) | ||
| expect(body.sent_messages[0].id).to eq('461230966842064897') | ||
| expect(body.sent_messages[0].quote_token).to eq('IStG5h1Tz7b...') |
| @@ -22,6 +22,12 @@ module Line | |||
| quick_reply: QuickReply?, | |||
| sender: Sender? | |||
| ) -> void | |||
|
|
|||
| def self.create: (args: Hash[Symbol, untyped]) -> Message | |||
- many query parameters with whitespace - nested response (#461 resolved this)
As confirmed [here](#461 (comment)), the value of type embedded in the class has some peculiarities. There are sufficient tests for deserializing (webhook), so this change adds the missing tests for serialization.
When classes are nested, child classes were not initialized.
This caused the following problems
This PR resolves those problems.
This also adds support for
MembershipEvent, which was not supported by the webhook parser. 61c4a5a