@@ -12,24 +12,26 @@ import (
12
12
"github.com/tiendc/gofn"
13
13
)
14
14
15
+ // DecodeConfig configuration for decoding CSV data as structs
15
16
type DecodeConfig struct {
16
- // TagName tag name to parse the struct (default is " csv" )
17
+ // TagName tag name to parse the struct (default is ` csv` )
17
18
TagName string
18
19
19
- // NoHeaderMode indicates the input data have no header (default is " false" )
20
+ // NoHeaderMode indicates the input data have no header (default is ` false` )
20
21
NoHeaderMode bool
21
22
22
- // StopOnError when error occurs, stop the processing (default is " true" )
23
+ // StopOnError when error occurs, stop the processing (default is ` true` )
23
24
StopOnError bool
24
25
25
- // TrimSpace trim all cell values before processing (default is " false" )
26
+ // TrimSpace trim all cell values before processing (default is ` false` )
26
27
TrimSpace bool
27
28
28
29
// RequireColumnOrder order of columns defined in struct must match the order of columns
29
30
// in the input data (default is "true")
30
31
RequireColumnOrder bool
31
32
32
- // ParseLocalizedHeader header in the input data is localized (default is "false")
33
+ // ParseLocalizedHeader header in the input data is localized (default is `false`)
34
+ //
33
35
// For example:
34
36
// type Student struct {
35
37
// Name string `csv:"name"` -> `name` is header key now, the actual header is localized based on the key
@@ -41,12 +43,14 @@ type DecodeConfig struct {
41
43
// (default is "false")
42
44
AllowUnrecognizedColumns bool
43
45
44
- // TreatIncorrectStructureAsError treat incorrect data structure as error (default is "true")
46
+ // TreatIncorrectStructureAsError treat incorrect data structure as error (default is `true`)
47
+ //
45
48
// For example: header has 5 columns, if there is a row having 6 columns, it will be treated as error
46
49
// and the decoding process will stop even StopOnError flag is false.
47
50
TreatIncorrectStructureAsError bool
48
51
49
- // DetectRowLine detect exact lines of rows (default is "false")
52
+ // DetectRowLine detect exact lines of rows (default is `false`)
53
+ //
50
54
// If turn this flag on, the input reader should be an instance of "encoding/csv" Reader
51
55
// as this lib uses Reader.FieldPos() function to get the line of a row.
52
56
DetectRowLine bool
@@ -79,7 +83,7 @@ func (c *DecodeConfig) ConfigureColumn(name string, fn func(*DecodeColumnConfig)
79
83
fn (columnCfg )
80
84
}
81
85
82
- // DecodeColumnConfig configuration for a specific column
86
+ // DecodeColumnConfig configuration for decoding a specific column
83
87
type DecodeColumnConfig struct {
84
88
// TrimSpace if `true` and DecodeConfig.TrimSpace is `false`, only trim space this column
85
89
// (default is "false")
@@ -98,7 +102,7 @@ type DecodeColumnConfig struct {
98
102
// ValidatorFuncs a list of functions will be called after decoding (optional)
99
103
ValidatorFuncs []ValidatorFunc
100
104
101
- // OnCellErrorFunc function will be called every time an error happens when decode a cell
105
+ // OnCellErrorFunc function will be called every time an error happens when decode a cell.
102
106
// This func can be helpful to set localization key and additional params for the error
103
107
// to localize the error message later on. (optional)
104
108
OnCellErrorFunc OnCellErrorFunc
@@ -108,8 +112,10 @@ func defaultDecodeColumnConfig() *DecodeColumnConfig {
108
112
return & DecodeColumnConfig {}
109
113
}
110
114
115
+ // DecodeOption function to modify decoding config
111
116
type DecodeOption func (cfg * DecodeConfig )
112
117
118
+ // DecodeResult decoding result
113
119
type DecodeResult struct {
114
120
totalRow int
115
121
unrecognizedColumns []string
@@ -128,6 +134,7 @@ func (r *DecodeResult) MissingOptionalColumns() []string {
128
134
return r .missingOptionalColumns
129
135
}
130
136
137
+ // Decoder data structure of the default decoder
131
138
type Decoder struct {
132
139
r Reader
133
140
cfg * DecodeConfig
@@ -142,6 +149,7 @@ type Decoder struct {
142
149
colsMeta []* decodeColumnMeta
143
150
}
144
151
152
+ // NewDecoder creates a new Decoder object
145
153
func NewDecoder (r Reader , options ... DecodeOption ) * Decoder {
146
154
cfg := defaultDecodeConfig ()
147
155
for _ , opt := range options {
@@ -154,8 +162,8 @@ func NewDecoder(r Reader, options ...DecodeOption) *Decoder {
154
162
}
155
163
}
156
164
157
- // Decode decode input data and store the result in the given variable
158
- // The input var must be a pointer to a slice, e.g. `*[]Student` (recommended) or `*[]*Student`
165
+ // Decode decode input data and store the result in the given variable.
166
+ // The input var must be a pointer to a slice, e.g. `*[]Student` (recommended) or `*[]*Student`.
159
167
func (d * Decoder ) Decode (v any ) (* DecodeResult , error ) {
160
168
if d .finished {
161
169
return nil , ErrFinished
@@ -216,8 +224,8 @@ func (d *Decoder) Decode(v any) (*DecodeResult, error) {
216
224
return d .result , nil
217
225
}
218
226
219
- // DecodeOne decode the next one row data
220
- // The input var must be a pointer to a struct (e.g. *Student)
227
+ // DecodeOne decode the next one row data.
228
+ // The input var must be a pointer to a struct (e.g. *Student).
221
229
// This func returns error of the current row processing only, after finishing the last row decoding,
222
230
// call Finish() to get the overall result and error.
223
231
func (d * Decoder ) DecodeOne (v any ) error {
@@ -271,8 +279,8 @@ func (d *Decoder) Finish() (*DecodeResult, error) {
271
279
return d .result , nil
272
280
}
273
281
274
- // prepareDecode prepare for decoding by parsing the struct tags and build column decoders
275
- // This step is performed one time only before the first row decoding
282
+ // prepareDecode prepare for decoding by parsing the struct tags and build column decoders.
283
+ // This step is performed one time only before the first row decoding.
276
284
func (d * Decoder ) prepareDecode (v reflect.Value ) error {
277
285
d .result = & DecodeResult {}
278
286
itemType , err := d .parseOutputVar (v )
@@ -446,7 +454,7 @@ func (d *Decoder) parseOutputVarOne(v reflect.Value) (val reflect.Value, itemTyp
446
454
return
447
455
}
448
456
449
- // readRowData read data of all rows from the input to struct type
457
+ // readRowData read data of all rows from the input to struct type.
450
458
// If you use `encoding/csv` Reader, we can determine the lines of rows (via Reader.FieldPos func).
451
459
// Otherwise, `line` will be set to `-1` which mean undetected.
452
460
func (d * Decoder ) readRowData () error {
@@ -814,7 +822,7 @@ func (d *Decoder) parseDynamicInlineColumns(colsMetaFromStruct []*decodeColumnMe
814
822
return newColsMetaFromStruct , nil
815
823
}
816
824
817
- // buildColumnDecoders build decoders for each column type
825
+ // buildColumnDecoders build decoders for each column type.
818
826
// If the type of column is determined, e.g. `int`, the decode function for that will be determined at
819
827
// the prepare step, and it will be fast at decoding. If it is `interface`, the decode function will parse
820
828
// the actual type at decoding, and it will be slower.
0 commit comments