-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
579 lines (511 loc) · 14.6 KB
/
encoder.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package csvlib
import (
"fmt"
"reflect"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/tiendc/gofn"
)
// EncodeConfig configuration for encoding Go structs as CSV data
type EncodeConfig struct {
// TagName tag name to parse the struct (default is `csv`)
TagName string
// NoHeaderMode indicates whether to write header or not (default is `false`)
NoHeaderMode bool
// LocalizeHeader indicates whether to localize the header or not (default is `false`)
LocalizeHeader bool
// LocalizationFunc localization function, required when LocalizeHeader is true
LocalizationFunc LocalizationFunc
// columnConfigMap a map consists of configuration for specific columns (optional)
columnConfigMap map[string]*EncodeColumnConfig
}
func defaultEncodeConfig() *EncodeConfig {
return &EncodeConfig{
TagName: DefaultTagName,
}
}
// ConfigureColumn configures encoding for a column by name
func (c *EncodeConfig) ConfigureColumn(name string, fn func(*EncodeColumnConfig)) {
if c.columnConfigMap == nil {
c.columnConfigMap = map[string]*EncodeColumnConfig{}
}
columnCfg, ok := c.columnConfigMap[name]
if !ok {
columnCfg = defaultEncodeColumnConfig()
c.columnConfigMap[name] = columnCfg
}
fn(columnCfg)
}
// EncodeColumnConfig configuration for encoding a specific column
type EncodeColumnConfig struct {
// Skip whether skip encoding the column or not (this is equivalent to use `csv:"-"` in struct tag)
// (default is `false`)
Skip bool
// EncodeFunc custom encode function (optional)
EncodeFunc EncodeFunc
// PostprocessorFuncs a list of functions will be called after encoding a cell value (optional)
PostprocessorFuncs []ProcessorFunc
}
func defaultEncodeColumnConfig() *EncodeColumnConfig {
return &EncodeColumnConfig{}
}
// EncodeOption function to modify encoding config
type EncodeOption func(cfg *EncodeConfig)
// Encoder data structure of the default encoder
type Encoder struct {
w Writer
cfg *EncodeConfig
err error
finished bool
itemType reflect.Type
headerWritten bool
hasDynamicInlineColumns bool
hasFixedInlineColumns bool
colsMeta []*encodeColumnMeta
}
// NewEncoder creates a new Encoder object
func NewEncoder(w Writer, options ...EncodeOption) *Encoder {
cfg := defaultEncodeConfig()
for _, opt := range options {
opt(cfg)
}
return &Encoder{
w: w,
cfg: cfg,
}
}
// Encode encode input data stored in the given variable.
// The input var must be a slice, e.g. `[]Student` or `[]*Student`.
func (e *Encoder) Encode(v any) error {
if e.finished {
return ErrFinished
}
if e.err != nil {
return ErrAlreadyFailed
}
val := reflect.ValueOf(v)
if e.itemType == nil {
if err := e.prepareEncode(val); err != nil {
e.err = err
return err
}
} else {
itemType, err := e.parseInputVar(val)
if err != nil {
return err
}
if itemType != e.itemType {
return fmt.Errorf("%w: %v (expect %v)", ErrTypeUnmatched, itemType, e.itemType)
}
}
totalRow := val.Len()
itemKindIsPtr := e.itemType.Kind() == reflect.Pointer
for row := 0; row < totalRow; row++ {
rowVal := val.Index(row)
if itemKindIsPtr {
if rowVal.IsNil() {
continue
}
rowVal = rowVal.Elem()
}
if err := e.encodeRow(rowVal); err != nil {
e.err = err
break
}
}
return e.err
}
// EncodeOne encode single object into a single CSV row
func (e *Encoder) EncodeOne(v any) error {
if e.finished {
return ErrFinished
}
if e.err != nil {
return ErrAlreadyFailed
}
rowVal := reflect.ValueOf(v)
itemType := rowVal.Type()
if !isKindOrPtrOf(itemType, reflect.Struct) {
return fmt.Errorf("%w: must be a struct", ErrTypeInvalid)
}
if e.itemType == nil {
slice := reflect.MakeSlice(reflect.SliceOf(itemType), 1, 1)
slice.Index(0).Set(rowVal)
err := e.prepareEncode(slice)
if err != nil {
e.err = err
return err
}
} else if itemType != e.itemType {
return fmt.Errorf("%w: %v (expect %v)", ErrTypeUnmatched, itemType, e.itemType)
}
if err := e.encodeRow(rowVal); err != nil {
e.err = err
return err
}
return nil
}
// Finish encoding, after calling this func, you can't encode more
func (e *Encoder) Finish() error {
e.finished = true
return e.err
}
func (e *Encoder) prepareEncode(v reflect.Value) error {
if e.itemType != nil {
return fmt.Errorf("%w: item type already parsed", ErrUnexpected)
}
itemType, err := e.parseInputVar(v)
if err != nil {
return err
}
e.itemType = itemType
if err = e.validateConfig(); err != nil {
return err
}
if err = e.parseColumnsMeta(itemType, v); err != nil {
return err
}
if err = e.buildColumnEncoders(); err != nil {
return err
}
if err = e.encodeHeader(); err != nil {
return err
}
return nil
}
func (e *Encoder) encodeHeader() error {
if e.headerWritten {
return fmt.Errorf("%w: header already encoded", ErrUnexpected)
}
record := make([]string, 0, len(e.colsMeta))
for _, colMeta := range e.colsMeta {
if colMeta.skipColumn {
continue
}
record = append(record, colMeta.headerText)
}
if err := validateHeader(record); err != nil {
return err
}
if e.cfg.NoHeaderMode {
e.headerWritten = true
return nil
} else {
err := e.w.Write(record)
e.headerWritten = err == nil
return err
}
}
func (e *Encoder) encodeRow(rowVal reflect.Value) error {
colsMeta := e.colsMeta
if e.hasDynamicInlineColumns || e.hasFixedInlineColumns {
for _, colMeta := range colsMeta {
if colMeta.inlineColumnMeta != nil {
colMeta.inlineColumnMeta.encodePrepareForNextRow()
}
}
}
record := make([]string, 0, len(colsMeta))
for _, colMeta := range colsMeta {
if colMeta.skipColumn {
continue
}
colVal := colMeta.getColumnValue(rowVal)
if !colVal.IsValid() {
record = append(record, "")
continue
}
text, err := colMeta.encodeFunc(colVal, colMeta.omitEmpty)
if err != nil {
return err
}
for _, fn := range colMeta.postprocessorFuncs {
text = fn(text)
}
record = append(record, text)
}
return e.w.Write(record)
}
func (e *Encoder) parseInputVar(v reflect.Value) (itemType reflect.Type, err error) {
kind := v.Kind()
if kind != reflect.Slice && kind != reflect.Array {
err = fmt.Errorf("%w: %v", ErrTypeInvalid, kind)
return
}
if v.IsNil() {
err = fmt.Errorf("%w: must be non-nil pointer", ErrValueNil)
return
}
typ := v.Type()
itemType = typ.Elem() // E.g. val: []Item, typ: []Item, itemType: Item
if indirectType(itemType).Kind() != reflect.Struct {
err = fmt.Errorf("%w: %v", ErrTypeInvalid, itemType.Kind())
return
}
return
}
func (e *Encoder) validateConfig() error {
if e.cfg.LocalizeHeader && e.cfg.LocalizationFunc == nil {
return fmt.Errorf("%w: localization function required", ErrConfigOptionInvalid)
}
return nil
}
func (e *Encoder) parseColumnsMeta(itemType reflect.Type, val reflect.Value) error {
colsMeta, err := e.parseColumnsMetaFromStructType(itemType, val)
if err != nil {
return err
}
if err = e.validateColumnsMeta(colsMeta); err != nil {
return err
}
e.colsMeta = colsMeta
return nil
}
func (e *Encoder) validateColumnsMeta(colsMeta []*encodeColumnMeta) error {
cfg := e.cfg
// Make sure all column options valid
for colKey := range cfg.columnConfigMap {
if !gofn.ContainBy(colsMeta, func(colMeta *encodeColumnMeta) bool {
return colMeta.headerKey == colKey || colMeta.parentKey == colKey
}) {
return fmt.Errorf("%w: column \"%s\" not found", ErrConfigOptionInvalid, colKey)
}
}
// Make sure all columns are unique
if err := e.validateHeaderUniqueness(colsMeta); err != nil {
return err
}
return nil
}
func (e *Encoder) parseColumnsMetaFromStructType(itemType reflect.Type, val reflect.Value) (
colsMeta []*encodeColumnMeta, err error) {
cfg := e.cfg
// Get first row data from the input
firstRowVal := reflect.Value{}
if val.Len() > 0 {
firstRowVal = val.Index(0)
}
if firstRowVal.IsValid() && firstRowVal.Kind() == reflect.Pointer {
firstRowVal = firstRowVal.Elem()
}
itemType = indirectType(itemType)
numFields := itemType.NumField()
for i := 0; i < numFields; i++ {
field := itemType.Field(i)
tag, err := parseTag(cfg.TagName, field)
if err != nil {
return nil, err
}
if tag == nil || tag.ignored {
continue
}
colMeta := &encodeColumnMeta{
column: len(colsMeta),
headerKey: tag.name,
headerText: tag.name,
prefix: tag.prefix,
omitEmpty: tag.omitEmpty,
targetField: field,
}
if tag.inline {
inlineColsMeta, err := e.parseInlineColumn(field, colMeta, firstRowVal)
if err != nil {
return nil, err
}
colsMeta = append(colsMeta, inlineColsMeta...)
continue
}
colMeta.copyConfig(cfg.columnConfigMap[colMeta.headerKey])
if err = colMeta.localizeHeader(cfg); err != nil {
return nil, err
}
colsMeta = append(colsMeta, colMeta)
}
for i, colMeta := range colsMeta {
colMeta.column = i
}
return colsMeta, err
}
func (e *Encoder) parseInlineColumn(field reflect.StructField, parentCol *encodeColumnMeta, firstRowVal reflect.Value) (
colsMeta []*encodeColumnMeta, err error) {
if firstRowVal.IsValid() {
inlineStruct := firstRowVal.Field(field.Index[0])
inlineColumnsMeta, err := e.parseInlineColumnDynamicType(inlineStruct, parentCol)
if err == nil {
e.hasDynamicInlineColumns = true
return inlineColumnsMeta, nil
}
}
inlineColumnsMeta, err := e.parseInlineColumnFixedType(field.Type, parentCol)
if err == nil && len(inlineColumnsMeta) > 0 {
e.hasFixedInlineColumns = true
return inlineColumnsMeta, nil
}
return nil, fmt.Errorf("%w: %v", ErrHeaderDynamicTypeInvalid, field.Type)
}
func (e *Encoder) parseInlineColumnFixedType(typ reflect.Type, parent *encodeColumnMeta) ([]*encodeColumnMeta, error) {
cfg := e.cfg
typ = indirectType(typ)
if typ.Kind() != reflect.Struct {
return nil, fmt.Errorf("%w: not struct type", ErrHeaderDynamicTypeInvalid)
}
numFields := typ.NumField()
colsMeta := make([]*encodeColumnMeta, 0, numFields)
for i := 0; i < numFields; i++ {
field := typ.Field(i)
tag, err := parseTag(cfg.TagName, field)
if err != nil {
return nil, err
}
if tag == nil || tag.ignored {
continue
}
headerKey := parent.prefix + tag.name
colMeta := &encodeColumnMeta{
column: len(colsMeta),
headerKey: headerKey,
headerText: headerKey,
parentKey: parent.headerKey,
targetField: parent.targetField,
inlineColumnMeta: &inlineColumnMeta{
inlineType: inlineColumnStructFixed,
targetField: field,
dataType: field.Type,
},
}
columnCfg := cfg.columnConfigMap[headerKey]
if columnCfg == nil {
columnCfg = cfg.columnConfigMap[colMeta.parentKey]
}
colMeta.copyConfig(columnCfg)
if err = colMeta.localizeHeader(cfg); err != nil {
return nil, err
}
colsMeta = append(colsMeta, colMeta)
}
return colsMeta, nil
}
func (e *Encoder) parseInlineColumnDynamicType(inlineStruct reflect.Value, parent *encodeColumnMeta) (
[]*encodeColumnMeta, error) {
cfg := e.cfg
inlineStruct = indirectValue(inlineStruct)
if !inlineStruct.IsValid() {
return []*encodeColumnMeta{}, nil
}
if inlineStruct.Kind() != reflect.Struct {
return nil, fmt.Errorf("%w: not struct type", ErrHeaderDynamicTypeInvalid)
}
headerField := inlineStruct.FieldByName(dynamicInlineColumnHeader)
if !headerField.IsValid() {
return nil, fmt.Errorf("%w: field Header not found", ErrHeaderDynamicTypeInvalid)
}
if headerField.Type() != reflect.TypeOf([]string{}) {
return nil, fmt.Errorf("%w: field Header not []string", ErrHeaderDynamicTypeInvalid)
}
valuesField, ok := inlineStruct.Type().FieldByName(dynamicInlineColumnValues)
if !ok {
return nil, fmt.Errorf("%w: field Values not found", ErrHeaderDynamicTypeInvalid)
}
if valuesField.Type.Kind() != reflect.Slice {
return nil, fmt.Errorf("%w: field Values not slice", ErrHeaderDynamicTypeInvalid)
}
dataType := valuesField.Type.Elem()
header, _ := headerField.Interface().([]string)
colsMeta := make([]*encodeColumnMeta, 0, len(header))
inlineColumnMeta := &inlineColumnMeta{
headerText: header,
inlineType: inlineColumnStructDynamic,
targetField: valuesField,
dataType: dataType,
}
for _, h := range header {
headerKey := parent.prefix + h
colMeta := *parent
colMeta.headerKey = headerKey
colMeta.headerText = headerKey
colMeta.parentKey = parent.headerKey
colMeta.inlineColumnMeta = inlineColumnMeta
columnCfg := cfg.columnConfigMap[colMeta.headerKey]
if columnCfg == nil {
columnCfg = cfg.columnConfigMap[colMeta.parentKey]
}
colMeta.copyConfig(columnCfg)
// Try to localize header (ignore the error when fail)
_ = colMeta.localizeHeader(cfg)
colsMeta = append(colsMeta, &colMeta)
}
return colsMeta, nil
}
func (e *Encoder) buildColumnEncoders() error {
for _, colMeta := range e.colsMeta {
if colMeta.encodeFunc != nil {
continue
}
dataType := colMeta.targetField.Type
if colMeta.inlineColumnMeta != nil {
dataType = colMeta.inlineColumnMeta.dataType
}
encodeFunc, err := getEncodeFunc(dataType)
if err != nil {
return err
}
colMeta.encodeFunc = encodeFunc
}
return nil
}
// validateHeaderUniqueness validate to make sure header columns are unique
func (e *Encoder) validateHeaderUniqueness(colsMeta []*encodeColumnMeta) error {
mapCheckUniq := make(map[string]struct{}, len(colsMeta))
for _, colMeta := range colsMeta {
h := colMeta.headerKey
hh := strings.TrimSpace(h)
if h != hh || len(hh) == 0 {
return fmt.Errorf("%w: \"%s\" invalid", ErrHeaderColumnInvalid, h)
}
isDynamicInline := colMeta.inlineColumnMeta != nil &&
colMeta.inlineColumnMeta.inlineType == inlineColumnStructDynamic
if _, ok := mapCheckUniq[hh]; ok && !isDynamicInline {
return fmt.Errorf("%w: \"%s\" duplicated", ErrHeaderColumnDuplicated, h)
}
mapCheckUniq[hh] = struct{}{}
}
return nil
}
type encodeColumnMeta struct {
column int
headerKey string
headerText string
parentKey string
prefix string
omitEmpty bool
skipColumn bool
targetField reflect.StructField
inlineColumnMeta *inlineColumnMeta
encodeFunc EncodeFunc
postprocessorFuncs []ProcessorFunc
}
func (m *encodeColumnMeta) localizeHeader(cfg *EncodeConfig) error {
if cfg.LocalizeHeader {
headerText, err := cfg.LocalizationFunc(m.headerKey, nil)
if err != nil {
return multierror.Append(ErrLocalization, err)
}
m.headerText = headerText
}
return nil
}
func (m *encodeColumnMeta) copyConfig(columnCfg *EncodeColumnConfig) {
if columnCfg == nil {
return
}
m.skipColumn = columnCfg.Skip
m.encodeFunc = columnCfg.EncodeFunc
m.postprocessorFuncs = columnCfg.PostprocessorFuncs
}
func (m *encodeColumnMeta) getColumnValue(rowVal reflect.Value) reflect.Value {
colVal := rowVal.Field(m.targetField.Index[0])
if m.inlineColumnMeta != nil {
colVal = m.inlineColumnMeta.encodeGetColumnValue(colVal)
}
return colVal
}