This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
/
convert.go
86 lines (77 loc) · 2.26 KB
/
convert.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
package dialects
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"time"
)
// Converts EPOCH timestamp to isoformat string
func ConvertIsoformat(at uint64) string {
// Use this format instead of isoformat: https://msdn.microsoft.com/en-us/library/dn935026.aspx
// It's working with Amazon Redshift and Azure SQL Data Warehouse
return time.Unix(int64(at), 0).UTC().Format("2006-01-02 15:04:05")
}
// Define converter functions based on the file extension type.
type Converter func(event *Event) (*bytes.Buffer, error)
type BatchConverter func(events []*Event) (*bytes.Buffer, error)
// Returns a single event converter function based on file extension.
func GetConverterFunction(fileFormat string) (Converter, error) {
switch fileFormat {
case "json":
return ConvertJSON, nil
case "csv":
return ConvertCSV, nil
}
return nil, fmt.Errorf("Unsupported output `%s` file format (use `json` or `csv`)", fileFormat)
}
// Returns a batch event converter function based on file extension.
func GetBatchConverterFunction(fileFormat string) (BatchConverter, error) {
switch fileFormat {
case "json":
return ConvertBatchJSON, nil
case "csv":
return ConvertBatchCSV, nil
}
return nil, fmt.Errorf("Unsupported output `%s` file format (use `json` or `csv`)", fileFormat)
}
// Dumps the Event into a JSON string
func ConvertJSON(event *Event) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(event); err != nil {
return b, err
}
return b, nil
}
// Dumps the Event into a CSV string
func ConvertCSV(event *Event) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
writer := csv.NewWriter(b)
writer.Comma = '\001'
writer.Write(event.String())
writer.Flush()
return b, nil
}
// Converts multiple events into JSON string
func ConvertBatchJSON(events []*Event) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
for _, event := range events {
buffer, err := ConvertJSON(event)
if err != nil {
return b, err
}
b.WriteString(buffer.String())
}
return b, nil
}
// Converts to CSV string for list of events
func ConvertBatchCSV(events []*Event) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
writer := csv.NewWriter(b)
writer.Comma = '\001'
for _, event := range events {
writer.Write(event.String())
}
writer.Flush()
return b, nil
}