Skip to content

Commit 7c7c806

Browse files
authored
Add Go docs for functions and types (#34)
1 parent c2bf8d8 commit 7c7c806

9 files changed

+118
-43
lines changed

csvlib.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type LocalizationFunc func(key string, params ParameterMap) (string, error)
5353
// OnCellErrorFunc function to be called when error happens on decoding cell value
5454
type OnCellErrorFunc func(e *CellError)
5555

56+
// ColumnDetail details of a column parsed from a struct tag
5657
type ColumnDetail struct {
5758
Name string
5859
Optional bool

decoder.go

+25-17
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@ import (
1212
"github.com/tiendc/gofn"
1313
)
1414

15+
// DecodeConfig configuration for decoding CSV data as structs
1516
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`)
1718
TagName string
1819

19-
// NoHeaderMode indicates the input data have no header (default is "false")
20+
// NoHeaderMode indicates the input data have no header (default is `false`)
2021
NoHeaderMode bool
2122

22-
// StopOnError when error occurs, stop the processing (default is "true")
23+
// StopOnError when error occurs, stop the processing (default is `true`)
2324
StopOnError bool
2425

25-
// TrimSpace trim all cell values before processing (default is "false")
26+
// TrimSpace trim all cell values before processing (default is `false`)
2627
TrimSpace bool
2728

2829
// RequireColumnOrder order of columns defined in struct must match the order of columns
2930
// in the input data (default is "true")
3031
RequireColumnOrder bool
3132

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+
//
3335
// For example:
3436
// type Student struct {
3537
// 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 {
4143
// (default is "false")
4244
AllowUnrecognizedColumns bool
4345

44-
// TreatIncorrectStructureAsError treat incorrect data structure as error (default is "true")
46+
// TreatIncorrectStructureAsError treat incorrect data structure as error (default is `true`)
47+
//
4548
// For example: header has 5 columns, if there is a row having 6 columns, it will be treated as error
4649
// and the decoding process will stop even StopOnError flag is false.
4750
TreatIncorrectStructureAsError bool
4851

49-
// DetectRowLine detect exact lines of rows (default is "false")
52+
// DetectRowLine detect exact lines of rows (default is `false`)
53+
//
5054
// If turn this flag on, the input reader should be an instance of "encoding/csv" Reader
5155
// as this lib uses Reader.FieldPos() function to get the line of a row.
5256
DetectRowLine bool
@@ -79,7 +83,7 @@ func (c *DecodeConfig) ConfigureColumn(name string, fn func(*DecodeColumnConfig)
7983
fn(columnCfg)
8084
}
8185

82-
// DecodeColumnConfig configuration for a specific column
86+
// DecodeColumnConfig configuration for decoding a specific column
8387
type DecodeColumnConfig struct {
8488
// TrimSpace if `true` and DecodeConfig.TrimSpace is `false`, only trim space this column
8589
// (default is "false")
@@ -98,7 +102,7 @@ type DecodeColumnConfig struct {
98102
// ValidatorFuncs a list of functions will be called after decoding (optional)
99103
ValidatorFuncs []ValidatorFunc
100104

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.
102106
// This func can be helpful to set localization key and additional params for the error
103107
// to localize the error message later on. (optional)
104108
OnCellErrorFunc OnCellErrorFunc
@@ -108,8 +112,10 @@ func defaultDecodeColumnConfig() *DecodeColumnConfig {
108112
return &DecodeColumnConfig{}
109113
}
110114

115+
// DecodeOption function to modify decoding config
111116
type DecodeOption func(cfg *DecodeConfig)
112117

118+
// DecodeResult decoding result
113119
type DecodeResult struct {
114120
totalRow int
115121
unrecognizedColumns []string
@@ -128,6 +134,7 @@ func (r *DecodeResult) MissingOptionalColumns() []string {
128134
return r.missingOptionalColumns
129135
}
130136

137+
// Decoder data structure of the default decoder
131138
type Decoder struct {
132139
r Reader
133140
cfg *DecodeConfig
@@ -142,6 +149,7 @@ type Decoder struct {
142149
colsMeta []*decodeColumnMeta
143150
}
144151

152+
// NewDecoder creates a new Decoder object
145153
func NewDecoder(r Reader, options ...DecodeOption) *Decoder {
146154
cfg := defaultDecodeConfig()
147155
for _, opt := range options {
@@ -154,8 +162,8 @@ func NewDecoder(r Reader, options ...DecodeOption) *Decoder {
154162
}
155163
}
156164

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`.
159167
func (d *Decoder) Decode(v any) (*DecodeResult, error) {
160168
if d.finished {
161169
return nil, ErrFinished
@@ -216,8 +224,8 @@ func (d *Decoder) Decode(v any) (*DecodeResult, error) {
216224
return d.result, nil
217225
}
218226

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).
221229
// This func returns error of the current row processing only, after finishing the last row decoding,
222230
// call Finish() to get the overall result and error.
223231
func (d *Decoder) DecodeOne(v any) error {
@@ -271,8 +279,8 @@ func (d *Decoder) Finish() (*DecodeResult, error) {
271279
return d.result, nil
272280
}
273281

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.
276284
func (d *Decoder) prepareDecode(v reflect.Value) error {
277285
d.result = &DecodeResult{}
278286
itemType, err := d.parseOutputVar(v)
@@ -446,7 +454,7 @@ func (d *Decoder) parseOutputVarOne(v reflect.Value) (val reflect.Value, itemTyp
446454
return
447455
}
448456

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.
450458
// If you use `encoding/csv` Reader, we can determine the lines of rows (via Reader.FieldPos func).
451459
// Otherwise, `line` will be set to `-1` which mean undetected.
452460
func (d *Decoder) readRowData() error {
@@ -814,7 +822,7 @@ func (d *Decoder) parseDynamicInlineColumns(colsMetaFromStruct []*decodeColumnMe
814822
return newColsMetaFromStruct, nil
815823
}
816824

817-
// buildColumnDecoders build decoders for each column type
825+
// buildColumnDecoders build decoders for each column type.
818826
// If the type of column is determined, e.g. `int`, the decode function for that will be determined at
819827
// the prepare step, and it will be fast at decoding. If it is `interface`, the decode function will parse
820828
// the actual type at decoding, and it will be slower.

encoder.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/tiendc/gofn"
1010
)
1111

12+
// EncodeConfig configuration for encoding Go structs as CSV data
1213
type EncodeConfig struct {
1314
// TagName tag name to parse the struct (default is `csv`)
1415
TagName string
@@ -32,6 +33,7 @@ func defaultEncodeConfig() *EncodeConfig {
3233
}
3334
}
3435

36+
// ConfigureColumn configures encoding for a column by name
3537
func (c *EncodeConfig) ConfigureColumn(name string, fn func(*EncodeColumnConfig)) {
3638
if c.columnConfigMap == nil {
3739
c.columnConfigMap = map[string]*EncodeColumnConfig{}
@@ -44,9 +46,10 @@ func (c *EncodeConfig) ConfigureColumn(name string, fn func(*EncodeColumnConfig)
4446
fn(columnCfg)
4547
}
4648

49+
// EncodeColumnConfig configuration for encoding a specific column
4750
type EncodeColumnConfig struct {
4851
// Skip whether skip encoding the column or not (this is equivalent to use `csv:"-"` in struct tag)
49-
// (default is "false")
52+
// (default is `false`)
5053
Skip bool
5154

5255
// EncodeFunc custom encode function (optional)
@@ -60,8 +63,10 @@ func defaultEncodeColumnConfig() *EncodeColumnConfig {
6063
return &EncodeColumnConfig{}
6164
}
6265

66+
// EncodeOption function to modify encoding config
6367
type EncodeOption func(cfg *EncodeConfig)
6468

69+
// Encoder data structure of the default encoder
6570
type Encoder struct {
6671
w Writer
6772
cfg *EncodeConfig
@@ -74,6 +79,7 @@ type Encoder struct {
7479
colsMeta []*encodeColumnMeta
7580
}
7681

82+
// NewEncoder creates a new Encoder object
7783
func NewEncoder(w Writer, options ...EncodeOption) *Encoder {
7884
cfg := defaultEncodeConfig()
7985
for _, opt := range options {
@@ -85,8 +91,8 @@ func NewEncoder(w Writer, options ...EncodeOption) *Encoder {
8591
}
8692
}
8793

88-
// Encode encode input data stored in the given variable
89-
// The input var must be a slice, e.g. `[]Student` or `[]*Student`
94+
// Encode encode input data stored in the given variable.
95+
// The input var must be a slice, e.g. `[]Student` or `[]*Student`.
9096
func (e *Encoder) Encode(v any) error {
9197
if e.finished {
9298
return ErrFinished
@@ -162,6 +168,7 @@ func (e *Encoder) EncodeOne(v any) error {
162168
return nil
163169
}
164170

171+
// Finish encoding, after calling this func, you can't encode more
165172
func (e *Encoder) Finish() error {
166173
e.finished = true
167174
return e.err

0 commit comments

Comments
 (0)