-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_render_as_csv.go
362 lines (317 loc) · 10.3 KB
/
errors_render_as_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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package csvlib
import (
"bytes"
"encoding/csv"
"sort"
"strconv"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/tiendc/gofn"
)
type CSVRenderConfig struct {
// CellSeparator separator to join cell error details within a row, normally a comma (`,`)
CellSeparator string
// LineBreak custom new line character (default is `\n`)
LineBreak string
// RenderHeader whether render header row or not
RenderHeader bool
// RenderRowNumberColumnIndex index of `row` column to render, set `-1` to not render it (default is `0`)
RenderRowNumberColumnIndex int
// RenderLineNumberColumnIndex index of `line` column to render, set `-1` to not render it (default is `-1`)
RenderLineNumberColumnIndex int
// RenderCommonErrorColumnIndex index of `common error` column to render, set `-1` to not render it
// (default is `1`)
RenderCommonErrorColumnIndex int
// LocalizeCellFields localize cell's fields before rendering the cell error (default is `true`)
LocalizeCellFields bool
// LocalizeCellHeader localize cell header before rendering the cell error (default is `true`)
LocalizeCellHeader bool
// Params custom params user wants to send to the localization (optional)
Params ParameterMap
// LocalizationFunc function to translate message (optional)
LocalizationFunc LocalizationFunc
// HeaderRenderFunc custom render function for rendering header row (optional)
HeaderRenderFunc func([]string, ParameterMap)
// CellRenderFunc custom render function for rendering a cell error (optional).
// The func can return ("", false) to skip rendering the cell error, return ("", true) to let the
// renderer continue using its solution, and return ("<str>", true) to override the value.
//
// Supported params:
// {{.Column}} - column index (0-based)
// {{.ColumnHeader}} - column name
// {{.Value}} - cell value
// {{.Error}} - error detail which is result of calling err.Error()
//
// Use cellErr.WithParam() to add more extra params
CellRenderFunc func(*RowErrors, *CellError, ParameterMap) (string, bool)
// CommonErrorRenderFunc renders common error (not RowErrors, CellError) (optional)
CommonErrorRenderFunc func(error, ParameterMap) (string, error)
}
func defaultCSVRenderConfig() *CSVRenderConfig {
return &CSVRenderConfig{
CellSeparator: ", ",
LineBreak: newLine,
RenderHeader: true,
RenderRowNumberColumnIndex: 0,
RenderLineNumberColumnIndex: 1,
RenderCommonErrorColumnIndex: 2, //nolint:mnd
LocalizeCellFields: true,
LocalizeCellHeader: true,
}
}
// CSVRenderer an implementation of error renderer which can produce messages
// for the input errors as CSV output data.
type CSVRenderer struct {
cfg *CSVRenderConfig
sourceErr *Errors
transErr error
numColumns int
startCellErrIndex int
data [][]string
}
// NewCSVRenderer creates a new CSVRenderer
func NewCSVRenderer(err *Errors, options ...func(*CSVRenderConfig)) (*CSVRenderer, error) {
cfg := defaultCSVRenderConfig()
for _, opt := range options {
opt(cfg)
}
// Validate/Correct the base columns to render
baseColumns := make([]*int, 0, 3) //nolint:mnd
if cfg.RenderRowNumberColumnIndex >= 0 {
baseColumns = append(baseColumns, &cfg.RenderRowNumberColumnIndex)
}
if cfg.RenderLineNumberColumnIndex >= 0 {
baseColumns = append(baseColumns, &cfg.RenderLineNumberColumnIndex)
}
if cfg.RenderCommonErrorColumnIndex >= 0 {
baseColumns = append(baseColumns, &cfg.RenderCommonErrorColumnIndex)
}
sort.Slice(baseColumns, func(i, j int) bool {
return *baseColumns[i] < *baseColumns[j]
})
for i := range baseColumns {
*baseColumns[i] = i
}
return &CSVRenderer{cfg: cfg, sourceErr: err}, nil
}
// Render renders Errors object as CSV rows data
func (r *CSVRenderer) Render() (data [][]string, transErr error, err error) {
cfg := r.cfg
r.startCellErrIndex = 0
if cfg.RenderRowNumberColumnIndex >= 0 {
r.startCellErrIndex++
}
if cfg.RenderLineNumberColumnIndex >= 0 {
r.startCellErrIndex++
}
if cfg.RenderCommonErrorColumnIndex >= 0 {
r.startCellErrIndex++
}
r.numColumns = len(r.sourceErr.Header()) + r.startCellErrIndex
errs := r.sourceErr.Unwrap()
r.data = make([][]string, 0, len(errs)+1)
params := gofn.MapUpdate(ParameterMap{
"CrLf": cfg.LineBreak,
"Tab": "\t",
"TotalRow": r.sourceErr.TotalRow(),
"TotalError": r.sourceErr.TotalError(),
"TotalRowError": r.sourceErr.TotalRowError(),
"TotalCellError": r.sourceErr.TotalCellError(),
}, cfg.Params)
// Render header row
r.renderHeader(params)
// Render rows content
for _, err := range errs {
if rowErr, ok := err.(*RowErrors); ok { // nolint: errorlint
rowContent := r.renderRow(rowErr, params)
r.data = append(r.data, rowContent)
} else {
_ = r.renderCommonError(err, params)
}
}
return r.data, r.transErr, nil
}
// RenderAsString renders the input as CSV string
func (r *CSVRenderer) RenderAsString() (msg string, transErr error, err error) {
csvData, transErr, err := r.Render()
if err != nil {
return "", transErr, err
}
buf := bytes.NewBuffer(make([]byte, 0, r.estimateCSVBuffer(csvData)))
w := csv.NewWriter(buf)
if err = w.WriteAll(csvData); err != nil {
return "", transErr, err
}
w.Flush()
return buf.String(), transErr, nil
}
// RenderTo renders the input as CSV string and writes it to the writer
func (r *CSVRenderer) RenderTo(w Writer) (transErr error, err error) {
csvData, transErr, err := r.Render()
if err != nil {
return transErr, err
}
writeAll, canWriteAll := w.(interface{ WriteAll([][]string) error })
if canWriteAll {
return transErr, writeAll.WriteAll(csvData)
}
for _, row := range csvData {
err = w.Write(row)
if err != nil {
return transErr, err
}
}
return transErr, nil
}
func (r *CSVRenderer) renderHeader(exparams ParameterMap) {
if !r.cfg.RenderHeader {
return
}
cfg := r.cfg
header := make([]string, r.numColumns)
if cfg.RenderRowNumberColumnIndex >= 0 {
header[cfg.RenderRowNumberColumnIndex] = "Row"
}
if cfg.RenderLineNumberColumnIndex >= 0 {
header[cfg.RenderLineNumberColumnIndex] = "Line"
}
if cfg.RenderCommonErrorColumnIndex >= 0 {
header[cfg.RenderCommonErrorColumnIndex] = "CommonError"
}
for i := r.startCellErrIndex; i < r.numColumns; i++ {
header[i] = r.sourceErr.header[i-r.startCellErrIndex]
}
if cfg.HeaderRenderFunc != nil {
cfg.HeaderRenderFunc(header, exparams)
}
r.data = append(r.data, header)
}
func (r *CSVRenderer) renderRow(rowErr *RowErrors, exparams ParameterMap) []string {
cfg := r.cfg
content := make([]string, r.numColumns)
if cfg.RenderRowNumberColumnIndex >= 0 {
content[cfg.RenderRowNumberColumnIndex] = strconv.FormatInt(int64(rowErr.row), 10)
}
if cfg.RenderLineNumberColumnIndex >= 0 {
content[cfg.RenderLineNumberColumnIndex] = strconv.FormatInt(int64(rowErr.line), 10)
}
errs := rowErr.Unwrap()
mapErrByIndex := make(map[int][]string, r.numColumns)
params := gofn.MapUpdate(ParameterMap{}, exparams)
params["Row"] = rowErr.Row()
params["Line"] = rowErr.Line()
for _, err := range errs {
if cellErr, ok := err.(*CellError); ok { // nolint: errorlint
detail := r.renderCell(rowErr, cellErr, params)
colIndex := cellErr.column + r.startCellErrIndex
if cellErr.column == -1 {
colIndex = cfg.RenderCommonErrorColumnIndex
}
if listItems, ok := mapErrByIndex[colIndex]; ok {
mapErrByIndex[colIndex] = append(listItems, detail)
} else {
mapErrByIndex[colIndex] = []string{detail}
}
continue
}
// Common error
detail := r.renderCommonError(err, params)
if listItems, ok := mapErrByIndex[cfg.RenderCommonErrorColumnIndex]; ok {
mapErrByIndex[cfg.RenderCommonErrorColumnIndex] = append(listItems, detail)
} else {
mapErrByIndex[cfg.RenderCommonErrorColumnIndex] = []string{detail}
}
}
for index, items := range mapErrByIndex {
content[index] = strings.Join(items, cfg.CellSeparator)
}
return content
}
func (r *CSVRenderer) renderCell(rowErr *RowErrors, cellErr *CellError, exparams ParameterMap) string {
params := gofn.MapUpdate(ParameterMap{}, exparams)
params = gofn.MapUpdate(params, r.renderCellFields(cellErr, params))
params["Column"] = cellErr.Column()
params["ColumnHeader"] = r.renderCellHeader(cellErr, params)
params["Value"] = cellErr.Value()
params["Error"] = cellErr.Error()
if r.cfg.CellRenderFunc != nil {
msg, flag := r.cfg.CellRenderFunc(rowErr, cellErr, exparams)
if !flag {
return ""
}
if msg != "" {
return msg
}
}
locKey := cellErr.LocalizationKey()
if locKey == "" {
locKey = cellErr.Error()
}
return r.localizeKeySkipError(locKey, params)
}
func (r *CSVRenderer) renderCellFields(cellErr *CellError, params ParameterMap) ParameterMap {
if !r.cfg.LocalizeCellFields {
return cellErr.fields
}
result := make(ParameterMap, len(cellErr.fields))
for k, v := range cellErr.fields {
vAsStr, ok := v.(string)
if !ok {
result[k] = v
continue
}
if translated, err := r.localizeKey(vAsStr, params); err != nil {
result[k] = v
} else {
result[k] = translated
}
}
return result
}
func (r *CSVRenderer) renderCellHeader(cellErr *CellError, params ParameterMap) string {
if !r.cfg.LocalizeCellHeader {
return cellErr.Header()
}
return r.localizeKeySkipError(cellErr.Header(), params)
}
func (r *CSVRenderer) renderCommonError(err error, params ParameterMap) string {
if r.cfg.CommonErrorRenderFunc == nil {
return r.localizeKeySkipError(err.Error(), params)
}
msg, err := r.cfg.CommonErrorRenderFunc(err, params)
if err != nil {
r.transErr = multierror.Append(r.transErr, err)
}
return msg
}
func (r *CSVRenderer) localizeKey(key string, params ParameterMap) (string, error) {
if r.cfg.LocalizationFunc == nil {
return processTemplate(key, params)
}
msg, err := r.cfg.LocalizationFunc(key, params)
if err != nil {
err = multierror.Append(ErrLocalization, err)
r.transErr = multierror.Append(r.transErr, err)
return "", err
}
return msg, nil
}
func (r *CSVRenderer) localizeKeySkipError(key string, params ParameterMap) string {
s, err := r.localizeKey(key, params)
if err == nil || r.cfg.LocalizationFunc == nil {
return s
}
s, _ = processTemplate(key, params)
return s
}
func (r *CSVRenderer) estimateCSVBuffer(data [][]string) int {
if len(data) <= 1 {
return 512 //nolint:mnd
}
row := data[1]
rowSz := 0
for _, v := range row {
rowSz += len(v)
}
return (rowSz + len(row)) * len(data)
}