-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·183 lines (150 loc) · 4.08 KB
/
main.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
package main
// #include <string.h>
// #include <stdbool.h>
// #include <mysql.h>
// #cgo CFLAGS: -O3 -I/usr/include/mysql -fno-omit-frame-pointer
import "C"
import (
"encoding/csv"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"unsafe"
)
// main function is needed even for generating shared object files
func main() {}
var l = log.New(os.Stderr, "csv-to-json: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
func msg(message *C.char, s string) {
m := C.CString(s)
defer C.free(unsafe.Pointer(m))
C.strcpy(message, m)
}
//export csv_to_json_init
func csv_to_json_init(initid *C.UDF_INIT, args *C.UDF_ARGS, message *C.char) C.bool {
if args.arg_count != 1 {
msg(message, "`csv_to_json` requires 1 parameter: the CSV file string to be encoded")
return C.bool(true)
}
argsTypes := (*[2]uint32)(unsafe.Pointer(args.arg_type))
argsTypes[0] = C.STRING_RESULT
initid.maybe_null = 1
return C.bool(false)
}
//export csv_to_json
func csv_to_json(initid *C.UDF_INIT, args *C.UDF_ARGS, result *C.char, length *uint64, isNull *C.char, message *C.char) *C.char {
c := 1
argsArgs := (*[1 << 30]*C.char)(unsafe.Pointer(args.args))[:c:c]
argsLengths := (*[1 << 30]uint64)(unsafe.Pointer(args.lengths))[:c:c]
*length = 0
*isNull = 1
if argsArgs[0] == nil {
return nil
}
a := make([]string, c, c)
for i, argsArg := range argsArgs {
a[i] = C.GoStringN(argsArg, C.int(argsLengths[i]))
}
csvr := csv.NewReader(strings.NewReader(a[0]))
csvr.FieldsPerRecord = -1
csvr.LazyQuotes = true
csvr.TrimLeadingSpace = true
stringSlices, err := csvr.ReadAll()
if err != nil {
l.Println(err)
return nil
}
var b []byte
if len(stringSlices) > 2 {
objects := make([]map[string]string, 0, len(stringSlices)-1)
h := stringSlices[0]
uniqueColumns := make(map[string]struct{}, len(h))
columnIndexes := make(map[int]string, len(h))
stringSlices = stringSlices[1:]
for _, slice := range stringSlices {
newObject := make(map[string]string, len(h))
for i, s := range slice {
var column string
var ok bool
if column, ok = columnIndexes[i]; !ok {
var newColumn string
if i < len(h) {
newColumn = strings.TrimSpace(h[i])
}
originalName := newColumn
j := 2
for {
if _, ok := uniqueColumns[newColumn]; !ok {
break
}
newColumn = strings.TrimSpace(fmt.Sprintf("%s %d", originalName, j))
j++
}
column = newColumn
columnIndexes[i] = newColumn
uniqueColumns[newColumn] = struct{}{}
}
newObject[column] = s
}
objects = append(objects, newObject)
}
b, err = json.Marshal(objects)
if err != nil {
l.Println(err)
return nil
}
}
if b == nil {
b = []byte("[]")
}
*length = uint64(len(b))
*isNull = 0
return C.CString(string(b))
}
//export csv_to_json_no_headers_init
func csv_to_json_no_headers_init(initid *C.UDF_INIT, args *C.UDF_ARGS, message *C.char) C.bool {
if args.arg_count != 1 {
msg(message, "`csv_to_json_no_headers` requires 1 parameter: the CSV file string to be encoded")
return C.bool(true)
}
argsTypes := (*[2]uint32)(unsafe.Pointer(args.arg_type))
argsTypes[0] = C.STRING_RESULT
initid.maybe_null = 1
return C.bool(false)
}
//export csv_to_json_no_headers
func csv_to_json_no_headers(initid *C.UDF_INIT, args *C.UDF_ARGS, result *C.char, length *uint64, isNull *C.char, message *C.char) *C.char {
c := 1
argsArgs := (*[1 << 30]*C.char)(unsafe.Pointer(args.args))[:c:c]
argsLengths := (*[1 << 30]uint64)(unsafe.Pointer(args.lengths))[:c:c]
*length = 0
*isNull = 1
if argsArgs[0] == nil {
return nil
}
a := make([]string, c, c)
for i, argsArg := range argsArgs {
a[i] = C.GoStringN(argsArg, C.int(argsLengths[i]))
}
csvr := csv.NewReader(strings.NewReader(a[0]))
csvr.FieldsPerRecord = -1
csvr.LazyQuotes = true
csvr.TrimLeadingSpace = true
stringSlices, err := csvr.ReadAll()
if err != nil {
l.Println(err)
return nil
}
if stringSlices == nil {
stringSlices = make([][]string, 0)
}
b, err := json.Marshal(stringSlices)
if err != nil {
l.Println(err)
return nil
}
*length = uint64(len(b))
*isNull = 0
return C.CString(string(b))
}