-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvdiff.go
258 lines (213 loc) · 6.91 KB
/
csvdiff.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
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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"io"
"io/ioutil"
"log"
"os"
)
type Options struct {
Pks []string `long:"pk" description:"primary keys"`
Output string `short:"o" long:"output" description:"file path where output json is written"`
}
type DiffJson struct {
Added []map[string]string `json:"added"`
Modified []ModifiedContent `json:"modified"`
Deleted []map[string]string `json:"deleted"`
AddedColumns []string `json:"addedColumns"`
DeletedColumns []string `json:"deletedColumns"`
}
type ModifiedContent struct {
Pks map[string]string `json:"pks"`
FromTo map[string]FromTo `json:"fromTo"`
}
type FromTo struct {
From string `json:"from"`
To string `json:"to"`
}
func sliceDiff(slice1 []string, slice2 []string) ([]string, []string) {
elemsOnlyInSlice1 := []string{}
elemsOnlyInSlice2 := []string{}
slice1Map := map[string]bool{}
for _, slice1Elem := range slice1 {
slice1Map[slice1Elem] = true
}
for _, slice2Elem := range slice2 {
_, exist := slice1Map[slice2Elem]
if !exist {
elemsOnlyInSlice2 = append(elemsOnlyInSlice2, slice2Elem)
}
delete(slice1Map, slice2Elem)
}
for elemOnlyInSlice1, _ := range slice1Map {
elemsOnlyInSlice1 = append(elemsOnlyInSlice1, elemOnlyInSlice1)
}
return elemsOnlyInSlice1, elemsOnlyInSlice2
}
func getPkIdxList(colNames []string, pks []string) []int {
pkIdxList := []int{}
for i, colName := range colNames {
for _, pk := range pks {
if colName == pk {
pkIdxList = append(pkIdxList, i)
}
}
}
return pkIdxList
}
// return "pk(1):::pk(2):::...:::pk(n)" string
func getPkValuesStr(record []string, pkIdxList []int) string {
pkValues := make([]byte, 0, 128)
for i, pkIdx := range pkIdxList {
pkValues = append(pkValues, record[pkIdx]...)
if i < len(pkIdxList) - 1 {
pkValues = append(pkValues, ":::"...)
}
}
return string(pkValues)
}
func parseCsv(baseCsv io.Reader, pks []string) ([]string, map[string][]string) {
colNames := []string{}
pkValuesStrToRecord := map[string][]string{}
pkIdxList := []int{}
r := csv.NewReader(baseCsv)
r.LazyQuotes = true
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
// column names are written in the first line
if len(colNames) == 0 {
colNames = record
pkIdxList = getPkIdxList(colNames, pks)
continue
}
pkValuesStr := getPkValuesStr(record, pkIdxList)
pkValuesStrToRecord[pkValuesStr] = record
}
return colNames, pkValuesStrToRecord
}
func csvDiff(baseCsv, comparisonCsv io.Reader, pks []string) []byte {
diffJson := DiffJson{}
diffJson.Added = []map[string]string{}
diffJson.Modified = []ModifiedContent{}
diffJson.Deleted = []map[string]string{}
baseColNames, basePkValuesStrToRecord := parseCsv(baseCsv, pks)
pkIdxList := []int{}
r := csv.NewReader(comparisonCsv)
r.LazyQuotes = true
comparisonColNames := []string{}
for {
comparisonRecord, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
// column names are written in the first line
if len(comparisonColNames) == 0 {
comparisonColNames = comparisonRecord
pkIdxList = getPkIdxList(comparisonColNames, pks)
continue
}
pkValuesStr := getPkValuesStr(comparisonRecord, pkIdxList)
baseRecord, exist := basePkValuesStrToRecord[pkValuesStr]
comparisonColNameToVal := map[string]string{}
for i, colName := range comparisonColNames {
comparisonColNameToVal[colName] = comparisonRecord[i]
}
if exist {
modifiedContent := ModifiedContent{}
modifiedContent.FromTo = map[string]FromTo{}
existModifiedVal := false
for i, colName := range baseColNames {
comparisonValue, exist := comparisonColNameToVal[colName]
if !exist {
continue
}
if comparisonValue != baseRecord[i] {
existModifiedVal = true
if len(modifiedContent.Pks) == 0 {
modifiedContent.Pks = map[string]string{}
for _, pkName := range pks {
modifiedContent.Pks[pkName] = comparisonColNameToVal[pkName]
}
}
fromTo := FromTo{}
fromTo.From = baseRecord[i]
fromTo.To = comparisonValue
modifiedContent.FromTo[colName] = fromTo
}
}
if existModifiedVal {
diffJson.Modified = append(diffJson.Modified, modifiedContent)
}
delete(basePkValuesStrToRecord, pkValuesStr)
} else {
diffJson.Added = append(diffJson.Added, comparisonColNameToVal)
}
}
for _, record := range basePkValuesStrToRecord {
baseColNameToVal := map[string]string{}
for i, colName := range baseColNames {
baseColNameToVal[colName] = record[i]
}
diffJson.Deleted = append(diffJson.Deleted, baseColNameToVal)
}
deletedColumns, addedColumns := sliceDiff(baseColNames, comparisonColNames)
diffJson.AddedColumns = addedColumns
diffJson.DeletedColumns = deletedColumns
outputJson, err := json.Marshal(&diffJson)
if err != nil {
log.Fatal(err)
}
return outputJson
}
func main() {
opts := Options{}
parser := flags.NewParser(&opts, flags.Default)
parser.Usage = "base.csv compare.csv [OPTIONS]"
args, err := parser.Parse()
// exit 0 early if help option is added
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if len(args) != 2 {
fmt.Println("argument num is invalid\n")
parser.WriteHelp(os.Stdout)
os.Exit(1)
}
if len(opts.Pks) == 0 {
fmt.Println("At least one pk option must be set\n")
parser.WriteHelp(os.Stdout)
os.Exit(1)
}
baseCsv, err := os.Open(args[0])
if err != nil {
log.Fatal(err)
}
defer baseCsv.Close()
comparisonCsv, err := os.Open(args[1])
if err != nil {
log.Fatal(err)
}
defer comparisonCsv.Close()
outputJson := csvDiff(baseCsv, comparisonCsv, opts.Pks)
if len(opts.Output) > 0 {
ioutil.WriteFile(opts.Output, outputJson, os.ModePerm)
} else {
fmt.Println(string(outputJson))
}
}