-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
unstructured.go
71 lines (65 loc) · 1.68 KB
/
unstructured.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package uo
import (
"fmt"
"github.com/kluctl/kluctl/pkg/utils"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func CopyUnstructured(u *unstructured.Unstructured) *unstructured.Unstructured {
var ret unstructured.Unstructured
err := utils.DeepCopy(&ret.Object, &u.Object)
if err != nil {
log.Fatal(err)
}
return &ret
}
func MergeStrMap(a map[string]string, b map[string]string) {
for k, v := range b {
a[k] = v
}
}
func CopyMergeStrMap(a map[string]string, b map[string]string) map[string]string {
c := make(map[string]string)
MergeStrMap(c, a)
MergeStrMap(c, b)
return c
}
func GetChild(parent interface{}, key interface{}) (interface{}, bool, error) {
if m, ok := parent.(map[string]interface{}); ok {
keyStr, ok := key.(string)
if !ok {
return nil, false, fmt.Errorf("key is not a string")
}
v, found := m[keyStr]
return v, found, nil
} else if l, ok := parent.([]interface{}); ok {
keyInt, ok := key.(int)
if !ok {
return nil, false, fmt.Errorf("key is not an int")
}
if keyInt < 0 || keyInt >= len(l) {
return nil, false, fmt.Errorf("index out of bounds")
}
v := l[keyInt]
return v, true, nil
}
return nil, false, fmt.Errorf("unknown parent type")
}
func SetChild(parent interface{}, key interface{}, value interface{}) error {
if m, ok := parent.(map[string]interface{}); ok {
keyStr, ok := key.(string)
if !ok {
return fmt.Errorf("key is not a string")
}
m[keyStr] = value
return nil
} else if l, ok := parent.([]interface{}); ok {
keyInt, ok := key.(int)
if !ok {
return fmt.Errorf("key is not an int")
}
l[keyInt] = value
return nil
}
return fmt.Errorf("unknown parent type")
}