Replace case statement in Claude Message.from_hash with dynamic class resolution
I maintainer di solito rispondono entro 1 giorno
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
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
- Lingua principale
- Ruby
- Stelle
- 1.3k
- Fork
- 80
- Merge medio
- 21m
- PR unite (30g)
- 3
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Shopify/roast
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 38/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
Shopify/roast#1009 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Shopify/roast
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
fossi-foundation/globalfoundries-pdk-libs-gf180mcu_fd_pv#61 ·
-
good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
KevinSaison/fuchikoma#14 ·
-
Mend: dependency security vulnerability
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
I maintainer di solito rispondono entro 1 giorno
-
PowerShell session to_cmd doesn't escape embedded newlines/carriage returns, allowing statement injectionForse già presa @kx7m2qd l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
rapid7/metasploit-framework#22230 ·
I maintainer di solito rispondono entro 2 giorni
-
blast/small refactor risk/medium spend-dem
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
chriskelly/MusicWall#83 ·
I maintainer di solito rispondono entro 1 giorno