-
Notifications
You must be signed in to change notification settings - Fork 0
/
unstructured.go
45 lines (40 loc) · 1.09 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
package configobserver
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
// Pruned returns the unstructured filtered by the given paths, i.e. everything
// outside of them will be dropped. The returned data structure might overlap
// with the input, but the input is not mutated. In case of error for a path,
// that path is dropped.
func Pruned(obj map[string]interface{}, pths ...[]string) map[string]interface{} {
if obj == nil || len(pths) == 0 {
return obj
}
ret := map[string]interface{}{}
if len(pths) == 1 {
x, found, err := unstructured.NestedFieldCopy(obj, pths[0]...)
if err != nil || !found {
return ret
}
unstructured.SetNestedField(ret, x, pths[0]...)
return ret
}
for i, p := range pths {
x, found, err := unstructured.NestedFieldCopy(obj, p...)
if err != nil {
continue
}
if !found {
continue
}
if i < len(pths)-1 {
// this might be overwritten by a later path
x = runtime.DeepCopyJSONValue(x)
}
if err := unstructured.SetNestedField(ret, x, p...); err != nil {
continue
}
}
return ret
}