forked from Jonathan-Rosenberg/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_map_diff.go
64 lines (51 loc) · 1.48 KB
/
string_map_diff.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
58
59
60
61
62
63
64
package diff
import "github.com/treeverse/oasdiff/utils"
// StringMapDiff describes the changes between a pair of string maps
type StringMapDiff struct {
Added utils.StringList `json:"added,omitempty" yaml:"added,omitempty"`
Deleted utils.StringList `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: utils.StringList{},
Deleted: utils.StringList{},
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 utils.StringMap) *StringMapDiff {
diff := getStringMapDiffInternal(strings1, strings2)
if diff.Empty() {
return nil
}
return diff
}
func getStringMapDiffInternal(strings1, strings2 utils.StringMap) *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
}