Skip to content

Commit 1cda8f9

Browse files
author
Stefan Tudose
committed
align terminology with the one used in encoding/csv
1 parent c71c0e1 commit 1cda8f9

File tree

12 files changed

+33
-33
lines changed

12 files changed

+33
-33
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Csvdecoder supports converting columns read from the source file into the follow
2121
- `*uint`, `*uint8`, `*uint16`, `*uint32`, `*uint64`
2222
- `*bool`
2323
- `*float32`, `*float64`
24-
- a slice of values. Note that the CSV record must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
25-
- an array of values. Note that the CSV record must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
24+
- a slice of values. Note that the CSV field must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
25+
- an array of values. Note that the CSV field must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
2626
- a pointer to any type implementing the `csvdecoder.Interface` interface
2727

2828
## Usage
@@ -91,7 +91,7 @@ See also the example files for more usage examples.
9191
The behavior of the decoder can be configured by passing one of following options when creating the decoder:
9292
- Comma: the character that separates values. Default value is comma.
9393
- IgnoreHeaders: if set to true, the first line will be ignored. This is useful when the CSV file contains a header line.
94-
- IgnoreUnmatchingFields: if set to true, the number of records and scan targets are allowed to be different. By default, if they don't match exactly it will cause an error.
94+
- IgnoreUnmatchingFields: if set to true, the number of fields and scan targets are allowed to be different. By default, if they don't match exactly it will cause an error.
9595

