-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffTool.go
More file actions
264 lines (241 loc) · 6 KB
/
diffTool.go
File metadata and controls
264 lines (241 loc) · 6 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"fmt"
"github.com/burch-cm/xlsx"
xxh "github.com/burch-cm/xxhash"
"github.com/deckarep/golang-set"
"log"
"regexp"
"strings"
)
// Type rowMap holds a slice of columns values as strings mapped to a key as string.
type rowMap map[string][]string
type diffMap map[string]map[string][]string
// Type rowMapKey holds the set difference and intersection of the keys from two rowMaps
type rowMapKey struct {
removed []string
added []string
intersection []string
different []string
}
// Function Lower() conversts all values in a string slice to lower case
func (r rowMap) Lower() {
for key, val := range r {
var lower []string
for _, j := range val {
lower = append(lower, strings.ToLower(j))
}
r[key] = lower
}
}
// Method colHash applies the xxhash algorithm to each value in a rowMap column slice and returns those values.
func (r rowMap) colHash(key string) []uint64 {
t := r[key]
var out []uint64
for _, v := range t {
out = append(out, xxh.Sum64String(v))
}
return out
}
// Method rowHash applies the xxhash algorithm to each value in a rowMap column slice and returns the sum.
func (r rowMap) rowHash(key string) uint64 {
vals := r.colHash(key)
var sum uint64 = 0
for _, v := range vals {
sum += v
}
return sum
}
// converts to type String
func conv2str(t interface{}) string {
return fmt.Sprint(t)
}
//
func getColNames(xlfile string) []string {
mySlice, err := xlsx.FileToSliceNlines(xlfile, 1)
if err != nil {
log.Fatalf("Unable to open file: %s\n", err)
}
return mySlice[0][0]
}
//RowMap reads in an excel file and returns a rowMap object and a string slice of column names.
func RowMap(xlfile string, indexpos int) (r rowMap, colnames []string) {
mySlice, err := xlsx.FileToSlice(xlfile)
if err != nil {
log.Fatalf("Unable to read file: %s\n", err)
}
ncol := len(mySlice[0][0])
nrow := len(mySlice[0])
var colNames []string
for i := 0; i < ncol; i++ {
colNames = append(colNames, (mySlice[0][0][i]))
}
var myMap = make(rowMap)
for i := 1; i < nrow; i++ {
keyval := mySlice[0][i][indexpos]
myMap[keyval] = mySlice[0][i]
}
myMap.Lower()
return myMap, colNames
}
// compare the keys between two files
func CompKeys(m1, m2 rowMap) rowMapKey {
m1_key := mapset.NewSet()
m2_key := mapset.NewSet()
var out rowMapKey
for key := range m1 {
m1_key.Add(key)
}
for key := range m2 {
m2_key.Add(key)
}
removed := m1_key.Difference(m2_key).ToSlice()
for _, i := range removed {
out.removed = append(out.removed, conv2str(i))
}
added := m2_key.Difference(m1_key).ToSlice()
for _, i := range added {
out.added = append(out.added, conv2str(i))
}
same := m1_key.Intersect(m2_key).ToSlice()
for _, i := range same {
out.intersection = append(out.intersection, conv2str(i))
}
return out
}
func sameHash(m1, m2 rowMap, keyval string) bool {
if m1.rowHash(keyval) == m2.rowHash(keyval) {
return true
}
return false
}
func Difference(xlfile1, xlfile2 string, indexpos int) (diffMap, []string) {
rm1, colnames1 := RowMap(xlfile1, indexpos)
rm2, _ := RowMap(xlfile2, indexpos)
keyset := CompKeys(rm1, rm2)
for _, v := range keyset.intersection {
if sameHash(rm1, rm2, v) == false {
keyset.different = append(keyset.different, v)
}
}
var outMap = make(diffMap)
outMap["colnames"] = make(map[string][]string)
outMap["colnames"]["colnames"] = colnames1
outMap["colnames"]["type"] = []string{"colnames"}
for _, v := range keyset.different {
outMap[v] = make(map[string][]string)
outMap[v]["type"] = []string{"different"}
outMap[v]["old"] = rm1[v]
outMap[v]["new"] = rm2[v]
}
for _, v := range keyset.added {
outMap[v] = make(map[string][]string)
outMap[v]["type"] = []string{"added"}
outMap[v]["new"] = rm2[v]
}
for _, v := range keyset.removed {
outMap[v] = make(map[string][]string)
outMap[v]["type"] = []string{"removed"}
outMap[v]["old"] = rm1[v]
}
return outMap, colnames1
}
// as a method for a diffMap
func (diff diffMap) writeFile(f string) bool {
matched, err := regexp.MatchString(".xlsx$|.csv$", f)
if matched != true {
f = f + ".xlsx"
}
var file *xlsx.File
var sheet *xlsx.Sheet
style := xlsx.NewStyle()
myFont := xlsx.NewFont(11, "Calibri")
style.Font = *myFont
file = xlsx.NewFile()
// Differences
sheet, err = file.AddSheet("Differences")
if err != nil {
fmt.Println(err.Error())
}
namerow := sheet.AddRow()
cell := namerow.AddCell()
cell.Value = "Row Source"
cell.SetStyle(style)
for _, v := range diff["colnames"]["colnames"] {
cell := namerow.AddCell()
cell.Value = v
cell.SetStyle(style)
}
for _, v := range diff {
if v["type"][0] == "different" {
oldrow := sheet.AddRow()
cell := oldrow.AddCell()
cell.Value = "file 1"
cell.SetStyle(style)
for _, j := range v["old"] {
cell := oldrow.AddCell()
cell.Value = j
cell.SetStyle(style)
}
newrow := sheet.AddRow()
cell = newrow.AddCell()
cell.Value = "file 2"
cell.SetStyle(style)
for _, j := range v["new"] {
cell := newrow.AddCell()
cell.Value = j
cell.SetStyle(style)
}
}
}
// end Differences
sheet, err = file.AddSheet("Added")
if err != nil {
fmt.Println(err.Error())
}
namerow = sheet.AddRow()
for _, v := range diff["colnames"]["colnames"] {
cell := namerow.AddCell()
cell.Value = v
cell.SetStyle(style)
}
for _, v := range diff {
if v["type"][0] == "added" {
oldrow := sheet.AddRow()
for _, j := range v["new"] {
cell := oldrow.AddCell()
cell.Value = j
cell.SetStyle(style)
}
}
}
// end Additions
sheet, err = file.AddSheet("Removed")
if err != nil {
fmt.Println(err.Error())
}
namerow = sheet.AddRow()
for _, v := range diff["colnames"]["colnames"] {
cell := namerow.AddCell()
cell.Value = v
cell.SetStyle(style)
}
for _, v := range diff {
if v["type"][0] == "removed" {
oldrow := sheet.AddRow()
for _, j := range v["old"] {
cell := oldrow.AddCell()
cell.Value = j
cell.SetStyle(style)
}
}
}
// end Removed
err = file.Save(f)
if err != nil {
fmt.Println(err.Error())
return false
} else {
return true
}
}