Skip to content

Refactor duplicated first-content-item rewrite logic in rewriteEnvelopeTextPayload#7324

Merged
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-fix
Jun 10, 2026
Merged

Refactor duplicated first-content-item rewrite logic in rewriteEnvelopeTextPayload#7324
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-fix

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

rewriteEnvelopeTextPayload contained duplicated branch logic for rewriting content[0].text across []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

    • Extracted shared content mutation flow into rewriteFirstContentItem(contentValue, filteredText).
    • Centralized first-item map copy + "text" update in rewriteContentItemText(...).
    • Simplified rewriteEnvelopeTextPayload to delegate content handling and keep envelope-copy behavior unchanged.
  • Behavioral guarantees kept explicit

    • Empty content still returns (nil, false).
    • []interface{} with non-map first element still returns (nil, false).
    • Shallow-copy semantics remain intact: original input maps/slices are not mutated.
  • Focused coverage for new helper path

    • Added TestRewriteFirstContentItem cases for:
      • []map[string]interface{}
      • []interface{}
      • invalid []interface{} first element type
rewrittenContent, ok := rewriteFirstContentItem(contentValue, filteredText)
if !ok {
    return nil, false
}
rewrittenMap["content"] = rewrittenContent
return rewrittenMap, true

Copilot AI changed the title [WIP] Refactor duplicate code in rewriteEnvelopeTextPayload Refactor duplicated first-content-item rewrite logic in rewriteEnvelopeTextPayload Jun 10, 2026
Copilot finished work on behalf of lpcox June 10, 2026 18:08
Copilot AI requested a review from lpcox June 10, 2026 18:08
@lpcox lpcox marked this pull request as ready for review June 10, 2026 19:50
Copilot AI review requested due to automatic review settings June 10, 2026 19:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 rewriteEnvelopeTextPayload with a call to rewriteFirstContentItem(...).
  • Added rewriteFirstContentItem(...) and rewriteContentItemText(...) 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

@lpcox lpcox merged commit 6f5d9b2 into main Jun 10, 2026
45 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix branch June 10, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Pattern: Branch duplication in rewriteEnvelopeTextPayload content slice handling

3 participants