-
Notifications
You must be signed in to change notification settings - Fork 0
/
slicediff.go
54 lines (47 loc) · 1014 Bytes
/
slicediff.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
package slicediff
import (
"strings"
)
type DiffResponse struct {
Added []string
Removed []string
}
func (d *DiffResponse) Changed() bool {
return len(d.Added)+len(d.Removed) != 0
}
func DiffIgnoreCase(src []*string, dst []*string) DiffResponse {
srcVal := make([]string, 0)
dstVal := make([]string, 0)
for _, s := range src {
if s == nil {
srcVal = append(srcVal, "")
} else {
srcVal = append(srcVal, strings.ToLower(*s))
}
}
for _, d := range dst {
if d == nil {
dstVal = append(dstVal, "")
} else {
dstVal = append(dstVal, strings.ToLower(*d))
}
}
return DiffResponse{
Added: difference(dstVal, srcVal),
Removed: difference(srcVal, dstVal),
}
}
// difference returns the elements in `a` that aren't in `b`.
func difference(a, b []string) []string {
mb := make(map[string]struct{}, len(b))
for _, x := range b {
mb[x] = struct{}{}
}
var diff []string
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}