-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
181 lines (164 loc) · 3.66 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
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
package csvutil
import (
"encoding/json"
"encoding/xml"
htmpl "html/template"
"io"
"strconv"
txtmpl "text/template"
yaml "gopkg.in/yaml.v2"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
)
var supportedFormats = []string{"markdown", "json", "yaml", "html", "xml"}
type convertContext struct {
Headers []string
Data [][]string
}
func (ctx convertContext) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "csv"
e.EncodeToken(start)
for _, l := range ctx.Data {
de := xml.StartElement{
Name: xml.Name{Local: "data"},
}
e.EncodeToken(de)
for i, h := range ctx.Headers {
se := xml.StartElement{
Name: xml.Name{Local: h},
}
e.EncodeElement(l[i], se)
}
e.EncodeToken(de.End())
}
e.EncodeToken(start.End())
return nil
}
// ConvertOption is option holder for Convert.
type ConvertOption struct {
// Source file does not have header line. (default false)
NoHeader bool
// Encoding of source file. (default utf8)
Encoding string
// Format of converter
Format string
// Template for convert
Template string
// HTML custom template is given
HTML bool
}
func (o ConvertOption) validate() error {
if o.Format != "" && !containsString(supportedFormats, o.Format) {
return errors.Errorf("unsupported format: %s", o.Format)
}
if o.Format == "" && o.Template == "" {
return errors.New("required format or template")
}
return nil
}
// Convert CSV.
func Convert(r io.Reader, w io.Writer, o ConvertOption) error {
if err := o.validate(); err != nil {
return errors.Wrap(err, "invalid option")
}
cr, _ := reader(r, o.Encoding)
recs, err := cr.ReadAll()
if err != nil {
return err
}
if len(recs) == 0 {
w.Write([]byte(""))
return nil
}
ctx := convertContext{
Headers: make([]string, len(recs[0])),
Data: nil,
}
if o.NoHeader {
for i := 0; i < len(recs[0]); i++ {
ctx.Headers[i] = "column" + strconv.Itoa(i+1)
}
ctx.Data = recs
} else {
for i, h := range recs[0] {
ctx.Headers[i] = h
}
ctx.Data = recs[1:]
}
var p []byte
if o.Template != "" {
if o.HTML {
var t *htmpl.Template
if t, err = htmpl.New("text").Parse(o.Template); err == nil {
err = t.Execute(w, ctx)
}
} else {
var t *txtmpl.Template
if t, err = txtmpl.New("text").Parse(o.Template); err == nil {
err = t.Execute(w, ctx)
}
}
} else if o.Format == "markdown" {
tw := tablewriter.NewWriter(w)
tw.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
tw.SetCenterSeparator("|")
tw.SetAutoFormatHeaders(false)
tw.SetHeader(ctx.Headers)
tw.AppendBulk(ctx.Data)
tw.Render()
} else if o.Format == "json" {
p, err = json.MarshalIndent(marshalee(ctx), "", "\t")
} else if o.Format == "yaml" {
p, err = yaml.Marshal(marshalee(ctx))
} else if o.Format == "html" {
var t *htmpl.Template
if t, err = htmpl.New("html").Parse(htmlTmpl()); err == nil {
err = t.Execute(w, ctx)
}
} else if o.Format == "xml" {
p, err = xml.MarshalIndent(ctx, "", "\t")
}
if err != nil {
return err
}
_, err = w.Write(appendLastNewLine(p))
return err
}
func htmlTmpl() string {
return `<table>
<thead>
<tr>
{{- range .Headers}}
<th>{{.}}</th>
{{- end}}
</tr>
</thead>
<tbody>
{{- range .Data}}
<tr>
{{- range .}}
<td>{{.}}</td>
{{- end}}
</tr>
{{- end}}
</tbody>
</table>
`
}
func marshalee(ctx convertContext) []map[string]string {
ret := make([]map[string]string, len(ctx.Data))
for i, l := range ctx.Data {
m := make(map[string]string)
for j, h := range ctx.Headers {
m[h] = l[j]
}
ret[i] = m
}
return ret
}
func appendLastNewLine(p []byte) []byte {
if len(p) == 0 || p[len(p)-1] == 10 {
return p
}
return append(p, 10)
}