Skip to content

Support for dynamic initialization of nested classes#461

Merged
mokuzon merged 5 commits intoline:masterfrom
mokuzon:release_v2/dynamic_parse
Apr 4, 2025
Merged

Support for dynamic initialization of nested classes#461
mokuzon merged 5 commits intoline:masterfrom
mokuzon:release_v2/dynamic_parse

Conversation

@mokuzon
Copy link
Copy Markdown
Contributor

@mokuzon mokuzon commented Apr 4, 2025

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

@mokuzon mokuzon self-assigned this Apr 4, 2025
Comment on lines +53 to +85
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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an important change sample in the generated code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mokuzon mokuzon requested a review from a team April 4, 2025 02:25
@mokuzon mokuzon force-pushed the release_v2/dynamic_parse branch from 61c4a5a to 9cedfba Compare April 4, 2025 02:34
@mokuzon mokuzon force-pushed the release_v2/dynamic_parse branch from 9cedfba to 91cb949 Compare April 4, 2025 02:35
Comment on lines -34 to +40
@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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is another important sample of generated code differences.

Copy link
Copy Markdown
Contributor

@Yang-33 Yang-33 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines +51 to +62
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]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

special point: richmenuswitch

Comment on lines +46 to +53
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +53 to +85
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you fix 2 files?

  1. def parse: (
    body: String,
    signature: String
    ) -> Array[Webhook::Event | ::Struct[untyped]]
    • now it always return Line::Bot::V2::Webhook::Event
  2. # @return [Array<Line::Bot::V2::Webhook::Event, Struct>]
    # An array of event objects. Recognized events become instances of classes
    # under `Line::Bot::V2::Webhook::*Event`; otherwise, they're returned as `Struct`.
    # `Struct` is returned as fallback only when the event class is not defined in line-bot-sdk library.
    # When you update the SDK, you may not need to handle `Struct` anymore.
    • comment should be update(maybe we can update sentences more simply)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 49e89af
The RBS file has already been updated.

Comment on lines +375 to +378
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...')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍

@@ -22,6 +22,12 @@ module Line
quick_reply: QuickReply?,
sender: Sender?
) -> void

def self.create: (args: Hash[Symbol, untyped]) -> Message
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mokuzon mokuzon merged commit d9366ab into line:master Apr 4, 2025
6 checks passed
@mokuzon mokuzon deleted the release_v2/dynamic_parse branch April 4, 2025 03:05
Yang-33 added a commit that referenced this pull request Apr 4, 2025
- many query parameters with whitespace
- nested response (#461
resolved this)
github-merge-queue Bot pushed a commit that referenced this pull request Apr 7, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessing a nested structure results in a NoMethodError Parse membership event in webhook parser V2: Automatically create a webhook parser

2 participants