Skip to content

refactor: simplify and de-duplicate utility helpers#26

Merged
appleboy merged 4 commits into
masterfrom
refactor/simplify-utilities
May 31, 2026
Merged

refactor: simplify and de-duplicate utility helpers#26
appleboy merged 4 commits into
masterfrom
refactor/simplify-utilities

Conversation

@appleboy

@appleboy appleboy commented May 30, 2026

Copy link
Copy Markdown
Owner

Summary

Quality-only cleanups from a /simplify pass over the library. No public API changes — all tests pass. One small, intentional behavior change (see below).

  • array: Contains now delegates to the stdlib slices.Contains instead of a hand-rolled loop (same signature/behavior).
  • bytesconv: Removed bytesconv_1.19.go (//go:build !go1.20), which is unreachable under the module's go 1.25.10 requirement. Dropped the now-always-true go1.20 build tag from the survivor and renamed it to bytesconv.go.
  • convert: ToString gains a typed fast path for string/*string, avoiding fmt.Sprintf reflection on the most common input. Collapsed three if value != 0 { return true } return false blocks in ToBool to return value != 0.
  • file: Extracted the two duplicated close-error defer closures in Copy into a single closeFile(f, role) helper.

Behavior change

  • file.Copy — close-error diagnostics now go to stderr (fmt.Fprintf(os.Stderr, ...)) instead of stdout. Previously these messages were printed to stdout, which could corrupt a caller's intended stdout data (e.g. when piping). This is an intentional fix; the only observable difference is the stream these diagnostic lines are written to.

Verification

  • go build ./... — clean
  • go vet ./... — clean
  • go test ./... — all packages pass

Out of scope (flagged for a follow-up /code-review)

The simplify pass also surfaced potential correctness/behavior issues that were intentionally not touched here: ToFloat/ToBool have narrower numeric/pointer type coverage than ToInt (e.g. ToFloat(int64(...)) returns 0.0). These are behavior changes and belong in a separate review.

🤖 Generated with Claude Code

- Delegate array Contains to the standard library slices package
- Remove the unreachable pre-Go-1.20 bytesconv build-tag file
- Add typed fast paths for string conversion to avoid reflection
- Collapse redundant boolean return blocks in ToBool
- Extract a shared helper for file close error logging
Copilot AI review requested due to automatic review settings May 30, 2026 06:19

Copilot AI left a comment

Copy link
Copy Markdown

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 performs internal refactors and small simplifications across utility packages, aiming to reduce duplication and rely on standard library helpers where possible.

Changes:

  • array.Contains now delegates to slices.Contains.
  • bytesconv removes the Go 1.19 fallback file and drops the go1.20 build tag from the remaining implementation.
  • convert.ToString adds typed fast paths for string/*string, and convert.ToBool is simplified with direct boolean expressions.
  • file.Copy de-duplicates close-error handling via a shared closeFile helper.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
file/file.go Extracts duplicated deferred close-error handling into closeFile.
convert/convert.go Adds string fast path to ToString; simplifies ToBool numeric checks.
bytesconv/bytesconv.go Removes the Go version build constraint from the remaining implementation.
bytesconv/bytesconv_1.19.go Deletes the Go 1.19-specific implementation file.
array/array.go Replaces manual loop with slices.Contains.
Comments suppressed due to low confidence (1)

bytesconv/bytesconv.go:3

  • Dropping the go1.20 build tag here (and removing the !go1.20 implementation) effectively removes the version-gated selection described in bytesconv/README.md (which still mentions Go 1.19 support/build tags). If Go <1.20 support is no longer intended, the docs should be updated accordingly; otherwise, the 1.19 implementation/build tags need to remain.

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

Comment thread file/file.go Outdated
Comment thread convert/convert.go
- Clarify closeFile doc comment to reflect stdout printing
- Add ToString test cases for string and string pointer branches
- Drop stale Go 1.19 build-tag references from bytesconv README

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread file/file.go Outdated
- Redirect closeFile diagnostics to stderr to avoid corrupting piped stdout

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread file/file.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread file/file.go
- Cover successful copy, existing-destination error, missing-source error
- Cover non-regular-file source error

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

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

@appleboy
appleboy merged commit e13a05c into master May 31, 2026
14 checks passed
@appleboy
appleboy deleted the refactor/simplify-utilities branch May 31, 2026 01:07
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.

2 participants