-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathinterface_map_diff.go
More file actions
70 lines (57 loc) · 1.6 KB
/
interface_map_diff.go
File metadata and controls
70 lines (57 loc) · 1.6 KB
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
package diff
// InterfaceMap is a map of string to interface
type InterfaceMap map[string]any
// InterfaceMapDiff describes the changes between a pair of InterfaceMap
type InterfaceMapDiff struct {
Added []string `json:"added,omitempty" yaml:"added,omitempty"`
Deleted []string `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedInterfaces `json:"modified,omitempty" yaml:"modified,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *InterfaceMapDiff) Empty() bool {
if diff == nil {
return true
}
return len(diff.Added) == 0 &&
len(diff.Deleted) == 0 &&
len(diff.Modified) == 0
}
func newInterfaceMapDiff() *InterfaceMapDiff {
return &InterfaceMapDiff{
Added: []string{},
Deleted: []string{},
Modified: ModifiedInterfaces{},
}
}
func getInterfaceMapDiff(map1, map2 InterfaceMap) (*InterfaceMapDiff, error) {
diff, err := getInterfaceMapDiffInternal(map1, map2)
if err != nil {
return nil, err
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func getInterfaceMapDiffInternal(map1, map2 InterfaceMap) (*InterfaceMapDiff, error) {
result := newInterfaceMapDiff()
for name1, interface1 := range map1 {
if interface2, ok := map2[name1]; ok {
patch, err := compareJson(interface1, interface2)
if err != nil {
return nil, err
}
if !patch.Empty() {
result.Modified[name1] = patch
}
} else {
result.Deleted = append(result.Deleted, name1)
}
}
for name2 := range map2 {
if _, ok := map1[name2]; !ok {
result.Added = append(result.Added, name2)
}
}
return result, nil
}