forked from argoproj/gitops-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
57 lines (53 loc) · 1.27 KB
/
json.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
package json
// https://github.com/ksonnet/ksonnet/blob/master/pkg/kubecfg/diff.go
func removeFields(config, live interface{}) interface{} {
switch c := config.(type) {
case map[string]interface{}:
l, ok := live.(map[string]interface{})
if ok {
return RemoveMapFields(c, l)
} else {
return live
}
case []interface{}:
l, ok := live.([]interface{})
if ok {
return RemoveListFields(c, l)
} else {
return live
}
default:
return live
}
}
// RemoveMapFields remove all non-existent fields in the live that don't exist in the config
func RemoveMapFields(config, live map[string]interface{}) map[string]interface{} {
result := map[string]interface{}{}
for k, v1 := range config {
v2, ok := live[k]
if !ok {
continue
}
if v2 != nil {
v2 = removeFields(v1, v2)
}
result[k] = v2
}
return result
}
func RemoveListFields(config, live []interface{}) []interface{} {
// If live is longer than config, then the extra elements at the end of the
// list will be returned as-is so they appear in the diff.
result := make([]interface{}, 0, len(live))
for i, v2 := range live {
if len(config) > i {
if v2 != nil {
v2 = removeFields(config[i], v2)
}
result = append(result, v2)
} else {
result = append(result, v2)
}
}
return result
}