mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Return error on input in GetValueFromIntOrPercent
This commit is contained in:
parent
dbe3b1a3b3
commit
3d7c589f05
@ -18,6 +18,7 @@ package intstr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@ -142,7 +143,17 @@ func (intstr *IntOrString) Fuzz(c fuzz.Continue) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ValueOrDefault(intOrPercent *IntOrString, defaultValue IntOrString) *IntOrString {
|
||||||
|
if intOrPercent == nil {
|
||||||
|
return &defaultValue
|
||||||
|
}
|
||||||
|
return intOrPercent
|
||||||
|
}
|
||||||
|
|
||||||
func GetValueFromIntOrPercent(intOrPercent *IntOrString, total int, roundUp bool) (int, error) {
|
func GetValueFromIntOrPercent(intOrPercent *IntOrString, total int, roundUp bool) (int, error) {
|
||||||
|
if intOrPercent == nil {
|
||||||
|
return 0, errors.New("nil value for IntOrString")
|
||||||
|
}
|
||||||
value, isPercent, err := getIntOrPercentValue(intOrPercent)
|
value, isPercent, err := getIntOrPercentValue(intOrPercent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("invalid value for IntOrString: %v", err)
|
return 0, fmt.Errorf("invalid value for IntOrString: %v", err)
|
||||||
|
@ -174,3 +174,10 @@ func TestGetValueFromIntOrPercent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetValueFromIntOrPercentNil(t *testing.T) {
|
||||||
|
_, err := GetValueFromIntOrPercent(nil, 0, false)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected error got none")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user