conversion.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // 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. // DefaultFieldSelectorConversion 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. // DefaultStringConversions are helpers for converting []string and string to real values.
  48. var DefaultStringConversions = []interface{}{
  49. Convert_Slice_string_To_string,
  50. Convert_Slice_string_To_int,
  51. Convert_Slice_string_To_bool,
  52. Convert_Slice_string_To_int64,
  53. }
  54. func Convert_Slice_string_To_string(input *[]string, out *string, s conversion.Scope) error {
  55. if len(*input) == 0 {
  56. *out = ""
  57. }
  58. *out = (*input)[0]
  59. return nil
  60. }
  61. func Convert_Slice_string_To_int(input *[]string, out *int, s conversion.Scope) error {
  62. if len(*input) == 0 {
  63. *out = 0
  64. }
  65. str := (*input)[0]
  66. i, err := strconv.Atoi(str)
  67. if err != nil {
  68. return err
  69. }
  70. *out = i
  71. return nil
  72. }
  73. // Conver_Slice_string_To_bool will convert a string parameter to boolean.
  74. // Only the absence of a value, a value of "false", or a value of "0" resolve to false.
  75. // Any other value (including empty string) resolves to true.
  76. func Convert_Slice_string_To_bool(input *[]string, out *bool, s conversion.Scope) error {
  77. if len(*input) == 0 {
  78. *out = false
  79. return nil
  80. }
  81. switch strings.ToLower((*input)[0]) {
  82. case "false", "0":
  83. *out = false
  84. default:
  85. *out = true
  86. }
  87. return nil
  88. }
  89. func Convert_Slice_string_To_int64(input *[]string, out *int64, s conversion.Scope) error {
  90. if len(*input) == 0 {
  91. *out = 0
  92. }
  93. str := (*input)[0]
  94. i, err := strconv.ParseInt(str, 10, 64)
  95. if err != nil {
  96. return err
  97. }
  98. *out = i
  99. return nil
  100. }