-
Notifications
You must be signed in to change notification settings - Fork 13
/
exporter.go
60 lines (47 loc) · 1.68 KB
/
exporter.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
package exporter
import (
"errors"
"strconv"
"strings"
)
type IExporter interface {
XLSXExportToFile(sheet string, columns []string, rows [][]interface{}, filePath string) error
XLSXExportToByte(sheet string, columns []string, rows [][]interface{}) ([]byte, error)
CSVExportToFile(columns []string, rows [][]interface{}, filePath string) error
CSVExportToByte(columns []string, rows [][]interface{}) ([]byte, error)
}
type Exporter struct {
xlsxExporter IXLSXExporter
csvExporter ICSVExporter
}
func NewExportService(xlsxExporter IXLSXExporter, csvExporter ICSVExporter) *Exporter {
return &Exporter{
xlsxExporter: xlsxExporter,
csvExporter: csvExporter,
}
}
func (e *Exporter) XLSXExportToFile(sheet string, columns []string, rows [][]interface{}, filePath string) error {
return e.xlsxExporter.exportToFile(sheet, columns, rows, filePath)
}
func (e *Exporter) XLSXExportToByte(sheet string, columns []string, rows [][]interface{}) ([]byte, error) {
return e.xlsxExporter.exportToByte(sheet, columns, rows)
}
func (e *Exporter) CSVExportToFile(columns []string, rows [][]interface{}, filePath string) error {
return e.csvExporter.exportToFile(columns, rows, filePath)
}
func (e *Exporter) CSVExportToByte(columns []string, rows [][]interface{}) ([]byte, error) {
return e.csvExporter.exportToByte(columns, rows)
}
func verifyRows(columns []string, rows [][]interface{}) error {
dataErrors := make([]string, 0, 1)
for rowID, row := range rows {
if len(row) != len(columns) {
dataErrors = append(dataErrors, "Different column count for row["+strconv.Itoa(rowID)+"]")
continue
}
}
if len(dataErrors) > 0 {
return errors.New(strings.Join(dataErrors, "\n"))
}
return nil
}