Skip to content

refactor: extract DeduplicateStrings helper to eliminate duplicated dedup logic#2123

Merged
lpcox merged 2 commits intomainfrom
copilot/duplicate-code-string-deduplication
Mar 19, 2026
Merged

refactor: extract DeduplicateStrings helper to eliminate duplicated dedup logic#2123
lpcox merged 2 commits intomainfrom
copilot/duplicate-code-string-deduplication

Conversation

Copy link
Contributor

Copilot AI commented Mar 18, 2026

The trim→skip-empty→deduplicate→optional-sort pattern was reimplemented inline in three separate packages, with behavioral drift already present (silent skip vs. error on duplicate, whitespace rejection in one but not others).

Changes

  • internal/strutil/deduplicate.go — new DeduplicateStrings(input []string, sorted bool) []string canonical helper
  • internal/strutil/deduplicate_test.go — table-driven tests covering nil input, trim, dedup, sort, and order preservation
  • internal/difc/sink_server_ids.go — replace 14-line inline loop with strutil.DeduplicateStrings(serverIDs, true); log when entries are dropped
  • internal/cmd/flags_difc.goparseDIFCSinkServerIDs: keep the embedded-whitespace error check (not handled by the helper), delegate dedup to strutil.DeduplicateStrings
  • internal/config/guard_policy.go — unchanged; normalizeAndValidateScopeArray returns an error on duplicates rather than silently dropping them, so it doesn't share the same contract

Before / After

// Before — ~15 lines repeated inline in SetSinkServerIDs
unique := make(map[string]struct{}, len(serverIDs))
normalized := make([]string, 0, len(serverIDs))
for _, serverID := range serverIDs {
    trimmed := strings.TrimSpace(serverID)
    if trimmed == "" { continue }
    if _, exists := unique[trimmed]; exists {
        duplicateIDs = append(duplicateIDs, trimmed)
        continue
    }
    unique[trimmed] = struct{}{}
    normalized = append(normalized, trimmed)
}
sort.Strings(normalized)

