Validate internal decisions and reconcile exact model references
This commit is contained in:
@@ -6,8 +6,8 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber"
|
||||
)
|
||||
|
||||
type SourceSegment struct {
|
||||
@@ -114,60 +114,13 @@ func extractSegments(raw []byte) (json.RawMessage, error) {
|
||||
type sourceID int
|
||||
|
||||
func (id *sourceID) UnmarshalJSON(raw []byte) error {
|
||||
spelling := string(raw)
|
||||
if value, err := strconv.Atoi(spelling); err == nil {
|
||||
*id = sourceID(value)
|
||||
return nil
|
||||
}
|
||||
if value, ok := integralDecimal(spelling); ok {
|
||||
if value, ok := jsonnumber.Int(string(raw)); ok {
|
||||
*id = sourceID(value)
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(raw, (*int)(id))
|
||||
}
|
||||
|
||||
func integralDecimal(value string) (int, bool) {
|
||||
if value == "" || (value[0] != '-' && (value[0] < '0' || value[0] > '9')) || !strings.ContainsAny(value, ".eE") {
|
||||
return 0, false
|
||||
}
|
||||
digits, negative := strings.CutPrefix(value, "-")
|
||||
var exponentText string
|
||||
if i := strings.IndexAny(digits, "eE"); i >= 0 {
|
||||
exponentText, digits = digits[i+1:], digits[:i]
|
||||
}
|
||||
whole, fraction, _ := strings.Cut(digits, ".")
|
||||
digits = strings.TrimLeft(whole+fraction, "0")
|
||||
if digits == "" {
|
||||
return 0, true
|
||||
}
|
||||
var exponent int
|
||||
if exponentText != "" {
|
||||
var err error
|
||||
exponent, err = strconv.Atoi(exponentText)
|
||||
if err != nil || exponent < -len(digits) || exponent > len(fraction)+19 {
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
shift := exponent - len(fraction)
|
||||
if shift < 0 {
|
||||
// A negative shift is integral only if every discarded digit is zero.
|
||||
if -shift > len(digits)-len(strings.TrimRight(digits, "0")) {
|
||||
return 0, false
|
||||
}
|
||||
digits = digits[:len(digits)+shift]
|
||||
} else {
|
||||
if len(digits)+shift > 19 {
|
||||
return 0, false
|
||||
}
|
||||
digits += strings.Repeat("0", shift)
|
||||
}
|
||||
if negative {
|
||||
digits = "-" + digits
|
||||
}
|
||||
integer, err := strconv.Atoi(digits)
|
||||
return integer, err == nil
|
||||
}
|
||||
|
||||
func validateSourceSegments(segments []SourceSegment) error {
|
||||
for i, s := range segments {
|
||||
segLabel := segmentLabel(i, s.ID)
|
||||
|
||||
Reference in New Issue
Block a user