Skip to content

feat: add parameter coverage for more operations - #2568

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:feature/channel_override_trim_prefix
Jan 3, 2026
Merged

feat: add parameter coverage for more operations#2568
seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:feature/channel_override_trim_prefix

Conversation

@seefs001

@seefs001 seefs001 commented Jan 3, 2026

Copy link
Copy Markdown
Collaborator

Add parameter coverage for the operations: copy, trim_prefix, trim_suffix, ensure_prefix, ensure_suffix, trim_space, to_lower, to_upper, replace, and regex_replace
example: https://github.com/seefs001/new-api/blob/817da8d73c1fbd527bc8405cdbd6b9ac6a338221/relay/common/override_test.go
#2496

Summary by CodeRabbit

Release Notes

  • New Features

    • Added 10 new string operation modes for parameter overrides: trim prefix/suffix, ensure prefix/suffix, case transformations (lowercase/uppercase), trim whitespace, replace text, and regex-based replacement.
    • Introduced copy operation to transfer values between parameter paths.
    • Enhanced error handling with contextual messages for invalid operations.
  • Tests

    • Added comprehensive test coverage for all parameter override operations and edge cases.

✏️ Tip: You can customize this high-level summary in your review settings.

…rim_suffix, ensure_prefix, ensure_suffix, trim_space, to_lower, to_upper, replace, and regex_replace
@coderabbitai

coderabbitai Bot commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The changes expand the parameter override system by introducing ten new operation modes (copy, trim_prefix, trim_suffix, ensure_prefix, ensure_suffix, trim_space, to_lower, to_upper, replace, regex_replace) with corresponding helper functions for string transformations and value copying, alongside comprehensive error handling and a full test suite.

Changes

Cohort / File(s) Summary
Implementation
relay/common/override.go
Adds 10 new ParamOperation modes; introduces copyValue function for value duplication with existence checks; implements 5 string transformation helpers (trimStringValue, ensureStringAffix, transformStringValue, replaceStringValue, regexReplaceStringValue); enhances error handling with operation-specific contextual messages for unknown operations, missing sources, and invalid regex patterns.
Tests
relay/common/override_test.go
Comprehensive test suite covering all new operations including trim/ensure/case-transform/replace variants, copy, regex_replace with invalid patterns, conditional execution with OR/AND, negative index navigation, nested paths, error scenarios for missing fields, and auxiliary assertJSONEqual helper for deep JSON equality validation.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰✨ A dozen new tricks in the override warren—
Trimming, copying, transforming with care,
From prefix to suffix, from lower to upper we soar,
Regex patterns dance where strings once lay bare,
The parameter garden now blooms evermore! 🌱

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately describes the main change: adding support for new parameter override operations (copy, trim_prefix, trim_suffix, ensure_prefix, ensure_suffix, trim_space, to_lower, to_upper, replace, regex_replace).
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 0

🧹 Nitpick comments (2)
relay/common/override.go (2)

343-352: Validation for "copy" is inconsistent with "move".

The copy operation validates that from and to are required (lines 347-349), but the move operation (lines 343-345) lacks similar validation. If op.From or op.To is empty for move, moveValue will proceed with empty paths, potentially causing unexpected behavior.

🔎 Proposed fix to add validation for move operation
 		case "move":
+			if op.From == "" || op.To == "" {
+				return "", fmt.Errorf("move from/to is required")
+			}
 			opFrom := processNegativeIndex(result, op.From)
 			opTo := processNegativeIndex(result, op.To)
 			result, err = moveValue(result, opFrom, opTo)

525-538: Consider architectural caching for dynamic regex patterns if performance becomes a concern.

The regex is compiled on every call to regexReplaceStringValue. Since the function accepts user-provided patterns, simple package-level caching isn't feasible—a cache mechanism like sync.Map would be required. Only implement caching if you observe repeated patterns in production.

For security, the code already validates patterns via regexp.Compile error handling, which mitigates basic ReDoS risks. If stricter ReDoS protection is needed for security-sensitive contexts, consider adding a timeout or complexity limit for pattern compilation.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43c671b and 817da8d.

📒 Files selected for processing (2)
  • relay/common/override.go
  • relay/common/override_test.go
🧰 Additional context used
🧬 Code graph analysis (1)
relay/common/override_test.go (2)
relay/common/override.go (1)
  • ApplyParamOverride (35-49)
common/json.go (1)
  • Unmarshal (9-11)
🔇 Additional comments (18)
relay/common/override_test.go (11)

1-28: LGTM!

Good test structure with clear comments showing the expected operation format. The test correctly validates the trim_prefix operation removes the specified prefix from the model string.


30-70: LGTM!

Good coverage for trim_suffix and the no-op scenario where the prefix doesn't exist. These tests properly verify idempotent behavior.


72-89: LGTM!

Proper error handling test - validates that trim_prefix requires a value parameter.


91-171: LGTM!

Comprehensive tests for replace and regex_replace operations, including validation of required parameters (from for replace, pattern for regex_replace).


173-272: LGTM!

Good coverage for core operations: delete, set, set with keep_origin, move, and move with missing source. Tests properly validate both successful operations and error conditions.


274-365: LGTM!

Thorough tests for prepend/append operations on both strings and arrays, plus object merge behavior with and without keep_origin. The array flattening behavior on append (line 310) is properly tested.


367-511: LGTM!

Excellent coverage for conditional execution including OR/AND logic, invert flag, pass_missing_key behavior, and context-based evaluation. These tests ensure the conditional system works correctly across various scenarios.


513-610: LGTM!

Good tests for negative index path handling and copy operation including error cases (missing source, missing from/to fields).


612-713: LGTM!

Comprehensive tests for ensure_prefix and ensure_suffix operations, including no-op scenarios when the affix already exists and validation that value is required.


715-773: LGTM!

Good coverage for trim_space, to_lower, and to_upper transformations. The trim_space test on line 718 properly tests whitespace including newline character.


775-791: LGTM!

Clean helper function using t.Helper() for proper test failure attribution. Using reflect.DeepEqual after unmarshaling is the correct approach for JSON comparison regardless of key ordering.

relay/common/override.go (7)

24-33: LGTM!

Good documentation of all supported modes in the struct comment. The struct properly supports all the new operations with the existing fields.


357-374: LGTM!

Clean dispatch to the new string operation helpers. Good use of the existing op.From and op.To fields for replace operations rather than introducing new fields.


397-403: LGTM!

Clean implementation of copyValue. Properly checks source existence and returns a contextual error message.


458-476: LGTM!

Good implementation with proper type checking and nil value validation. Using fmt.Sprintf("%v", value) handles various input types gracefully.


478-504: LGTM!

Proper implementation with both nil check and empty string validation for ensure operations. The logic correctly handles both prefix and suffix cases with early return when affix already exists.


506-512: LGTM!

Elegant use of higher-order function to handle multiple string transformations (TrimSpace, ToLower, ToUpper) with a single implementation.


514-523: LGTM!

Proper validation that from is required. Good use of strings.ReplaceAll for the replacement.

@seefs001
seefs001 merged commit 6b1f8b8 into QuantumNous:main Jan 3, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
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