Replace case statement in Claude Message.from_hash with dynamic class resolution
メンテナーはふだん 1 日以内に返信
評価
この issue はまだ評価されていません。
説明
Summary
Message.from_hash in lib/roast/cogs/agent/providers/claude/message.rb uses an 8-branch case statement to map a :type symbol to a Messages::*Message class. Since every branch follows the exact same naming convention (:"#{type}_message".to_s.camelize), we can replace the static dispatch with dynamic constant resolution.
Current code (lines 23–44)
def from_hash(hash)
type = hash.delete(:type)&.to_sym
case type
when :assistant
Messages::AssistantMessage.new(type:, hash:)
when :result
Messages::ResultMessage.new(type:, hash:)
when :system
Messages::SystemMessage.new(type:, hash:)
when :text
Messages::TextMessage.new(type:, hash:)
when :thinking
Messages::ThinkingMessage.new(type:, hash:)
when :tool_result
Messages::ToolResultMessage.new(type:, hash:)
when :tool_use
Messages::ToolUseMessage.new(type:, hash:)
when :user
Messages::UserMessage.new(type:, hash:)
else
Messages::UnknownMessage.new(type:, hash:)
end
end
Proposed replacement
def from_hash(hash)
type = hash.delete(:type)&.to_sym
class_name = :"#{type}_message".to_s.camelize # e.g. :tool_use → "ToolUseMessage"
klass = if Messages.const_defined?(class_name, false)
Messages.const_get(class_name, false)
else
Messages::UnknownMessage
end
klass.new(type:, hash:)
end
The false argument to const_defined?/const_get restricts the lookup to Messages itself (no ancestor walk), which is the safest approach.
Why
- Open/Closed Principle: Adding a new message type (e.g.
:image) only requires creatingMessages::ImageMessage— no need to touch the factory method. - Precedent: This mirrors how
Workflow#load_cog_or_provideralready usescamelize/constantizefor dynamic class resolution (seelib/roast/workflow.rb:116-119). - Less surface area: Eliminates 16 lines of mechanical case branches that are easy to forget when adding new types.
Edge cases to verify
typeisnil→"_message".camelize→"Message"—Messages.const_defined?("Message", false)returnsfalse, so we correctly fall through toUnknownMessage.- Unknown type like
:foo→"FooMessage"— not defined inMessages, falls through toUnknownMessage. ✓ - Existing types all resolve correctly (confirmed: the naming convention holds for all 8 current types). ✓
Acceptance criteria
- Replace the case statement with dynamic resolution
- All existing tests in
test/roast/cogs/agent/providers/claude/messages/continue to pass - Add a test confirming that an unknown type falls back to
UnknownMessage
- 主要言語
- Ruby
- スター
- 1.3k
- フォーク
- 80
- 平均マージ
- 21分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Shopify/roast のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 38/100
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
Shopify/roast#1009 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
fossi-foundation/globalfoundries-pdk-libs-gf180mcu_fd_pv#61 ·
-
good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
KevinSaison/fuchikoma#14 ·
-
Mend: dependency security vulnerability
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
PowerShell session to_cmd doesn't escape embedded newlines/carriage returns, allowing statement injection対応中かも @kx7m2qd が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
rapid7/metasploit-framework#22230 ·
メンテナーはふだん 2 日以内に返信
-
blast/small refactor risk/medium spend-dem
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
chriskelly/MusicWall#83 ·
メンテナーはふだん 1 日以内に返信