mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Make NestedNumberAsFloat64 accuracy test architecture-neutral.
NestedNumberAsFloat64 will convert int64s to float64 only if the int64 value can be represented exactly by a float64. The original test for this property used a roundtrip conversion from int64 to float64 and back, and the behavior of these conversions is inconsistent across architectures.
This commit is contained in:
parent
99cc395e2b
commit
e413e026a3
@ -20,6 +20,7 @@ import (
|
|||||||
gojson "encoding/json"
|
gojson "encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@ -136,10 +137,11 @@ func NestedNumberAsFloat64(obj map[string]interface{}, fields ...string) (float6
|
|||||||
}
|
}
|
||||||
switch x := val.(type) {
|
switch x := val.(type) {
|
||||||
case int64:
|
case int64:
|
||||||
if x != int64(float64(x)) {
|
f, accuracy := big.NewInt(x).Float64()
|
||||||
|
if accuracy != big.Exact {
|
||||||
return 0, false, fmt.Errorf("%v accessor error: int64 value %v cannot be losslessly converted to float64", jsonPath(fields), x)
|
return 0, false, fmt.Errorf("%v accessor error: int64 value %v cannot be losslessly converted to float64", jsonPath(fields), x)
|
||||||
}
|
}
|
||||||
return float64(x), true, nil
|
return f, true, nil
|
||||||
case float64:
|
case float64:
|
||||||
return x, true, nil
|
return x, true, nil
|
||||||
default:
|
default:
|
||||||
|
Loading…
Reference in New Issue
Block a user