-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge.go
101 lines (93 loc) · 1.83 KB
/
merge.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package tbln
import (
"fmt"
"io"
)
// MergeMode represents the mode of merge.
type MergeMode int
// Represents the merge mode
const (
MergeIgnore MergeMode = iota
MergeUpdate
MergeDelete
)
// MergeRow reads merged rows from DiffRow
func (d *DiffRow) MergeRow(mode MergeMode) []string {
switch d.Les {
case 0:
return d.Self
case 1:
return d.Other
case -1:
if mode == MergeDelete {
return nil
}
return d.Self
case 2:
if mode == MergeIgnore {
return d.Self
}
return d.Other
default:
return nil
}
}
// MergeAll merges two tbln and returns one tbln.
func MergeAll(t1, t2 Reader, mode MergeMode) (*TBLN, error) {
tb := &TBLN{}
diff, err := NewCompare(t1, t2)
if err != nil {
return nil, err
}
t1Definition := t1.GetDefinition()
t2Definition := t2.GetDefinition()
tb.Definition, err = MergeDefinition(t1Definition, t2Definition)
if err != nil {
return nil, err
}
tb.Rows = make([][]string, 0)
for {
dd, err := diff.ReadDiffRow()
if err != nil {
if err == io.EOF {
return tb, nil
}
return nil, err
}
row := dd.MergeRow(mode)
if row != nil {
tb.RowNum++
tb.Rows = append(tb.Rows, row)
}
}
}
// MergeDefinition merges two tbln Definitions.
func MergeDefinition(t1d, t2d *Definition) (*Definition, error) {
if t1d.columnNum != t2d.columnNum {
return nil, fmt.Errorf("different column num")
}
d := NewDefinition()
d.Comments = mergeComment(t1d, t2d)
d.Extras = t1d.Extras
for k, v := range t2d.Extras {
d.Extras[k] = v
}
return d, nil
}
func mergeComment(t1d, t2d *Definition) []string {
comments := make([]string, len(t1d.Comments))
copy(comments, t1d.Comments)
for _, dc := range t2d.Comments {
appFlag := true
for _, sc := range t1d.Comments {
if dc == sc {
appFlag = false
break
}
}
if appFlag {
comments = append(comments, dc)
}
}
return comments
}