// After
normalized := strutil.DeduplicateStrings(serverIDs, true)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build1731765213/b332/launcher.test /tmp/go-build1731765213/b332/launcher.test -test.testlogfile=/tmp/go-build1731765213/b332/testlog.txt -test.paniconexit0 -test.timeout=10m0s conf�� 1.4.0/auth/auth.go 1.4.0/auth/client.go x_amd64/compile (dns block)
    • Triggering command: /tmp/go-build908205860/b328/launcher.test /tmp/go-build908205860/b328/launcher.test -test.testlogfile=/tmp/go-build908205860/b328/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --version y 06bc07c272bb18e2 ntime.v2.task/mosystemd-run containerd.sock x_amd64/vet ctl insp�� .go {{.Config.OpenSt--property=CPUQuota=5% /usr/libexec/doc--property=MemoryHigh=170M 64/src/net ache/go/1.25.8/xinfo x_amd64/vet 45045761271a86c7-collect-logs (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build1731765213/b317/config.test /tmp/go-build1731765213/b317/config.test -test.testlogfile=/tmp/go-build1731765213/b317/testlog.txt -test.paniconexit0 -test.timeout=10m0s conf�� .go (dns block)
    • Triggering command: /tmp/go-build908205860/b313/config.test /tmp/go-build908205860/b313/config.test -test.testlogfile=/tmp/go-build908205860/b313/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 9c9123b84b4d4e8c -tests ash b8a4de3a75e76448grep fe9 x_amd64/compile fe9/init.pid --ve�� c48959766ef0f63f b8a4de3a75e764483c627dce6b038343b5b6efe353b9d178502 "CURL_CA_BUNDLE=/--systemd-cgroup b8a4de3a75e76448git (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build1731765213/b332/launcher.test /tmp/go-build1731765213/b332/launcher.test -test.testlogfile=/tmp/go-build1731765213/b332/testlog.txt -test.paniconexit0 -test.timeout=10m0s conf�� 1.4.0/auth/auth.go 1.4.0/auth/client.go x_amd64/compile (dns block)
    • Triggering command: /tmp/go-build908205860/b328/launcher.test /tmp/go-build908205860/b328/launcher.test -test.testlogfile=/tmp/go-build908205860/b328/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --version y 06bc07c272bb18e2 ntime.v2.task/mosystemd-run containerd.sock x_amd64/vet ctl insp�� .go {{.Config.OpenSt--property=CPUQuota=5% /usr/libexec/doc--property=MemoryHigh=170M 64/src/net ache/go/1.25.8/xinfo x_amd64/vet 45045761271a86c7-collect-logs (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build1731765213/b332/launcher.test /tmp/go-build1731765213/b332/launcher.test -test.testlogfile=/tmp/go-build1731765213/b332/testlog.txt -test.paniconexit0 -test.timeout=10m0s conf�� 1.4.0/auth/auth.go 1.4.0/auth/client.go x_amd64/compile (dns block)
    • Triggering command: /tmp/go-build908205860/b328/launcher.test /tmp/go-build908205860/b328/launcher.test -test.testlogfile=/tmp/go-build908205860/b328/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --version y 06bc07c272bb18e2 ntime.v2.task/mosystemd-run containerd.sock x_amd64/vet ctl insp�� .go {{.Config.OpenSt--property=CPUQuota=5% /usr/libexec/doc--property=MemoryHigh=170M 64/src/net ache/go/1.25.8/xinfo x_amd64/vet 45045761271a86c7-collect-logs (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build1731765213/b341/mcp.test /tmp/go-build1731765213/b341/mcp.test -test.testlogfile=/tmp/go-build1731765213/b341/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build1731765213/b271/vet.cfg flib/difflib.go main x_amd64/cgo (dns block)
    • Triggering command: /tmp/go-build908205860/b337/mcp.test /tmp/go-build908205860/b337/mcp.test -test.testlogfile=/tmp/go-build908205860/b337/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true y x_amd64/vet by/027a141771b2cb8a4de3a75e764483c627dce6b038343b5b6efe353b9d178502/log.json azero@v1.11.0/bugrep azero@v1.11.0/ca-qE x_amd64/vet 709/log.json 92b4�� by/027a141771b2cb8a4de3a75e764483c627dce6b038343b5b6efe353b9d178502 --bundle by/027a141771b2cb8a4de3a75e764483c627dce6b038343b5b6efe353b9d178502/init.pid 6efe353b9d178502bash 77a1358e1f687d17/usr/bin/runc x_amd64/vet table.d/chrony-onoffline (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

…icated dedup logic

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] [duplicate-code] Refactor duplicate string deduplication pattern refactor: extract DeduplicateStrings helper to eliminate duplicated dedup logic Mar 18, 2026
Copilot AI requested a review from lpcox March 18, 2026 23:56
@lpcox lpcox marked this pull request as ready for review March 19, 2026 00:08
Copilot AI review requested due to automatic review settings March 19, 2026 00:08
@lpcox lpcox merged commit a2a2ca6 into main Mar 19, 2026
11 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-string-deduplication branch March 19, 2026 00:08
Copy link
Contributor

Copilot AI left a comment

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 centralizes a common “trim → skip-empty → deduplicate → optional sort” normalization pattern into a single canonical helper to eliminate duplicated implementations and reduce behavioral drift across packages.

Changes:

  • Added strutil.DeduplicateStrings helper implementing trim/skip-empty/dedup with optional sorting.
  • Added table-driven tests covering trimming, empty handling, dedup behavior, order preservation, and sorting.
  • Refactored DIFC sink server ID normalization and CLI flag parsing to use the shared helper.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
internal/strutil/deduplicate.go Introduces the shared dedup/normalize helper with optional sorting.
internal/strutil/deduplicate_test.go Adds unit tests validating helper behavior across key edge cases.
internal/difc/sink_server_ids.go Replaces inline normalization/sort with the shared helper and logs when items are dropped.
internal/cmd/flags_difc.go Keeps embedded-whitespace validation, delegates dedup to the shared helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

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: String deduplication/normalization repeated in 3 packages

3 participants