-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
81 lines (71 loc) · 1.92 KB
/
csv.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
package goquery
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/google/uuid"
"github.com/stoewer/go-strcase"
)
func RowsToCSV(rows Rows, toCamelCase bool, dateFormat string) (string, error) {
columns, err := rows.Columns()
if err != nil {
return "", fmt.Errorf("column error: %v", err)
}
ct, err := rows.ColumnTypes()
if err != nil {
return "", fmt.Errorf("column type error: %v", err)
}
types := make([]reflect.Type, len(ct))
for i, tp := range ct {
types[i] = tp
}
values := make([]interface{}, len(ct))
var builder strings.Builder
for i, col := range columns {
if i > 0 {
builder.WriteString(",")
}
if toCamelCase {
col = strcase.LowerCamelCase(col)
}
builder.WriteString(`"` + col + `"`)
}
builder.WriteString("\n")
for rows.Next() {
for i := range values {
values[i] = reflect.New(types[i]).Interface()
}
err = rows.Scan(values...)
if err != nil {
return "", fmt.Errorf("failed to scan values: %v", err)
}
for i, v := range values {
if i > 0 {
builder.WriteString(",")
}
var valstring string
switch v.(type) {
case string, *string:
valstring = fmt.Sprintf("\"%s\"", reflect.ValueOf(v).Elem().String())
case int, *int, int32, *int32, int64, *int64:
valstring = fmt.Sprintf("%d", reflect.ValueOf(v).Elem().Int())
case float32, *float32, float64, *float64:
valstring = fmt.Sprintf("%f", reflect.ValueOf(v).Elem().Float())
case time.Time, *time.Time:
if dateFormat != "" {
valstring = reflect.ValueOf(v).Elem().Interface().(time.Time).Format(dateFormat)
} else {
valstring = reflect.ValueOf(v).Elem().Interface().(time.Time).String()
}
case uuid.UUID:
valstring = v.(*uuid.UUID).String()
default:
return "", fmt.Errorf("unsupported csv conversion type: %v", reflect.ValueOf(v).Elem().Type())
}
builder.WriteString(fmt.Sprintf("%v", valstring))
}
builder.WriteString("\n")
}
return builder.String(), nil
}