Skip to content

Commit c91099d

Browse files
committed
Merge remote-tracking branch 'origin/v2-fluent-functional-options'
2 parents a0fde37 + 7584ab7 commit c91099d

File tree

7 files changed

+57
-37
lines changed

7 files changed

+57
-37
lines changed

csv/column_scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ColumnScanner struct {
2525
// to be the header. It calls Scan once to read the header; subsequent
2626
// calls to Scan will return the remaining records.
2727
func NewColumnScanner(reader io.Reader, options ...Option) (*ColumnScanner, error) {
28-
inner := NewScanner(reader, append(options, FieldsPerRecord(0))...)
28+
inner := NewScanner(reader, append(options, Options.FieldsPerRecord(0))...)
2929
if !inner.Scan() {
3030
return nil, inner.Error()
3131
}

csv/example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"strings"
77

8-
"github.com/smarty/scanners/csv"
8+
"github.com/smarty/scanners/v2/csv"
99
)
1010

1111
func ExampleScanner() {
@@ -43,7 +43,7 @@ func ExampleScanner_options() {
4343
`"Robert";"Griesemer";"gri"`,
4444
}, "\n")
4545

46-
scanner := csv.NewScanner(strings.NewReader(in), csv.Comma(';'), csv.Comment('#'))
46+
scanner := csv.NewScanner(strings.NewReader(in), csv.Options.Comma(';'), csv.Options.Comment('#'))
4747

4848
for scanner.Scan() {
4949
fmt.Println(scanner.Record())

csv/options.go

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ package csv
44
// Each one allows configuration of the scanner and/or its internal *csv.Reader.
55
type Option func(*Scanner)
66

7+
// Options (a singleton instance) provides access to built-in functional options.
8+
var Options options
9+
10+
type options struct{}
11+
712
// ContinueOnError controls scanner behavior in error scenarios.
813
// If true is passed, continue scanning until io.EOF is reached.
914
// If false is passed (default), any error encountered during scanning
@@ -14,32 +19,47 @@ type Option func(*Scanner)
1419
// See the error variables starting at [csv.ErrBareQuote], and the
1520
// [csv.ParseError] type, for more information regarding possible
1621
// error values.
17-
func ContinueOnError(continue_ bool) Option {
18-
return func(s *Scanner) { s.continueOnError = continue_ }
22+
func (options) ContinueOnError(continue_ bool) Option {
23+
return func(s *Scanner) {
24+
s.continueOnError = continue_
25+
}
1926
}
20-
21-
// See the [csv.Reader.Comma] field.
22-
func Comma(comma rune) Option { return func(s *Scanner) { s.reader.Comma = comma } }
23-
24-
// See the [csv.Reader.Comment] field.
25-
func Comment(comment rune) Option { return func(s *Scanner) { s.reader.Comment = comment } }
26-
27-
// See the [csv.Reader.FieldsPerRecord] field.
28-
func FieldsPerRecord(fields int) Option {
29-
return func(s *Scanner) { s.reader.FieldsPerRecord = fields }
27+
func (options) Comma(comma rune) Option {
28+
return func(s *Scanner) {
29+
s.reader.Comma = comma
30+
}
3031
}
31-
32-
// See the [csv.Reader.LazyQuotes] field.
33-
func LazyQuotes(lazy bool) Option { return func(s *Scanner) { s.reader.LazyQuotes = lazy } }
34-
35-
// See the [csv.Reader.ReuseRecord] field.
36-
func ReuseRecord(reuse bool) Option { return func(s *Scanner) { s.reader.ReuseRecord = reuse } }
37-
38-
// See the [csv.Reader.TrimLeadingSpace] field.
39-
func TrimLeadingSpace(trim bool) Option { return func(s *Scanner) { s.reader.TrimLeadingSpace = trim } }
40-
41-
func SkipHeaderRecord() Option { return SkipRecords(1) }
42-
func SkipRecords(count int) Option {
32+
func (options) Comment(comment rune) Option {
33+
return func(s *Scanner) {
34+
s.reader.Comment = comment
35+
}
36+
}
37+
func (options) FieldsPerRecord(fields int) Option {
38+
return func(s *Scanner) {
39+
s.reader.FieldsPerRecord = fields
40+
}
41+
}
42+
func (options) LazyQuotes(lazy bool) Option {
43+
return func(s *Scanner) {
44+
s.reader.LazyQuotes = lazy
45+
}
46+
}
47+
func (options) ReuseRecord(reuse bool) Option {
48+
return func(s *Scanner) {
49+
s.reader.ReuseRecord = reuse
50+
}
51+
}
52+
func (options) TrimLeadingSpace(trim bool) Option {
53+
return func(s *Scanner) {
54+
s.reader.TrimLeadingSpace = trim
55+
}
56+
}
57+
func (options) SkipHeaderRecord() Option {
58+
return func(s *Scanner) {
59+
s.Scan()
60+
}
61+
}
62+
func (options) SkipRecords(count int) Option {
4363
return func(s *Scanner) {
4464
for x := 0; x < count; x++ {
4565
s.Scan()

csv/scanner_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,24 @@ func (this *ScanAllFixture) scanAll(inputs []string, options ...Option) (scanned
3838
}
3939

4040
func (this *ScanAllFixture) TestCanonical() {
41-
scanned := this.scanAll(csvCanon, Comma(','), FieldsPerRecord(3))
41+
scanned := this.scanAll(csvCanon, Options.Comma(','), Options.FieldsPerRecord(3))
4242
this.So(scanned, should.Resemble, expectedScannedOutput)
4343
}
4444

4545
func (this *ScanAllFixture) TestCanonicalWithOptions() {
46-
scanned := this.scanAll(csvCanonRequiringConfigOptions, Comma(';'), Comment('#'))
46+
scanned := this.scanAll(csvCanonRequiringConfigOptions, Options.Comma(';'), Options.Comment('#'))
4747
this.So(scanned, should.Resemble, expectedScannedOutput)
4848
}
4949

5050
func (this *ScanAllFixture) TestOptions() {
51-
scanner := NewScanner(nil, ReuseRecord(true), TrimLeadingSpace(true), LazyQuotes(true))
51+
scanner := NewScanner(nil, Options.ReuseRecord(true), Options.TrimLeadingSpace(true), Options.LazyQuotes(true))
5252
this.So(scanner.reader.ReuseRecord, should.BeTrue)
5353
this.So(scanner.reader.LazyQuotes, should.BeTrue)
5454
this.So(scanner.reader.TrimLeadingSpace, should.BeTrue)
5555
}
5656

5757
func (this *ScanAllFixture) TestInconsistentFieldCounts_ContinueOnError() {
58-
scanned := this.scanAll(csvCanonInconsistentFieldCounts, ContinueOnError(true))
58+
scanned := this.scanAll(csvCanonInconsistentFieldCounts, Options.ContinueOnError(true))
5959
this.So(scanned, should.Resemble, []Record{
6060
{line: 1, record: []string{"1", "2", "3"}, err: nil},
6161
{line: 2, record: []string{"1", "2", "3", "4"}, err: &csv.ParseError{StartLine: 2, Line: 2, Column: 1, Err: csv.ErrFieldCount}},
@@ -72,7 +72,7 @@ func (this *ScanAllFixture) TestInconsistentFieldCounts_HaltOnError() {
7272
}
7373

7474
func (this *ScanAllFixture) TestCallsToScanAfterEOFReturnFalse() {
75-
scanner := NewScanner(strings.NewReader("1,2,3"), Comma(','))
75+
scanner := NewScanner(strings.NewReader("1,2,3"), Options.Comma(','))
7676

7777
this.So(scanner.Scan(), should.BeTrue)
7878
this.So(scanner.Record(), should.Resemble, []string{"1", "2", "3"})
@@ -86,7 +86,7 @@ func (this *ScanAllFixture) TestCallsToScanAfterEOFReturnFalse() {
8686
}
8787

8888
func (this *ScanAllFixture) TestSkipHeader() {
89-
scanned := this.scanAll(csvCanon, Comma(','), SkipHeaderRecord())
89+
scanned := this.scanAll(csvCanon, Options.Comma(','), Options.SkipHeaderRecord())
9090
this.So(scanned, should.Resemble, []Record{
9191
{line: 1, record: []string{"Rob", "Pike", "rob"}},
9292
{line: 2, record: []string{"Ken", "Thompson", "ken"}},
@@ -95,7 +95,7 @@ func (this *ScanAllFixture) TestSkipHeader() {
9595
}
9696

9797
func (this *ScanAllFixture) TestRecords() {
98-
scanned := this.scanAll(csvCanon, Comma(','), SkipRecords(3))
98+
scanned := this.scanAll(csvCanon, Options.Comma(','), Options.SkipRecords(3))
9999
this.So(scanned, should.Resemble, []Record{
100100
{line: 1, record: []string{"Robert", "Griesemer", "gri"}},
101101
})

fields/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"strings"
77

8-
"github.com/smarty/scanners/fields"
8+
"github.com/smarty/scanners/v2/fields"
99
)
1010

1111
// Justification of fields should not affect the scanned values.

fixedwidth/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"strings"
77

8-
fw "github.com/smarty/scanners/fixedwidth"
8+
fw "github.com/smarty/scanners/v2/fixedwidth"
99
)
1010

1111
func ExampleScanner() {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/smarty/scanners
1+
module github.com/smarty/scanners/v2
22

33
go 1.17
44

0 commit comments

Comments
 (0)