-
Notifications
You must be signed in to change notification settings - Fork 31
/
unstructured-modification.go
53 lines (46 loc) · 1.58 KB
/
unstructured-modification.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
package main
// all of these have been pulled from k8s.io/apimachinery/pkg/apis/meta/v1/unstructured
// this issue https://github.com/kubernetes/apimachinery/issues/138 explains why I needed to duplicate these funcs
// most of the commands were using map[string]interface{} in the original but that wasn't what was returned by the `yaml.Unamrshal`
// thus I moved the checks to `map[interface{}]interface{}`
import "fmt"
func mutableNestedSlice(obj map[interface{}]interface{}, fields ...string) ([]interface{}, bool, error) {
val, found, err := nestedFieldNoCopy(obj, fields...)
if !found || err != nil {
return nil, found, err
}
// using retVal instead of the json stuff that was originally as if broke. this doesn't
retVal, ok := val.([]interface{})
if !ok {
return nil, false, fmt.Errorf("could not convert to []interface{}")
}
return retVal, true, nil
}
func nestedFieldNoCopy(obj map[interface{}]interface{}, fields ...string) (interface{}, bool, error) {
var val interface{} = obj
for _, field := range fields {
if val == nil {
return nil, false, nil
}
if m, ok := val.(map[interface{}]interface{}); ok {
val, ok = m[field]
if !ok {
return nil, false, nil
}
} else {
return nil, false, fmt.Errorf("%v is of the type %T, expected map[string]interface{}", val, val)
}
}
return val, true, nil
}
func removeNestedField(obj map[interface{}]interface{}, fields ...string) {
m := obj
for _, field := range fields[:len(fields)-1] {
if x, ok := m[field].(map[interface{}]interface{}); ok {
m = x
} else {
return
}
}
delete(m, fields[len(fields)-1])
}