You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ko...@apache.org on 2018/11/02 00:28:19 UTC

[arrow] branch master updated: ARROW-3683: [Go] add functional-option style to configure the CSV reader

This is an automated email from the ASF dual-hosted git repository.

kou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new f0fa18f  ARROW-3683: [Go] add functional-option style to configure the CSV reader
f0fa18f is described below

commit f0fa18f0ffb778ba6b49fd100f08c936c8ec08b5
Author: Sebastien Binet <bi...@cern.ch>
AuthorDate: Fri Nov 2 09:28:03 2018 +0900

    ARROW-3683: [Go] add functional-option style to configure the CSV reader
    
    Author: Sebastien Binet <bi...@cern.ch>
    
    Closes #2891 from sbinet/issue-3683 and squashes the following commits:
    
    795b680e <Sebastien Binet> ARROW-3683:  add functional-option style to configure the CSV reader
---
 go/arrow/csv/csv.go      | 45 +++++++++++++++++++++++++++++++++++++++------
 go/arrow/csv/csv_test.go | 18 +++++++++---------
 2 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/go/arrow/csv/csv.go b/go/arrow/csv/csv.go
index 0e6d6e3..a572499 100644
--- a/go/arrow/csv/csv.go
+++ b/go/arrow/csv/csv.go
@@ -37,6 +37,30 @@ var (
 	ErrMismatchFields = errors.New("arrow/csv: number of records mismatch")
 )
 
+// Option configures a CSV reader.
+type Option func(*Reader)
+
+// WithComment specifies the comment character used while parsing CSV files.
+func WithComment(c rune) Option {
+	return func(r *Reader) {
+		r.R.Comment = c
+	}
+}
+
+// WithComma specifies the fields separation character used while parsing CSV files.
+func WithComma(c rune) Option {
+	return func(r *Reader) {
+		r.R.Comma = c
+	}
+}
+
+// WithAllocator specifies the Arrow memory allocator used while building records.
+func WithAllocator(mem memory.Allocator) Option {
+	return func(r *Reader) {
+		r.mem = mem
+	}
+}
+
 // Reader wraps encoding/csv.Reader and creates array.Records from a schema.
 type Reader struct {
 	R      *csv.Reader
@@ -46,6 +70,8 @@ type Reader struct {
 	bld  *array.RecordBuilder
 	cur  array.Record
 	err  error
+
+	mem memory.Allocator
 }
 
 // NewReader returns a reader that reads from the CSV file and creates
@@ -53,14 +79,21 @@ type Reader struct {
 //
 // NewReader panics if the given schema contains fields that have types that are not
 // primitive types.
-func NewReader(r io.Reader, mem memory.Allocator, schema *arrow.Schema) *Reader {
+func NewReader(r io.Reader, schema *arrow.Schema, opts ...Option) *Reader {
 	validate(schema)
-	return &Reader{
-		R:      csv.NewReader(r),
-		schema: schema,
-		refs:   1,
-		bld:    array.NewRecordBuilder(mem, schema),
+
+	rr := &Reader{R: csv.NewReader(r), schema: schema, refs: 1}
+	for _, opt := range opts {
+		opt(rr)
 	}
+
+	if rr.mem == nil {
+		rr.mem = memory.DefaultAllocator
+	}
+
+	rr.bld = array.NewRecordBuilder(rr.mem, rr.schema)
+
+	return rr
 }
 
 // Err returns the last error encountered during the iteration over the
diff --git a/go/arrow/csv/csv_test.go b/go/arrow/csv/csv_test.go
index 87b803b..534e8ea 100644
--- a/go/arrow/csv/csv_test.go
+++ b/go/arrow/csv/csv_test.go
@@ -44,9 +44,7 @@ func Example() {
 		},
 		nil,
 	)
-	r := csv.NewReader(f, memory.NewGoAllocator(), schema)
-	r.R.Comment = '#'
-	r.R.Comma = ';'
+	r := csv.NewReader(f, schema, csv.WithComment('#'), csv.WithComma(';'))
 	defer r.Release()
 
 	n := 0
@@ -117,9 +115,10 @@ func TestCSVReader(t *testing.T) {
 		},
 		nil,
 	)
-	r := csv.NewReader(bytes.NewReader(raw), mem, schema)
-	r.R.Comment = '#'
-	r.R.Comma = ';'
+	r := csv.NewReader(bytes.NewReader(raw), schema,
+		csv.WithAllocator(mem),
+		csv.WithComment('#'), csv.WithComma(';'),
+	)
 	defer r.Release()
 
 	r.Retain()
@@ -180,9 +179,10 @@ rec[11]["str"]: ["str-2"]
 
 	// test error modes
 	{
-		r := csv.NewReader(bytes.NewReader(raw), mem, schema)
-		r.R.Comment = '#'
-		r.R.Comma = ';'
+		r := csv.NewReader(bytes.NewReader(raw), schema,
+			csv.WithAllocator(mem),
+			csv.WithComment('#'), csv.WithComma(';'),
+		)
 
 		r.Next()
 		r.Record()