refactor: add strutil.InterfaceToIntString to deduplicate JSON numeric extraction#7937
Merged
Merged
Conversation
…N numeric extraction - Add strutil.InterfaceToIntString (float64 + json.Number → integer string) with explicit int64 range guard and json.Number integer validation - Replace inline type-switch in server/difc_log.go with the helper - Replace inline float64-only branch in proxy/router.go with the helper, adding json.Number support that was previously missing - Update difc_log_test: non-integer floats now return "" (consistent with router.go) - Add unit tests for the new helper and json.Number cases in router tests Closes #7919
- Improve comment in numbers.go: clarify why < is used for lower bound (float64(math.MinInt64) is exactly representable as -(2^63)) - Restore numeric value in router.go debug log message for better diagnostics
Copilot
AI
changed the title
[WIP] Refactor duplicate JSON numeric extraction code pattern
refactor: add strutil.InterfaceToIntString to deduplicate JSON numeric extraction
Jun 22, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a shared strutil.InterfaceToIntString helper to centralize “JSON-decoded number → integer string” extraction, replacing several ad-hoc type switches and aligning behavior across server logging and proxy routing while adding targeted unit tests.
Changes:
- Added
strutil.InterfaceToIntString(v interface{}) (string, bool)to safely accept only in-range integerfloat64and integerjson.Numberinputs. - Refactored DIFC filtered-item logging and proxy routing to use the shared helper, eliminating duplicated numeric parsing logic.
- Added/updated unit tests to cover integer vs non-integer numeric cases and
json.Numbersupport.
Show a summary per file
| File | Description |
|---|---|
| internal/strutil/numbers.go | Adds the shared numeric interface→int-string conversion helper with range/non-integer rejection. |
| internal/strutil/numbers_test.go | Introduces unit coverage for float64 and json.Number integer/non-integer and out-of-range cases. |
| internal/server/difc_log.go | Uses the helper to populate filtered-item number without truncating non-integer floats. |
| internal/server/difc_log_test.go | Updates expectations for decimal float handling (now rejected rather than truncated). |
| internal/proxy/router.go | Refactors route argument numeric extraction to use the helper (and should preserve prior validation/error semantics). |
| internal/proxy/rest_backend_caller_tool_test.go | Adds a json.Number test case to ensure numeric args are accepted beyond float64. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 6/6 changed files
- Comments generated: 3
Comment on lines
+28
to
+33
| case json.Number: | ||
| // Validate that the json.Number represents a valid integer. | ||
| if _, err := n.Int64(); err != nil { | ||
| return "", false // non-integer or out-of-range json.Number | ||
| } | ||
| return n.String(), true |
Comment on lines
+48
to
+51
| t.Run("float64 out of int64 range returns false", func(t *testing.T) { | ||
| t.Parallel() | ||
| // 1e20 exceeds int64 max; round-trip check catches the overflow | ||
| s, ok := InterfaceToIntString(float64(1e20)) |
Comment on lines
79
to
83
| if number == "" { | ||
| if n, ok := argsMap[numberKey].(float64); ok { | ||
| logRouter.Printf("extractOwnerRepoNumber: %s provided as float64=%v, parsing as integer for tool=%s", numberKey, n, toolName) | ||
| const maxInt64AsFloat = float64(int64(^uint64(0) >> 1)) | ||
| if n < 0 || n > maxInt64AsFloat { | ||
| logRouter.Printf("extractOwnerRepoNumber: %s float64=%v out of int64 range [0,%v] for tool=%s", numberKey, n, maxInt64AsFloat, toolName) | ||
| err = fmt.Errorf("%s: invalid %s (out of range)", toolName, numberKey) | ||
| return | ||
| } | ||
| i := int64(n) | ||
| if n != float64(i) { | ||
| logRouter.Printf("extractOwnerRepoNumber: %s float64=%v is not a whole number for tool=%s", numberKey, n, toolName) | ||
| err = fmt.Errorf("%s: invalid %s (expected integer)", toolName, numberKey) | ||
| return | ||
| } | ||
| number = fmt.Sprintf("%d", i) | ||
| if s, ok := strutil.InterfaceToIntString(argsMap[numberKey]); ok { | ||
| logRouter.Printf("extractOwnerRepoNumber: %s provided as numeric=%v, parsing as integer for tool=%s", numberKey, argsMap[numberKey], toolName) | ||
| number = s | ||
| } |
Collaborator
|
@copilot address review feedback |
Contributor
Author
Addressed all three review items in commit
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three packages independently inlined
float64/json.Number→ integer-string type-switches, with inconsistent safety:difc_log.gosilently truncated non-integer floats (123.9→"123"), whilerouter.govalidated but lackedjson.Numbersupport entirely.Changes
internal/strutil/numbers.go— newInterfaceToIntString(v interface{}) (string, bool)helper:int64range guard onfloat64before conversion (avoids implementation-defined overflow)json.Number.Int64()to reject non-integerjson.Numbervalues("", false)for all non-integer or out-of-range inputsinternal/server/difc_log.go— replaces 6-line switch withstrutil.InterfaceToIntString; non-integer floats now return""(bug fix, consistent withrouter.go)internal/proxy/router.go— replaces 12-line float64-only block with the helper, gainingjson.Numbersupport for freeinternal/strutil/numbers_test.go— new unit tests covering all numeric/non-numeric input types and edge casesinternal/server/difc_log_test.go/internal/proxy/rest_backend_caller_tool_test.go— updated/added tests reflecting new behavior andjson.Numbercoverage