mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 15:37:24 +00:00
Refactor IntOrString into a new pkg
pkg/util/intstr is a cleaner encapsulation for this type and supporting functions. No behavioral change.
This commit is contained in:
@@ -19,7 +19,6 @@ package util
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
@@ -34,8 +33,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"k8s.io/kubernetes/pkg/util/intstr"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/google/gofuzz"
|
||||
)
|
||||
|
||||
// For testing, bypass HandleCrash.
|
||||
@@ -133,88 +133,15 @@ func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// IntOrString is a type that can hold an int or a string. When used in
|
||||
// JSON or YAML marshalling and unmarshalling, it produces or consumes the
|
||||
// inner type. This allows you to have, for example, a JSON field that can
|
||||
// accept a name or number.
|
||||
type IntOrString struct {
|
||||
Kind IntstrKind
|
||||
IntVal int
|
||||
StrVal string
|
||||
}
|
||||
|
||||
// IntstrKind represents the stored type of IntOrString.
|
||||
type IntstrKind int
|
||||
|
||||
const (
|
||||
IntstrInt IntstrKind = iota // The IntOrString holds an int.
|
||||
IntstrString // The IntOrString holds a string.
|
||||
)
|
||||
|
||||
// NewIntOrStringFromInt creates an IntOrString object with an int value.
|
||||
func NewIntOrStringFromInt(val int) IntOrString {
|
||||
return IntOrString{Kind: IntstrInt, IntVal: val}
|
||||
}
|
||||
|
||||
// NewIntOrStringFromString creates an IntOrString object with a string value.
|
||||
func NewIntOrStringFromString(val string) IntOrString {
|
||||
return IntOrString{Kind: IntstrString, StrVal: val}
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaller interface.
|
||||
func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
|
||||
if value[0] == '"' {
|
||||
intstr.Kind = IntstrString
|
||||
return json.Unmarshal(value, &intstr.StrVal)
|
||||
}
|
||||
intstr.Kind = IntstrInt
|
||||
return json.Unmarshal(value, &intstr.IntVal)
|
||||
}
|
||||
|
||||
// String returns the string value, or Itoa's the int value.
|
||||
func (intstr *IntOrString) String() string {
|
||||
if intstr.Kind == IntstrString {
|
||||
return intstr.StrVal
|
||||
}
|
||||
return strconv.Itoa(intstr.IntVal)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaller interface.
|
||||
func (intstr IntOrString) MarshalJSON() ([]byte, error) {
|
||||
switch intstr.Kind {
|
||||
case IntstrInt:
|
||||
return json.Marshal(intstr.IntVal)
|
||||
case IntstrString:
|
||||
return json.Marshal(intstr.StrVal)
|
||||
default:
|
||||
return []byte{}, fmt.Errorf("impossible IntOrString.Kind")
|
||||
}
|
||||
}
|
||||
|
||||
func (intstr *IntOrString) Fuzz(c fuzz.Continue) {
|
||||
if intstr == nil {
|
||||
return
|
||||
}
|
||||
if c.RandBool() {
|
||||
intstr.Kind = IntstrInt
|
||||
c.Fuzz(&intstr.IntVal)
|
||||
intstr.StrVal = ""
|
||||
} else {
|
||||
intstr.Kind = IntstrString
|
||||
intstr.IntVal = 0
|
||||
c.Fuzz(&intstr.StrVal)
|
||||
}
|
||||
}
|
||||
|
||||
func GetIntOrPercentValue(intStr *IntOrString) (int, bool, error) {
|
||||
switch intStr.Kind {
|
||||
case IntstrInt:
|
||||
return intStr.IntVal, false, nil
|
||||
case IntstrString:
|
||||
s := strings.Replace(intStr.StrVal, "%", "", -1)
|
||||
func GetIntOrPercentValue(intOrStr *intstr.IntOrString) (int, bool, error) {
|
||||
switch intOrStr.Type {
|
||||
case intstr.Int:
|
||||
return intOrStr.IntVal, false, nil
|
||||
case intstr.String:
|
||||
s := strings.Replace(intOrStr.StrVal, "%", "", -1)
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("invalid value %q: %v", intStr.StrVal, err)
|
||||
return 0, false, fmt.Errorf("invalid value %q: %v", intOrStr.StrVal, err)
|
||||
}
|
||||
return v, true, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user