Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Package runtime defines conversions between generic types and structs to map query strings |
| 18 | // to struct objects. |
| 19 | package runtime |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "reflect" |
| 24 | "strconv" |
| 25 | "strings" |
| 26 | |
| 27 | "k8s.io/apimachinery/pkg/conversion" |
| 28 | ) |
| 29 | |
| 30 | // DefaultMetaV1FieldSelectorConversion auto-accepts metav1 values for name and namespace. |
| 31 | // A cluster scoped resource specifying namespace empty works fine and specifying a particular |
| 32 | // namespace will return no results, as expected. |
| 33 | func DefaultMetaV1FieldSelectorConversion(label, value string) (string, string, error) { |
| 34 | switch label { |
| 35 | case "metadata.name": |
| 36 | return label, value, nil |
| 37 | case "metadata.namespace": |
| 38 | return label, value, nil |
| 39 | default: |
| 40 | return "", "", fmt.Errorf("%q is not a known field selector: only %q, %q", label, "metadata.name", "metadata.namespace") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // JSONKeyMapper uses the struct tags on a conversion to determine the key value for |
| 45 | // the other side. Use when mapping from a map[string]* to a struct or vice versa. |
| 46 | func JSONKeyMapper(key string, sourceTag, destTag reflect.StructTag) (string, string) { |
| 47 | if s := destTag.Get("json"); len(s) > 0 { |
| 48 | return strings.SplitN(s, ",", 2)[0], key |
| 49 | } |
| 50 | if s := sourceTag.Get("json"); len(s) > 0 { |
| 51 | return key, strings.SplitN(s, ",", 2)[0] |
| 52 | } |
| 53 | return key, key |
| 54 | } |
| 55 | |
| 56 | // DefaultStringConversions are helpers for converting []string and string to real values. |
| 57 | var DefaultStringConversions = []interface{}{ |
| 58 | Convert_Slice_string_To_string, |
| 59 | Convert_Slice_string_To_int, |
| 60 | Convert_Slice_string_To_bool, |
| 61 | Convert_Slice_string_To_int64, |
| 62 | } |
| 63 | |
| 64 | func Convert_Slice_string_To_string(input *[]string, out *string, s conversion.Scope) error { |
| 65 | if len(*input) == 0 { |
| 66 | *out = "" |
| 67 | } |
| 68 | *out = (*input)[0] |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | func Convert_Slice_string_To_int(input *[]string, out *int, s conversion.Scope) error { |
| 73 | if len(*input) == 0 { |
| 74 | *out = 0 |
| 75 | } |
| 76 | str := (*input)[0] |
| 77 | i, err := strconv.Atoi(str) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | *out = i |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // Convert_Slice_string_To_bool will convert a string parameter to boolean. |
| 86 | // Only the absence of a value, a value of "false", or a value of "0" resolve to false. |
| 87 | // Any other value (including empty string) resolves to true. |
| 88 | func Convert_Slice_string_To_bool(input *[]string, out *bool, s conversion.Scope) error { |
| 89 | if len(*input) == 0 { |
| 90 | *out = false |
| 91 | return nil |
| 92 | } |
| 93 | switch strings.ToLower((*input)[0]) { |
| 94 | case "false", "0": |
| 95 | *out = false |
| 96 | default: |
| 97 | *out = true |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | func Convert_Slice_string_To_int64(input *[]string, out *int64, s conversion.Scope) error { |
| 103 | if len(*input) == 0 { |
| 104 | *out = 0 |
| 105 | } |
| 106 | str := (*input)[0] |
| 107 | i, err := strconv.ParseInt(str, 10, 64) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | *out = i |
| 112 | return nil |
| 113 | } |