You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inside rewriteEnvelopeTextPayload in internal/middleware/jqschema.go (lines 400–452), the two case branches for rewriting the first content item share almost identical code. The only structural difference is the element type (map[string]interface{} vs interface{}); the actual mutation logic — shallow-copy the slice, shallow-copy the first item map, set "text", put it back — is identical in both branches.
Duplication Details
Pattern: Shallow-copy-and-mutate first content item
Severity: Medium
Occurrences: 2 (adjacent case branches inside the same function)
Maintainability: Any change to the "set text in first item" logic (e.g., adding a second content item, checking the type field) must be applied twice.
Bug Risk: Low — the two branches are in the same function and visually adjacent, so reviewers tend to catch divergence. However, the asymmetry in handling the !ok case (the []interface{} branch already returns nil, false while the []map branch cannot fail) is a subtle difference that could mask a future bug.
Code Bloat: ~18 lines × 2 branches ≈ 36 lines reducible to ~18 + a small helper.
Refactoring Recommendations
Extract rewriteFirstContentItem helper:
// rewriteFirstContentItem normalises a content slice (either []interface{} or// []map[string]interface{}), shallow-copies the first item, sets its "text"// field to filteredText, and returns the updated items as []interface{}.// Returns nil, false if the slice is empty or the first element is not a map.funcrewriteFirstContentItem(contentValinterface{}, filteredTextstring) ([]interface{}, bool)
Accepts the raw contentVal interface (eliminating the outer type switch)
Returns a []interface{} so rewrittenMap["content"] always has a consistent type
Estimated effort: 1 hour
Benefits: eliminates branch duplication, consolidates the !ok guard in one place
Part of duplicate code analysis: #7299
Summary
Inside
rewriteEnvelopeTextPayloadininternal/middleware/jqschema.go(lines 400–452), the twocasebranches for rewriting the first content item share almost identical code. The only structural difference is the element type (map[string]interface{}vsinterface{}); the actual mutation logic — shallow-copy the slice, shallow-copy the first item map, set"text", put it back — is identical in both branches.Duplication Details
Pattern: Shallow-copy-and-mutate first content item
casebranches inside the same function)internal/middleware/jqschema.go(lines 413–451,rewriteEnvelopeTextPayload)Impact Analysis
typefield) must be applied twice.!okcase (the[]interface{}branch already returnsnil, falsewhile the[]mapbranch cannot fail) is a subtle difference that could mask a future bug.Refactoring Recommendations
Extract
rewriteFirstContentItemhelper:contentValinterface (eliminating the outer type switch)[]interface{}sorewrittenMap["content"]always has a consistent type!okguard in one placeNote: if
NormalizeContentItems(see sub-issue [duplicate-code] Duplicate Code Pattern: Content Slice Normalization across mcp, mcpresult, and middleware #7300) is implemented first, this refactoring becomes simpler — use the new helper to normalize, then mutate the first item.Implementation Checklist
rewriteFirstContentItemhelper (or useNormalizeContentItemsfrom [duplicate-code] Duplicate Code Pattern: Content Slice Normalization across mcp, mcpresult, and middleware #7300)casebranches with calls to the new helperjqschematests still passmake build)Parent Issue
See parent analysis report: #7299
Related to #7299