Skip to content

Refactor TimeoutPositive to reuse TimeoutMinimum and remove duplicated validator logic#7105

Merged
lpcox merged 4 commits into
mainfrom
copilot/duplicate-code-fix-validator-functions
Jun 6, 2026
Merged

Refactor TimeoutPositive to reuse TimeoutMinimum and remove duplicated validator logic#7105
lpcox merged 4 commits into
mainfrom
copilot/duplicate-code-fix-validator-functions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 6, 2026

internal/config/rules/rules.go had overlapping >= 1 validation logic across TimeoutPositive and PositiveInteger, with TimeoutPositive effectively duplicating functionality already represented by TimeoutMinimum(..., 1, ...). This change consolidates the timeout path onto the shared minimum validator while keeping the existing API surface intact.

  • Validator deduplication

    • Replaced TimeoutPositive’s inline validation/error construction with delegation to TimeoutMinimum(timeout, 1, fieldName, jsonPath).
    • Removes duplicated conditional/log/error-assembly logic in the timeout validator path.
  • API and call-site stability

    • Kept TimeoutPositive function signature and usage unchanged.
    • No changes required at existing call sites (e.g., gateway startup timeout validation).
  • Documentation alignment

    • Updated TimeoutPositive doc comments to explicitly describe wrapper behavior and delegated error/suggestion formatting.
func TimeoutPositive(timeout int, fieldName, jsonPath string) *ValidationError {
	return TimeoutMinimum(timeout, 1, fieldName, jsonPath)
}

Copilot AI changed the title [WIP] Refactor overlapping validators TimeoutPositive and PositiveInteger Refactor TimeoutPositive to reuse TimeoutMinimum and remove duplicated validator logic Jun 6, 2026
Copilot finished work on behalf of lpcox June 6, 2026 16:26
Copilot AI requested a review from lpcox June 6, 2026 16:26
@lpcox lpcox marked this pull request as ready for review June 6, 2026 16:26
Copilot AI review requested due to automatic review settings June 6, 2026 16:26
Copy link
Copy Markdown
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 refactors the configuration validation rules to reduce duplicated timeout validation logic by making TimeoutPositive delegate to the shared TimeoutMinimum validator, while keeping the TimeoutPositive API unchanged.

Changes:

  • Replaced TimeoutPositive’s inline >= 1 validation with TimeoutMinimum(timeout, 1, ...).
  • Updated TimeoutPositive doc comments to describe its wrapper/delegation behavior.
Show a summary per file
File Description
internal/config/rules/rules.go Refactors TimeoutPositive to reuse TimeoutMinimum and updates associated doc comments.

Copilot's findings

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread internal/config/rules/rules.go Outdated
Comment on lines +107 to +112
// TimeoutPositive validates that a timeout value is at least 1.
// Returns nil if valid, *ValidationError if invalid.
// It is a convenience wrapper around TimeoutMinimum with min=1.
// Error message and suggestion formatting are provided by TimeoutMinimum.
func TimeoutPositive(timeout int, fieldName, jsonPath string) *ValidationError {
log.Printf("Validating timeout: field=%s, value=%d, jsonPath=%s", fieldName, timeout, jsonPath)
if timeout < 1 {
log.Printf("Timeout validation failed: %s=%d is not positive", fieldName, timeout)
return &ValidationError{
Field: fieldName,
Message: fmt.Sprintf("%s must be at least 1, got %d", fieldName, timeout),
JSONPath: jsonPath,
Suggestion: "Use a positive number of seconds (e.g., 30)",
}
}
return nil
return TimeoutMinimum(timeout, 1, fieldName, jsonPath)
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Jun 6, 2026

@copilot address review feedback

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jun 6, 2026

@copilot address review feedback

Fixed in the latest commit. TimeoutPositive now overrides Suggestion on the error returned by TimeoutMinimum, restoring the original wording ("Use a positive number of seconds (e.g., 30)") while still delegating all validation logic. Tests updated to assert the suggestion text.

Copilot finished work on behalf of lpcox June 6, 2026 16:39
@lpcox lpcox merged commit 76b4550 into main Jun 6, 2026
16 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix-validator-functions branch June 6, 2026 16:49
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: TimeoutPositive and PositiveInteger Overlapping Validators

3 participants