Refactor duplicated first-content-item rewrite logic in rewriteEnvelopeTextPayload#7324
Merged
Conversation
5 tasks
Copilot
AI
changed the title
[WIP] Refactor duplicate code in rewriteEnvelopeTextPayload
Refactor duplicated first-content-item rewrite logic in Jun 10, 2026
rewriteEnvelopeTextPayload
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors rewriteEnvelopeTextPayload’s duplicated logic for rewriting content[0].text across the two supported payload shapes ([]map[string]interface{} and []interface{}) by extracting shared helpers, while keeping the same success/failure conditions and shallow-copy (non-mutating) behavior.
Changes:
- Replaced the inlined type-switch rewrite logic in
rewriteEnvelopeTextPayloadwith a call torewriteFirstContentItem(...). - Added
rewriteFirstContentItem(...)andrewriteContentItemText(...)helpers to centralize first-item copy-and-rewrite behavior. - Added targeted unit tests for the new helper path in
TestRewriteFirstContentItem.
Show a summary per file
| File | Description |
|---|---|
| internal/middleware/jqschema.go | Extracts shared “rewrite first content item text” logic into helpers and simplifies envelope rewriting to delegate to the helper. |
| internal/middleware/filter_rewrite_test.go | Adds focused unit tests validating helper behavior for both supported content slice shapes and an invalid first element case. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rewriteEnvelopeTextPayloadcontained duplicated branch logic for rewritingcontent[0].textacross[]map[string]interface{}and[]interface{}payload shapes. This change consolidates that behavior into shared helpers while preserving existing type-specific output behavior and failure conditions.Payload rewrite refactor
rewriteFirstContentItem(contentValue, filteredText)."text"update inrewriteContentItemText(...).rewriteEnvelopeTextPayloadto delegate content handling and keep envelope-copy behavior unchanged.Behavioral guarantees kept explicit
(nil, false).[]interface{}with non-map first element still returns(nil, false).Focused coverage for new helper path
TestRewriteFirstContentItemcases for:[]map[string]interface{}[]interface{}[]interface{}first element type