9696
```golang
9797
decoder, err := csvdecoder.NewWithConfig(file, csvdecoder.Config{Comma: ';', IgnoreHeaders: true})

convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func convertAssignValue(dest interface{}, src string) error {
2727

2828
// check if the destination implements the Decoder interface
2929
if decoder, ok := dest.(Interface); ok {
30-
return decoder.DecodeRecord(src)
30+
return decoder.DecodeField(src)
3131
}
3232

3333
var sv reflect.Value

decoder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Decoder struct {
1818
type Config struct {
1919
Comma rune // the character that separates values. Default value is comma.
2020
IgnoreHeaders bool // if set to true, the first line will be ignored
21-
IgnoreUnmatchingFields bool // if set to true, the number of records and scan targets are allowed to be different
21+
IgnoreUnmatchingFields bool // if set to true, the number of fields and scan targets are allowed to be different
2222
}
2323

2424
// New returns a new CSV decoder that reads from r.
@@ -57,7 +57,7 @@ func newDecoder(reader io.Reader, config Config) (*Decoder, error) {
5757
// at by dest.
5858
// With the default behavior, it will throw an error if the number of values in dest
5959
// is different from the number of values. If the `IgnoreUnmatchingFields` flag is
60-
// set, it will ignore the records and the arguments that have no match.
60+
// set, it will ignore the fields and the arguments that have no match.
6161
//
6262
// Scan converts columns read from the source into the following
6363
// types:
@@ -80,15 +80,15 @@ func (p *Decoder) Scan(dest ...interface{}) error {
8080
case p.currentRowValues == nil:
8181
return ErrNextNotCalled
8282
case !p.config.IgnoreUnmatchingFields && len(p.currentRowValues) != len(dest):
83-
return fmt.Errorf("%w: got %d scan targets and %d records",
83+
return fmt.Errorf("%w: got %d scan targets and %d fields",
8484
ErrScanTargetsNotMatch,
8585
len(dest),
8686
len(p.currentRowValues),
8787
)
8888
}
8989
for i, val := range p.currentRowValues {
9090
if i >= len(dest) {
91-
// ignore the remaining records as they have no scan target
91+
// ignore the remaining fields as they have no scan target
9292
break
9393
}
9494
err := convertAssignValue(dest[i], val)

decoder_interface_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type MyDecoder struct {
99
value string
1010
}
1111

12-
func (d *MyDecoder) DecodeRecord(value string) error {
12+
func (d *MyDecoder) DecodeField(value string) error {
1313
d.value = value + "!"
1414
return nil
1515
}
@@ -33,8 +33,8 @@ func TestDecoderStruct(t *testing.T) {
3333
{
3434
name: "should work for a struct using the Decoder interface",
3535
dest: MyDecoder{},
36-
data: "record1\n",
37-
expected: "record1!",
36+
data: "field1\n",
37+
expected: "field1!",
3838
},
3939
} {
4040
tc := tc
@@ -69,8 +69,8 @@ func TestDecoderPointer(t *testing.T) {
6969
{
7070
name: "should work for a struct holding a pointer to a decoder",
7171
dest: &MyDecoder{},
72-
data: "record1\n",
73-
expected: "record1!",
72+
data: "field1\n",
73+
expected: "field1!",
7474
},
7575
} {
7676
tc := tc
@@ -106,8 +106,8 @@ func TestDecoderDoublePointer(t *testing.T) {
106106
{
107107
name: "should work for a struct holding a double pointer to a decoder",
108108
dest: &myDec,
109-
data: "record1\n",
110-
expected: "record1!",
109+
data: "field1\n",
110+
expected: "field1!",
111111
},
112112
} {
113113
tc := tc
@@ -142,8 +142,8 @@ func TestDecoderInterface(t *testing.T) {
142142
{
143143
name: "should work for a struct using the Decoder interface",
144144
dest: &MyDecoder{},
145-
data: "record1\n",
146-
expected: "record1!",
145+
data: "field1\n",
146+
expected: "field1!",
147147
},
148148
} {
149149
tc := tc

decoder_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestIgnoreUnmatchingFields(t *testing.T) {
5353
expectedError: nil,
5454
},
5555
{
56-
name: "should work with more records when the flag is true",
56+
name: "should work with more fields when the flag is true",
5757
config: Config{
5858
IgnoreUnmatchingFields: true,
5959
},
@@ -71,7 +71,7 @@ func TestIgnoreUnmatchingFields(t *testing.T) {
7171
expectedError: ErrScanTargetsNotMatch,
7272
},
7373
{
74-
name: "should fail with more records when the flag is false",
74+
name: "should fail with more fields when the flag is false",
7575
config: Config{
7676
IgnoreUnmatchingFields: false,
7777
},
@@ -87,7 +87,7 @@ func TestIgnoreUnmatchingFields(t *testing.T) {
8787
expectedError: ErrScanTargetsNotMatch,
8888
},
8989
{
90-
name: "should fail with more records with the default config",
90+
name: "should fail with more fields with the default config",
9191
config: Config{},
9292
data: "rec,2\n",
9393
scanTargets: []interface{}{&strVal},

doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// - `*uint`, `*uint8`, `*uint16`, `*uint32`, `*uint64`
1313
// - `*bool`
1414
// - `*float32`, `*float64`
15-
// - a slice of values. Note that the CSV record must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
16-
// - an array of values. Note that the CSV record must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
15+
// - a slice of values. Note that the CSV field must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
16+
// - an array of values. Note that the CSV field must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
1717
// - a pointer to any type implementing the `csvdecoder.Interface` interface
1818

1919
// csvdecoder uses the same terminology as package encoding/csv:
@@ -26,7 +26,7 @@
2626
// The behavior of the decoder can be configured by passing one of following options when creating the decoder:
2727
// - Comma: the character that separates values. The default value is comma.
2828
// - IgnoreHeaders: if set to true, the first line will be ignored. This is useful when the CSV file contains a header line.
29-
// - IgnoreUnmatchingFields: if set to true, the number of records and scan targets are allowed to be different. By default, if they don't match exactly it will cause an error.
29+
// - IgnoreUnmatchingFields: if set to true, the number of fields and scan targets are allowed to be different. By default, if they don't match exactly it will cause an error.
3030

3131
// See README.md for more info.
3232
package csvdecoder

errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
var (
88
ErrEOF = errors.New("end of file reached") // ErrEOF is thrown if the EOF is reached by the Next method.
9-
ErrScanTargetsNotMatch = errors.New("the number of scan targets does not match the number of csv records")
9+
ErrScanTargetsNotMatch = errors.New("the number of scan targets does not match the number of csv fields")
1010
ErrReadingOccurred = errors.New("can't continue after a reading error")
1111
ErrNextNotCalled = errors.New("scan called without calling Next")
1212

example_custom_decoder_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ type Point struct {
1313
Y int
1414
}
1515

16-
// DecodeRecord implements the csvdecoder.Interface type
17-
func (p *Point) DecodeRecord(record string) error {
16+
// DecodeField implements the csvdecoder.Interface type
17+
func (p *Point) DecodeField(field string) error {
1818
// the decode code is specific to the way the object is serialized.
1919
// in this example the point is encoded as a JSON array with two values
2020

2121
data := make([]int, 2)
22-
if err := json.NewDecoder(strings.NewReader(record)).Decode(&data); err != nil {
23-
return fmt.Errorf("could not parse %s as JSON array: %w", record, err)
22+
if err := json.NewDecoder(strings.NewReader(field)).Decode(&data); err != nil {
23+
return fmt.Errorf("could not parse %s as JSON array: %w", field, err)
2424
}
2525

2626
(*p).X = data[0]
File renamed without changes.

example_simple_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Example_simple() {
1919
//john,44,true
2020
//lucy,48,false
2121
//mr hyde,34,true
22-
file, err := os.Open("./data/simple.csv")
22+
file, err := os.Open("./example_data/simple.csv")
2323
if err != nil {
2424
// handle error
2525
return

0 commit comments

Comments
 (0)