2 Copyright 2014 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 // Package runtime defines conversions between generic types and structs to map query strings
27 "k8s.io/apimachinery/pkg/conversion"
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) {
36 return label, value, nil
37 case "metadata.namespace":
38 return label, value, nil
40 return "", "", fmt.Errorf("%q is not a known field selector: only %q, %q", label, "metadata.name", "metadata.namespace")
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
50 if s := sourceTag.Get("json"); len(s) > 0 {
51 return key, strings.SplitN(s, ",", 2)[0]
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,
64 func Convert_Slice_string_To_string(input *[]string, out *string, s conversion.Scope) error {
72 func Convert_Slice_string_To_int(input *[]string, out *int, s conversion.Scope) error {
77 i, err := strconv.Atoi(str)
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 {
93 switch strings.ToLower((*input)[0]) {
102 func Convert_Slice_string_To_int64(input *[]string, out *int64, s conversion.Scope) error {
103 if len(*input) == 0 {
107 i, err := strconv.ParseInt(str, 10, 64)