conversion.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "reflect"
  18. "strconv"
  19. "strings"
  20. "k8s.io/kubernetes/pkg/conversion"
  21. )
  22. // JSONKeyMapper uses the struct tags on a conversion to determine the key value for
  23. // the other side. Use when mapping from a map[string]* to a struct or vice versa.
  24. func JSONKeyMapper(key string, sourceTag, destTag reflect.StructTag) (string, string) {
  25. if s := destTag.Get("json"); len(s) > 0 {
  26. return strings.SplitN(s, ",", 2)[0], key
  27. }
  28. if s := sourceTag.Get("json"); len(s) > 0 {
  29. return key, strings.SplitN(s, ",", 2)[0]
  30. }
  31. return key, key
  32. }
  33. // DefaultStringConversions are helpers for converting []string and string to real values.
  34. var DefaultStringConversions = []interface{}{
  35. Convert_Slice_string_To_string,
  36. Convert_Slice_string_To_int,
  37. Convert_Slice_string_To_bool,
  38. Convert_Slice_string_To_int64,
  39. }
  40. func Convert_Slice_string_To_string(input *[]string, out *string, s conversion.Scope) error {
  41. if len(*input) == 0 {
  42. *out = ""
  43. }
  44. *out = (*input)[0]
  45. return nil
  46. }
  47. func Convert_Slice_string_To_int(input *[]string, out *int, s conversion.Scope) error {
  48. if len(*input) == 0 {
  49. *out = 0
  50. }
  51. str := (*input)[0]
  52. i, err := strconv.Atoi(str)
  53. if err != nil {
  54. return err
  55. }
  56. *out = i
  57. return nil
  58. }
  59. // Conver_Slice_string_To_bool will convert a string parameter to boolean.
  60. // Only the absence of a value, a value of "false", or a value of "0" resolve to false.
  61. // Any other value (including empty string) resolves to true.
  62. func Convert_Slice_string_To_bool(input *[]string, out *bool, s conversion.Scope) error {
  63. if len(*input) == 0 {
  64. *out = false
  65. return nil
  66. }
  67. switch strings.ToLower((*input)[0]) {
  68. case "false", "0":
  69. *out = false
  70. default:
  71. *out = true
  72. }
  73. return nil
  74. }
  75. func Convert_Slice_string_To_int64(input *[]string, out *int64, s conversion.Scope) error {
  76. if len(*input) == 0 {
  77. *out = 0
  78. }
  79. str := (*input)[0]
  80. i, err := strconv.ParseInt(str, 10, 64)
  81. if err != nil {
  82. return err
  83. }
  84. *out = i
  85. return nil
  86. }