forked from MasteryConnect/pipe
/
diff.go
66 lines (56 loc) · 1.74 KB
/
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
65
66
package x
import (
"github.com/Reisender/pipe/line"
"github.com/Reisender/pipe/message"
)
// Diff is a producer that compares two pipelines and emits the
// differences as message.Diff{} structs
func Diff(pipe1, pipe2 line.Pipeline, includeMatches bool) line.Pfunc {
return func(out chan<- interface{}, errs chan<- error) {
pipe1Out := make(chan interface{})
pipe2Out := make(chan interface{})
go func() {
defer close(pipe1Out)
pipe1.Add(Tap(pipe1Out)).Run()
}()
go func() {
defer close(pipe2Out)
pipe2.Add(Tap(pipe2Out)).Run()
}()
// process the first pipe and extract the keys
data := map[string]interface{}{}
for m := range pipe1Out {
msg := m.(message.Keyer)
data[msg.Key()] = m
}
// process the second pipe and compare the keys
// this is where the diffs are produced
for m := range pipe2Out {
msg := m.(message.Keyer)
if first, ok := data[msg.Key()]; ok {
// diff the content and not just the Key() if we can
if hasher1, ok1 := first.(message.Hasher); ok1 {
if hasher2, ok2 := m.(message.Hasher); ok2 {
if hasher1.Hash() != hasher2.Hash() {
out <- message.Diff{Left: first, Right: m}
} else if includeMatches {
// since the hashes match, we can emit as a match
out <- message.Diff{Left: first, Right: msg}
}
}
} else if includeMatches {
// since there is not hash to check, we have a match so emit
out <- message.Diff{Left: first, Right: msg}
}
delete(data, msg.Key()) // remove it since it matches both
} else {
// not in first stream so emit a diff
out <- message.Diff{Right: msg}
}
}
// new emit anything left in the first stream's data
for _, msg := range data {
out <- message.Diff{Left: msg}
}
}
}