intstr.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package intstr
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "math"
  18. "strconv"
  19. "strings"
  20. "github.com/google/gofuzz"
  21. )
  22. // IntOrString is a type that can hold an int32 or a string. When used in
  23. // JSON or YAML marshalling and unmarshalling, it produces or consumes the
  24. // inner type. This allows you to have, for example, a JSON field that can
  25. // accept a name or number.
  26. // TODO: Rename to Int32OrString
  27. //
  28. // +protobuf=true
  29. // +protobuf.options.(gogoproto.goproto_stringer)=false
  30. type IntOrString struct {
  31. Type Type `protobuf:"varint,1,opt,name=type,casttype=Type"`
  32. IntVal int32 `protobuf:"varint,2,opt,name=intVal"`
  33. StrVal string `protobuf:"bytes,3,opt,name=strVal"`
  34. }
  35. // Type represents the stored type of IntOrString.
  36. type Type int
  37. const (
  38. Int Type = iota // The IntOrString holds an int.
  39. String // The IntOrString holds a string.
  40. )
  41. // FromInt creates an IntOrString object with an int32 value. It is
  42. // your responsibility not to call this method with a value greater
  43. // than int32.
  44. // TODO: convert to (val int32)
  45. func FromInt(val int) IntOrString {
  46. return IntOrString{Type: Int, IntVal: int32(val)}
  47. }
  48. // FromString creates an IntOrString object with a string value.
  49. func FromString(val string) IntOrString {
  50. return IntOrString{Type: String, StrVal: val}
  51. }
  52. // UnmarshalJSON implements the json.Unmarshaller interface.
  53. func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
  54. if value[0] == '"' {
  55. intstr.Type = String
  56. return json.Unmarshal(value, &intstr.StrVal)
  57. }
  58. intstr.Type = Int
  59. return json.Unmarshal(value, &intstr.IntVal)
  60. }
  61. // String returns the string value, or the Itoa of the int value.
  62. func (intstr *IntOrString) String() string {
  63. if intstr.Type == String {
  64. return intstr.StrVal
  65. }
  66. return strconv.Itoa(intstr.IntValue())
  67. }
  68. // IntValue returns the IntVal if type Int, or if
  69. // it is a String, will attempt a conversion to int.
  70. func (intstr *IntOrString) IntValue() int {
  71. if intstr.Type == String {
  72. i, _ := strconv.Atoi(intstr.StrVal)
  73. return i
  74. }
  75. return int(intstr.IntVal)
  76. }
  77. // MarshalJSON implements the json.Marshaller interface.
  78. func (intstr IntOrString) MarshalJSON() ([]byte, error) {
  79. switch intstr.Type {
  80. case Int:
  81. return json.Marshal(intstr.IntVal)
  82. case String:
  83. return json.Marshal(intstr.StrVal)
  84. default:
  85. return []byte{}, fmt.Errorf("impossible IntOrString.Type")
  86. }
  87. }
  88. func (intstr *IntOrString) Fuzz(c fuzz.Continue) {
  89. if intstr == nil {
  90. return
  91. }
  92. if c.RandBool() {
  93. intstr.Type = Int
  94. c.Fuzz(&intstr.IntVal)
  95. intstr.StrVal = ""
  96. } else {
  97. intstr.Type = String
  98. intstr.IntVal = 0
  99. c.Fuzz(&intstr.StrVal)
  100. }
  101. }
  102. func GetValueFromIntOrPercent(intOrPercent *IntOrString, total int, roundUp bool) (int, error) {
  103. value, isPercent, err := getIntOrPercentValue(intOrPercent)
  104. if err != nil {
  105. return 0, fmt.Errorf("invalid value for IntOrString: %v", err)
  106. }
  107. if isPercent {
  108. if roundUp {
  109. value = int(math.Ceil(float64(value) * (float64(total)) / 100))
  110. } else {
  111. value = int(math.Floor(float64(value) * (float64(total)) / 100))
  112. }
  113. }
  114. return value, nil
  115. }
  116. func getIntOrPercentValue(intOrStr *IntOrString) (int, bool, error) {
  117. switch intOrStr.Type {
  118. case Int:
  119. return intOrStr.IntValue(), false, nil
  120. case String:
  121. s := strings.Replace(intOrStr.StrVal, "%", "", -1)
  122. v, err := strconv.Atoi(s)
  123. if err != nil {
  124. return 0, false, fmt.Errorf("invalid value %q: %v", intOrStr.StrVal, err)
  125. }
  126. return int(v), true, nil
  127. }
  128. return 0, false, fmt.Errorf("invalid type: neither int nor percentage")
  129. }