mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-06 11:42:14 +00:00
ParseOrDie -> MustParse; stop returning pointer
This commit is contained in:
@@ -102,14 +102,14 @@ const (
|
||||
DecimalSI = Format("DecimalSI") // e.g., 12M (12 * 10^6)
|
||||
)
|
||||
|
||||
// ParseOrDie turns the given string into a quantity or panics; for tests
|
||||
// MustParse turns the given string into a quantity or panics; for tests
|
||||
// or others cases where you know the string is valid.
|
||||
func ParseOrDie(str string) *Quantity {
|
||||
func MustParse(str string) Quantity {
|
||||
q, err := ParseQuantity(str)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot parse '%v': %v", str, err))
|
||||
}
|
||||
return q
|
||||
return *q
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -403,10 +403,7 @@ func (qf qFlag) String() string {
|
||||
// QuantityFlag is a helper that makes a quantity flag (using standard flag package).
|
||||
// Will panic if defaultValue is not a valid quantity.
|
||||
func QuantityFlag(flagName, defaultValue, description string) *Quantity {
|
||||
q, err := ParseQuantity(defaultValue)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("can't use %v as a quantity: %v", defaultValue, err))
|
||||
}
|
||||
flag.Var(qFlag{q}, flagName, description)
|
||||
return q
|
||||
q := MustParse(defaultValue)
|
||||
flag.Var(qFlag{&q}, flagName, description)
|
||||
return &q
|
||||
}
|
||||
|
@@ -38,17 +38,17 @@ func ExampleFormat() {
|
||||
// cores = 5300m
|
||||
}
|
||||
|
||||
func ExampleParseOrDie() {
|
||||
memorySize := resource.ParseOrDie("5Gi")
|
||||
func ExampleMustParse() {
|
||||
memorySize := resource.MustParse("5Gi")
|
||||
fmt.Printf("memorySize = %v (%v)\n", memorySize.Value(), memorySize.Format)
|
||||
|
||||
diskSize := resource.ParseOrDie("5G")
|
||||
diskSize := resource.MustParse("5G")
|
||||
fmt.Printf("diskSize = %v (%v)\n", diskSize.Value(), diskSize.Format)
|
||||
|
||||
cores := resource.ParseOrDie("5300m")
|
||||
cores := resource.MustParse("5300m")
|
||||
fmt.Printf("milliCores = %v (%v)\n", cores.MilliValue(), cores.Format)
|
||||
|
||||
cores2 := resource.ParseOrDie("5.4")
|
||||
cores2 := resource.MustParse("5.4")
|
||||
fmt.Printf("milliCores = %v (%v)\n", cores2.MilliValue(), cores2.Format)
|
||||
|
||||
// Output:
|
||||
|
Reference in New Issue
Block a user