Replace case statement in Claude Message.from_hash with dynamic class resolution
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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
- Ngôn ngữ chính
- Ruby
- Star
- 1.3k
- Fork
- 80
- Merge trung bình
- 21 phút
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Shopify/roast
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 38/100
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Shopify/roast#1009 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của Shopify/roast
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
fossi-foundation/globalfoundries-pdk-libs-gf180mcu_fd_pv#61 ·
-
good first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
KevinSaison/fuchikoma#14 ·
-
Mend: dependency security vulnerability
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
PowerShell session to_cmd doesn't escape embedded newlines/carriage returns, allowing statement injectionCó thể đã có người làm @kx7m2qd đã nhận 1 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
rapid7/metasploit-framework#22230 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
blast/small refactor risk/medium spend-dem
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
chriskelly/MusicWall#83 ·
Maintainer thường phản hồi trong vòng 1 ngày