-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.go
170 lines (136 loc) · 3.68 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package ginsa
import (
"fmt"
"github.com/fatih/color"
"sort"
)
type Diff interface {
String() string
}
type AllDiff struct {
NewBanks map[string]*Bank
Banks []Diff
Branches map[string][]Diff
}
func (a *AllDiff) Out() {
cyan := color.New(color.FgCyan, color.Bold).PrintfFunc()
blue := color.New(color.FgBlue, color.Bold).PrintfFunc()
cyan("=> Banks diff(%d)\n", len(a.Banks))
for _, d := range a.Banks {
fmt.Println(" " + d.String())
}
cyan("=> Branches diff\n")
dCodes := []string{}
for code, _ := range a.Branches {
dCodes = append(dCodes, code)
}
sort.Strings(dCodes)
for _, code := range dCodes {
diffs := a.Branches[code]
bank := a.NewBanks[code]
blue("===> %s(%s)'s branches diff\n", bank.Name, bank.Code)
for _, d := range diffs {
fmt.Println(" " + d.String())
}
}
}
type AddBankDiff struct {
Bank *Bank
}
func (d *AddBankDiff) String() string {
c := color.New(color.FgGreen, color.Bold).SprintFunc()
return c("+ Bank") + ": " + d.Bank.Name + "(" + d.Bank.Code + ")"
}
type RemoveBankDiff struct {
Bank *Bank
}
func (d *RemoveBankDiff) String() string {
c := color.New(color.FgRed, color.Bold).SprintFunc()
return c("- Bank") + ": " + d.Bank.Name + "(" + d.Bank.Code + ")"
}
type ChangeBankDiff struct {
OldBank *Bank
NewBank *Bank
}
func (d *ChangeBankDiff) String() string {
c := color.New(color.FgYellow, color.Bold).SprintFunc()
return c("+/- Bank") + ": " + d.OldBank.Name + " -> " + d.NewBank.Name + "(" + d.NewBank.Code + ")"
}
type AddBranchDiff struct {
Branch *Branch
}
func (d *AddBranchDiff) String() string {
c := color.New(color.FgGreen, color.Bold).SprintFunc()
return c("+ Branch") + ": " + d.Branch.Name + "(" + d.Branch.Code + ")"
}
type RemoveBranchDiff struct {
Branch *Branch
}
func (d *RemoveBranchDiff) String() string {
c := color.New(color.FgRed, color.Bold).SprintFunc()
return c("- Branch") + ": " + d.Branch.Name + "(" + d.Branch.Code + ")"
}
type ChangeBranchDiff struct {
OldBranch *Branch
NewBranch *Branch
}
func (d *ChangeBranchDiff) String() string {
c := color.New(color.FgYellow, color.Bold).SprintFunc()
return c("+/- Branch") + ": " + d.OldBranch.Name + " -> " + d.NewBranch.Name + "(" + d.NewBranch.Code + ")"
}
func DiffSourceData(old *SourceData, now *SourceData) *AllDiff {
allDiff := &AllDiff{
NewBanks: now.Banks,
Branches: map[string][]Diff{},
}
allDiff.Banks = DiffBanks(old.Banks, now.Banks)
for code, branches := range now.Branches {
if oldBranches, ok := old.Branches[code]; ok {
bdiff := DiffBranches(oldBranches, branches)
if len(bdiff) > 0 {
allDiff.Branches[code] = bdiff
}
}
}
return allDiff
}
func DiffBanks(old map[string]*Bank, now map[string]*Bank) []Diff {
found := map[string]bool{}
diffs := []Diff{}
for code, bank := range old {
found[code] = true
if nowBank, ok := now[code]; ok {
if nowBank.Name != bank.Name {
diffs = append(diffs, &ChangeBankDiff{OldBank: bank, NewBank: nowBank})
}
} else {
diffs = append(diffs, &RemoveBankDiff{Bank: bank})
}
}
for code, bank := range now {
if _, ok := found[code]; !ok {
diffs = append(diffs, &AddBankDiff{Bank: bank})
}
}
return diffs
}
func DiffBranches(old map[string]*Branch, now map[string]*Branch) []Diff {
found := map[string]bool{}
diffs := []Diff{}
for code, bank := range old {
found[code] = true
if nowBranch, ok := now[code]; ok {
if nowBranch.Name != bank.Name {
diffs = append(diffs, &ChangeBranchDiff{OldBranch: bank, NewBranch: nowBranch})
}
} else {
diffs = append(diffs, &RemoveBranchDiff{Branch: bank})
}
}
for code, bank := range now {
if _, ok := found[code]; !ok {
diffs = append(diffs, &AddBranchDiff{Branch: bank})
}
}
return diffs
}