Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / apimachinery / pkg / apis / meta / v1 / conversion.go
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 v1
18
19 import (
20         "fmt"
21         "strconv"
22         "strings"
23
24         "k8s.io/apimachinery/pkg/api/resource"
25         "k8s.io/apimachinery/pkg/conversion"
26         "k8s.io/apimachinery/pkg/fields"
27         "k8s.io/apimachinery/pkg/labels"
28         "k8s.io/apimachinery/pkg/runtime"
29         "k8s.io/apimachinery/pkg/util/intstr"
30 )
31
32 func AddConversionFuncs(scheme *runtime.Scheme) error {
33         return scheme.AddConversionFuncs(
34                 Convert_v1_TypeMeta_To_v1_TypeMeta,
35
36                 Convert_v1_ListMeta_To_v1_ListMeta,
37
38                 Convert_intstr_IntOrString_To_intstr_IntOrString,
39
40                 Convert_Pointer_v1_Duration_To_v1_Duration,
41                 Convert_v1_Duration_To_Pointer_v1_Duration,
42
43                 Convert_Slice_string_To_v1_Time,
44
45                 Convert_v1_Time_To_v1_Time,
46                 Convert_v1_MicroTime_To_v1_MicroTime,
47
48                 Convert_resource_Quantity_To_resource_Quantity,
49
50                 Convert_string_To_labels_Selector,
51                 Convert_labels_Selector_To_string,
52
53                 Convert_string_To_fields_Selector,
54                 Convert_fields_Selector_To_string,
55
56                 Convert_Pointer_bool_To_bool,
57                 Convert_bool_To_Pointer_bool,
58
59                 Convert_Pointer_string_To_string,
60                 Convert_string_To_Pointer_string,
61
62                 Convert_Pointer_int64_To_int,
63                 Convert_int_To_Pointer_int64,
64
65                 Convert_Pointer_int32_To_int32,
66                 Convert_int32_To_Pointer_int32,
67
68                 Convert_Pointer_int64_To_int64,
69                 Convert_int64_To_Pointer_int64,
70
71                 Convert_Pointer_float64_To_float64,
72                 Convert_float64_To_Pointer_float64,
73
74                 Convert_Map_string_To_string_To_v1_LabelSelector,
75                 Convert_v1_LabelSelector_To_Map_string_To_string,
76
77                 Convert_Slice_string_To_Slice_int32,
78
79                 Convert_Slice_string_To_v1_DeletionPropagation,
80         )
81 }
82
83 func Convert_Pointer_float64_To_float64(in **float64, out *float64, s conversion.Scope) error {
84         if *in == nil {
85                 *out = 0
86                 return nil
87         }
88         *out = float64(**in)
89         return nil
90 }
91
92 func Convert_float64_To_Pointer_float64(in *float64, out **float64, s conversion.Scope) error {
93         temp := float64(*in)
94         *out = &temp
95         return nil
96 }
97
98 func Convert_Pointer_int32_To_int32(in **int32, out *int32, s conversion.Scope) error {
99         if *in == nil {
100                 *out = 0
101                 return nil
102         }
103         *out = int32(**in)
104         return nil
105 }
106
107 func Convert_int32_To_Pointer_int32(in *int32, out **int32, s conversion.Scope) error {
108         temp := int32(*in)
109         *out = &temp
110         return nil
111 }
112
113 func Convert_Pointer_int64_To_int64(in **int64, out *int64, s conversion.Scope) error {
114         if *in == nil {
115                 *out = 0
116                 return nil
117         }
118         *out = int64(**in)
119         return nil
120 }
121
122 func Convert_int64_To_Pointer_int64(in *int64, out **int64, s conversion.Scope) error {
123         temp := int64(*in)
124         *out = &temp
125         return nil
126 }
127
128 func Convert_Pointer_int64_To_int(in **int64, out *int, s conversion.Scope) error {
129         if *in == nil {
130                 *out = 0
131                 return nil
132         }
133         *out = int(**in)
134         return nil
135 }
136
137 func Convert_int_To_Pointer_int64(in *int, out **int64, s conversion.Scope) error {
138         temp := int64(*in)
139         *out = &temp
140         return nil
141 }
142
143 func Convert_Pointer_string_To_string(in **string, out *string, s conversion.Scope) error {
144         if *in == nil {
145                 *out = ""
146                 return nil
147         }
148         *out = **in
149         return nil
150 }
151
152 func Convert_string_To_Pointer_string(in *string, out **string, s conversion.Scope) error {
153         if in == nil {
154                 stringVar := ""
155                 *out = &stringVar
156                 return nil
157         }
158         *out = in
159         return nil
160 }
161
162 func Convert_Pointer_bool_To_bool(in **bool, out *bool, s conversion.Scope) error {
163         if *in == nil {
164                 *out = false
165                 return nil
166         }
167         *out = **in
168         return nil
169 }
170
171 func Convert_bool_To_Pointer_bool(in *bool, out **bool, s conversion.Scope) error {
172         if in == nil {
173                 boolVar := false
174                 *out = &boolVar
175                 return nil
176         }
177         *out = in
178         return nil
179 }
180
181 // +k8s:conversion-fn=drop
182 func Convert_v1_TypeMeta_To_v1_TypeMeta(in, out *TypeMeta, s conversion.Scope) error {
183         // These values are explicitly not copied
184         //out.APIVersion = in.APIVersion
185         //out.Kind = in.Kind
186         return nil
187 }
188
189 // +k8s:conversion-fn=copy-only
190 func Convert_v1_ListMeta_To_v1_ListMeta(in, out *ListMeta, s conversion.Scope) error {
191         *out = *in
192         return nil
193 }
194
195 // +k8s:conversion-fn=copy-only
196 func Convert_intstr_IntOrString_To_intstr_IntOrString(in, out *intstr.IntOrString, s conversion.Scope) error {
197         *out = *in
198         return nil
199 }
200
201 // +k8s:conversion-fn=copy-only
202 func Convert_v1_Time_To_v1_Time(in *Time, out *Time, s conversion.Scope) error {
203         // Cannot deep copy these, because time.Time has unexported fields.
204         *out = *in
205         return nil
206 }
207
208 // +k8s:conversion-fn=copy-only
209 func Convert_v1_MicroTime_To_v1_MicroTime(in *MicroTime, out *MicroTime, s conversion.Scope) error {
210         // Cannot deep copy these, because time.Time has unexported fields.
211         *out = *in
212         return nil
213 }
214
215 func Convert_Pointer_v1_Duration_To_v1_Duration(in **Duration, out *Duration, s conversion.Scope) error {
216         if *in == nil {
217                 *out = Duration{} // zero duration
218                 return nil
219         }
220         *out = **in // copy
221         return nil
222 }
223
224 func Convert_v1_Duration_To_Pointer_v1_Duration(in *Duration, out **Duration, s conversion.Scope) error {
225         temp := *in //copy
226         *out = &temp
227         return nil
228 }
229
230 // Convert_Slice_string_To_v1_Time allows converting a URL query parameter value
231 func Convert_Slice_string_To_v1_Time(input *[]string, out *Time, s conversion.Scope) error {
232         str := ""
233         if len(*input) > 0 {
234                 str = (*input)[0]
235         }
236         return out.UnmarshalQueryParameter(str)
237 }
238
239 func Convert_string_To_labels_Selector(in *string, out *labels.Selector, s conversion.Scope) error {
240         selector, err := labels.Parse(*in)
241         if err != nil {
242                 return err
243         }
244         *out = selector
245         return nil
246 }
247
248 func Convert_string_To_fields_Selector(in *string, out *fields.Selector, s conversion.Scope) error {
249         selector, err := fields.ParseSelector(*in)
250         if err != nil {
251                 return err
252         }
253         *out = selector
254         return nil
255 }
256
257 func Convert_labels_Selector_To_string(in *labels.Selector, out *string, s conversion.Scope) error {
258         if *in == nil {
259                 return nil
260         }
261         *out = (*in).String()
262         return nil
263 }
264
265 func Convert_fields_Selector_To_string(in *fields.Selector, out *string, s conversion.Scope) error {
266         if *in == nil {
267                 return nil
268         }
269         *out = (*in).String()
270         return nil
271 }
272
273 // +k8s:conversion-fn=copy-only
274 func Convert_resource_Quantity_To_resource_Quantity(in *resource.Quantity, out *resource.Quantity, s conversion.Scope) error {
275         *out = *in
276         return nil
277 }
278
279 func Convert_Map_string_To_string_To_v1_LabelSelector(in *map[string]string, out *LabelSelector, s conversion.Scope) error {
280         if in == nil {
281                 return nil
282         }
283         for labelKey, labelValue := range *in {
284                 AddLabelToSelector(out, labelKey, labelValue)
285         }
286         return nil
287 }
288
289 func Convert_v1_LabelSelector_To_Map_string_To_string(in *LabelSelector, out *map[string]string, s conversion.Scope) error {
290         var err error
291         *out, err = LabelSelectorAsMap(in)
292         return err
293 }
294
295 // Convert_Slice_string_To_Slice_int32 converts multiple query parameters or
296 // a single query parameter with a comma delimited value to multiple int32.
297 // This is used for port forwarding which needs the ports as int32.
298 func Convert_Slice_string_To_Slice_int32(in *[]string, out *[]int32, s conversion.Scope) error {
299         for _, s := range *in {
300                 for _, v := range strings.Split(s, ",") {
301                         x, err := strconv.ParseUint(v, 10, 16)
302                         if err != nil {
303                                 return fmt.Errorf("cannot convert to []int32: %v", err)
304                         }
305                         *out = append(*out, int32(x))
306                 }
307         }
308         return nil
309 }
310
311 // Convert_Slice_string_To_v1_DeletionPropagation allows converting a URL query parameter propagationPolicy
312 func Convert_Slice_string_To_v1_DeletionPropagation(input *[]string, out *DeletionPropagation, s conversion.Scope) error {
313         if len(*input) > 0 {
314                 *out = DeletionPropagation((*input)[0])
315         } else {
316                 *out = ""
317         }
318         return nil
319 }