blob: afe4fab15e8f486cc0c2289b327d20de4124844b [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Defines conversions between generic types and structs to map query strings
18// to struct objects.
19package runtime
20
21import (
22 "fmt"
23 "reflect"
24 "strconv"
25 "strings"
26
27 "k8s.io/apimachinery/pkg/conversion"
28)
29
30// DefaultFieldSelectorConversion 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.
33func 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.
46func 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.
57var 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
64func 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
72func 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// Conver_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.
88func 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
102func 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}