Accept equivalent integer references in model responses

This commit is contained in:
2026-11-17 04:22:20 +00:00
parent 2d93e8ae7c
commit 44c767f285
10 changed files with 52 additions and 27 deletions

View File

@@ -2,10 +2,22 @@
package jsonnumber
import (
"encoding/json"
"strconv"
"strings"
)
// Integer accepts exact JSON integer values within the native integer range.
type Integer int
func (n *Integer) UnmarshalJSON(raw []byte) error {
if value, ok := Int(string(raw)); ok {
*n = Integer(value)
return nil
}
return json.Unmarshal(raw, (*int)(n))
}
// Int accepts lexical integers and equivalent decimal and exponent spellings
// in the native integer range. value must be a valid JSON number token.
func Int(value string) (int, bool) {

View File

@@ -143,17 +143,7 @@ func extractSegments(raw []byte) (json.RawMessage, error) {
}
}
// sourceID decodes native integers without routing them through float64.
// Decimal and exponent spellings are accepted if they denote an exact integer.
type sourceID int
func (id *sourceID) UnmarshalJSON(raw []byte) error {
if value, ok := jsonnumber.Int(string(raw)); ok {
*id = sourceID(value)
return nil
}
return json.Unmarshal(raw, (*int)(id))
}
type sourceID = jsonnumber.Integer
func validateSourceSegments(segments []SourceSegment) error {
for i, s := range segments {