-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv2json.go
208 lines (163 loc) · 4.2 KB
/
csv2json.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
package main
import (
"encoding/csv"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type inputFile struct {
filepath string
separator string
pretty bool
}
func exitGracefully(err error) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
func check(e error) {
if e != nil {
exitGracefully(e)
}
}
func getFileData() (inputFile, error) {
// Validate arguments
if len(os.Args) < 2 {
return inputFile{}, errors.New("A filepath argument is required")
}
separator := flag.String("separator", "comma", "Column separator")
pretty := flag.Bool("pretty", false, "Generate pretty JSON")
flag.Parse()
fileLocation := flag.Arg(0)
if !(*separator == "comma" || *separator == "semicolon") {
return inputFile{}, errors.New("only comma or semicolon separators are allowed")
}
return inputFile{fileLocation, *separator, *pretty}, nil
}
func checkIfValidFile(filename string) (bool, error) {
// Check if file is CSV
if fileExtension := filepath.Ext(filename); fileExtension != ".csv" {
return false, fmt.Errorf("file %s is not CSV", filename)
}
// Check if file does exist
if _, err := os.Stat(filename); err != nil && os.IsNotExist(err) {
return false, fmt.Errorf("file %s does not exist", filename)
}
return true, nil
}
func processLine(headers []string, dataList []string) (map[string]string, error) {
if len(dataList) != len(headers) {
return nil, errors.New("line doesn't match headers format. Skipping")
}
recordMap := make(map[string]string)
for i, name := range headers {
recordMap[name] = dataList[i]
}
return recordMap, nil
}
func processCsvFile(fileData inputFile, writerChannel chan<- map[string]string) {
file, err := os.Open(fileData.filepath)
check(err)
defer file.Close()
// Get Headers
var headers, line []string
reader := csv.NewReader(file)
if fileData.separator == "semicolon" {
reader.Comma = ';'
}
headers, err = reader.Read()
check(err)
for {
line, err = reader.Read()
if err == io.EOF {
close(writerChannel)
break
} else if err != nil {
exitGracefully(err)
}
record, err := processLine(headers, line)
if err != nil {
fmt.Printf("Line: %sError: %s\n", line, err)
continue
}
writerChannel <- record
}
}
func createStringWriter(csvPath string) func(string, bool) {
jsonDir := filepath.Dir(csvPath)
jsonName := fmt.Sprintf("%s.json", strings.TrimSuffix(filepath.Base(csvPath), ".csv"))
finalLocation := fmt.Sprintf("%s/%s", jsonDir, jsonName)
f, err := os.Create(finalLocation)
check(err)
return func(data string, close bool) {
_, err := f.WriteString(data)
check(err)
if close {
f.Close()
}
}
}
func getJSONFunc(pretty bool) (func(map[string]string) string, string) {
var jsonFunc func(map[string]string) string
var breakLine string
if pretty {
breakLine = "\n"
jsonFunc = func(record map[string]string) string {
jsonData, _ := json.MarshalIndent(record, " ", " ")
return " " + string(jsonData)
}
} else {
breakLine = ""
jsonFunc = func(record map[string]string) string {
jsonData, _ := json.Marshal(record)
return string(jsonData)
}
}
return jsonFunc, breakLine
}
func writeJSONFile(csvPath string, writerChannel <-chan map[string]string, done chan<- bool, pretty bool) {
writeString := createStringWriter(csvPath)
jsonFunc, breakLine := getJSONFunc(pretty)
fmt.Println("Writing JSON file...")
writeString("["+breakLine, false)
first := true
for {
record, more := <-writerChannel
if more {
if !first {
writeString(","+breakLine, false)
} else {
first = false
}
jsonData := jsonFunc(record)
writeString(jsonData, false)
} else {
writeString(breakLine+"]", true)
fmt.Println("Completed!")
done <- true
break
}
}
}
func main() {
flag.Usage = func() {
fmt.Printf("Usage: %s [options] <csvFile>\nOptions:\n", os.Args[0])
flag.PrintDefaults()
}
fileData, err := getFileData()
if err != nil {
exitGracefully(err)
}
if _, err := checkIfValidFile(fileData.filepath); err != nil {
exitGracefully(err)
}
writerChannel := make(chan map[string]string)
done := make(chan bool)
go processCsvFile(fileData, writerChannel)
go writeJSONFile(fileData.filepath, writerChannel, done, fileData.pretty)
<-done
}