conversion.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 runtime defines conversions between generic types and structs to map query strings
  14. // to struct objects.
  15. package runtime
  16. import (
  17. "fmt"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "k8s.io/apimachinery/pkg/conversion"
  22. )
  23. // DefaultMetaV1FieldSelectorConversion auto-accepts metav1 values for name and namespace.
  24. // A cluster scoped resource specifying namespace empty works fine and specifying a particular
  25. // namespace will return no results, as expected.
  26. func DefaultMetaV1FieldSelectorConversion(label, value string) (string, string, error) {
  27. switch label {
  28. case "metadata.name":
  29. return label, value, nil
  30. case "metadata.namespace":
  31. return label, value, nil
  32. default:
  33. return "", "", fmt.Errorf("%q is not a known field selector: only %q, %q", label, "metadata.name", "metadata.namespace")
  34. }
  35. }
  36. // JSONKeyMapper uses the struct tags on a conversion to determine the key value for
  37. // the other side. Use when mapping from a map[string]* to a struct or vice versa.
  38. func JSONKeyMapper(key string, sourceTag, destTag reflect.StructTag) (string, string) {
  39. if s := destTag.Get("json"); len(s) > 0 {
  40. return strings.SplitN(s, ",", 2)[0], key
  41. }
  42. if s := sourceTag.Get("json"); len(s) > 0 {
  43. return key, strings.SplitN(s, ",", 2)[0]
  44. }
  45. return key, key
  46. }
  47. func Convert_Slice_string_To_string(in *[]string, out *string, s conversion.Scope) error {
  48. if len(*in) == 0 {
  49. *out = ""
  50. return nil
  51. }
  52. *out = (*in)[0]
  53. return nil
  54. }
  55. func Convert_Slice_string_To_int(in *[]string, out *int, s conversion.Scope) error {
  56. if len(*in) == 0 {
  57. *out = 0
  58. return nil
  59. }
  60. str := (*in)[0]
  61. i, err := strconv.Atoi(str)
  62. if err != nil {
  63. return err
  64. }
  65. *out = i
  66. return nil
  67. }
  68. // Convert_Slice_string_To_bool will convert a string parameter to boolean.
  69. // Only the absence of a value (i.e. zero-length slice), a value of "false", or a
  70. // value of "0" resolve to false.
  71. // Any other value (including empty string) resolves to true.
  72. func Convert_Slice_string_To_bool(in *[]string, out *bool, s conversion.Scope) error {
  73. if len(*in) == 0 {
  74. *out = false
  75. return nil
  76. }
  77. switch {
  78. case (*in)[0] == "0", strings.EqualFold((*in)[0], "false"):
  79. *out = false
  80. default:
  81. *out = true
  82. }
  83. return nil
  84. }
  85. // Convert_Slice_string_To_bool will convert a string parameter to boolean.
  86. // Only the absence of a value (i.e. zero-length slice), a value of "false", or a
  87. // value of "0" resolve to false.
  88. // Any other value (including empty string) resolves to true.
  89. func Convert_Slice_string_To_Pointer_bool(in *[]string, out **bool, s conversion.Scope) error {
  90. if len(*in) == 0 {
  91. boolVar := false
  92. *out = &boolVar
  93. return nil
  94. }
  95. switch {
  96. case (*in)[0] == "0", strings.EqualFold((*in)[0], "false"):
  97. boolVar := false
  98. *out = &boolVar
  99. default:
  100. boolVar := true
  101. *out = &boolVar
  102. }
  103. return nil
  104. }
  105. func string_to_int64(in string) (int64, error) {
  106. return strconv.ParseInt(in, 10, 64)
  107. }
  108. func Convert_string_To_int64(in *string, out *int64, s conversion.Scope) error {
  109. if in == nil {
  110. *out = 0
  111. return nil
  112. }
  113. i, err := string_to_int64(*in)
  114. if err != nil {
  115. return err
  116. }
  117. *out = i
  118. return nil
  119. }
  120. func Convert_Slice_string_To_int64(in *[]string, out *int64, s conversion.Scope) error {
  121. if len(*in) == 0 {
  122. *out = 0
  123. return nil
  124. }
  125. i, err := string_to_int64((*in)[0])
  126. if err != nil {
  127. return err
  128. }
  129. *out = i
  130. return nil
  131. }
  132. func Convert_string_To_Pointer_int64(in *string, out **int64, s conversion.Scope) error {
  133. if in == nil {
  134. *out = nil
  135. return nil
  136. }
  137. i, err := string_to_int64(*in)
  138. if err != nil {
  139. return err
  140. }
  141. *out = &i
  142. return nil
  143. }
  144. func Convert_Slice_string_To_Pointer_int64(in *[]string, out **int64, s conversion.Scope) error {
  145. if len(*in) == 0 {
  146. *out = nil
  147. return nil
  148. }
  149. i, err := string_to_int64((*in)[0])
  150. if err != nil {
  151. return err
  152. }
  153. *out = &i
  154. return nil
  155. }
  156. func RegisterStringConversions(s *Scheme) error {
  157. if err := s.AddConversionFunc((*[]string)(nil), (*string)(nil), func(a, b interface{}, scope conversion.Scope) error {
  158. return Convert_Slice_string_To_string(a.(*[]string), b.(*string), scope)
  159. }); err != nil {
  160. return err
  161. }
  162. if err := s.AddConversionFunc((*[]string)(nil), (*int)(nil), func(a, b interface{}, scope conversion.Scope) error {
  163. return Convert_Slice_string_To_int(a.(*[]string), b.(*int), scope)
  164. }); err != nil {
  165. return err
  166. }
  167. if err := s.AddConversionFunc((*[]string)(nil), (*bool)(nil), func(a, b interface{}, scope conversion.Scope) error {
  168. return Convert_Slice_string_To_bool(a.(*[]string), b.(*bool), scope)
  169. }); err != nil {
  170. return err
  171. }
  172. if err := s.AddConversionFunc((*[]string)(nil), (*int64)(nil), func(a, b interface{}, scope conversion.Scope) error {
  173. return Convert_Slice_string_To_int64(a.(*[]string), b.(*int64), scope)
  174. }); err != nil {
  175. return err
  176. }
  177. return nil
  178. }