-
Notifications
You must be signed in to change notification settings - Fork 858
/
utils.go
57 lines (49 loc) · 1.48 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cue
// int data can evaluate with number in CUE, so it's OK if we convert the original float type data to int
func isIntegral(val float64) bool {
return val == float64(int(val))
}
// IntifyValues will make values to int.
// JSON marshalling of user values will put integer into float,
// we have to change it back so that CUE check will succeed.
func IntifyValues(raw interface{}) interface{} {
switch v := raw.(type) {
case map[string]interface{}:
return intifyMap(v)
case []interface{}:
return intifyList(v)
case float64:
if isIntegral(v) {
return int(v)
}
return v
default:
return raw
}
}
func intifyList(l []interface{}) interface{} {
l2 := make([]interface{}, 0, len(l))
for _, v := range l {
l2 = append(l2, IntifyValues(v))
}
return l2
}
func intifyMap(m map[string]interface{}) interface{} {
m2 := make(map[string]interface{}, len(m))
for k, v := range m {
m2[k] = IntifyValues(v)
}
return m2
}