-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathstring_map_diff.go
More file actions
62 lines (50 loc) · 1.41 KB
/
string_map_diff.go
File metadata and controls
62 lines (50 loc) · 1.41 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
package diff
// StringMapDiff describes the changes between a pair of string maps
type StringMapDiff struct {
Added []string `json:"added,omitempty" yaml:"added,omitempty"`
Deleted []string `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedKeys `json:"modified,omitempty" yaml:"modified,omitempty"`
}
// ModifiedKeys maps keys to their respective diffs
type ModifiedKeys map[string]*ValueDiff
func newStringMapDiffDiff() *StringMapDiff {
return &StringMapDiff{
Added: []string{},
Deleted: []string{},
Modified: ModifiedKeys{},
}
}
// Empty indicates whether a change was found in this element
func (diff *StringMapDiff) Empty() bool {
if diff == nil {
return true
}
return len(diff.Added) == 0 &&
len(diff.Deleted) == 0 &&
len(diff.Modified) == 0
}
func getStringMapDiff(strings1, strings2 map[string]string) *StringMapDiff {
diff := getStringMapDiffInternal(strings1, strings2)
if diff.Empty() {
return nil
}
return diff
}
func getStringMapDiffInternal(strings1, strings2 map[string]string) *StringMapDiff {
result := newStringMapDiffDiff()
for k1, v1 := range strings1 {
if v2, ok := strings2[k1]; ok {
if v1 != v2 {
result.Modified[k1] = getValueDiff(v1, v2)
}
} else {
result.Deleted = append(result.Deleted, k1)
}
}
for k2 := range strings2 {
if _, ok := strings1[k2]; !ok {
result.Added = append(result.Added, k2)
}
}
return result
}