Skip to content

Commit 6908522

Browse files
Merge pull request #6 from goark/refactoring-and-debug
Add TrimSpace option
2 parents 4a089a6 + dd11cd0 commit 6908522

File tree

5 files changed

+116
-17
lines changed

5 files changed

+116
-17
lines changed

calcdata/calcdata.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func New(doc *ods.Doc, sheetName string) (*Reader, error) {
3535
return &Reader{table: &doc.Table[index]}, nil
3636
}
3737

38+
// TrimSpace returns false.
39+
func (r *Reader) TrimSpace() bool {
40+
return false
41+
}
42+
3843
// LazyQuotes returns true.
3944
func (r *Reader) LazyQuotes() bool {
4045
return true

csvdata.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010

1111
// Reader is class of CSV reader
1212
type Reader struct {
13-
reader *csv.Reader
14-
closer func() error
13+
trimSpace bool
14+
reader *csv.Reader
15+
closer func() error
1516
}
1617

1718
var _ RowsReader = (*Reader)(nil) //Reader is compatible with RowsReader interface
@@ -38,6 +39,11 @@ func New(r io.Reader) *Reader {
3839
return &Reader{reader: cr, closer: closer}
3940
}
4041

42+
// TrimSpace returns TrimSpace option.
43+
func (r *Reader) TrimSpace() bool {
44+
return r.trimSpace
45+
}
46+
4147
// LazyQuotes returns LazyQuotes option.
4248
func (r *Reader) LazyQuotes() bool {
4349
return r.reader.LazyQuotes
@@ -52,6 +58,16 @@ func (r *Reader) WithComma(c rune) *Reader {
5258
return r
5359
}
5460

61+
// WithTrimSpace method sets trimSpace and TrimLeadingSpace property.
62+
func (r *Reader) WithTrimSpace(mode bool) *Reader {
63+
if r == nil {
64+
return nil
65+
}
66+
r.trimSpace = mode
67+
r.reader.TrimLeadingSpace = mode
68+
return r
69+
}
70+
5571
// WithLazyQuotes method sets LazyQuotes property.
5672
func (r *Reader) WithLazyQuotes(mode bool) *Reader {
5773
if r == nil {

csvdata_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ import (
1414

1515
const (
1616
csv1 = `"order", name ,"mass","distance","habitable","note"
17-
1, Mercury, 0.055, 0.4,false,""
18-
2, Venus, 0.815, 0.7,false,""
19-
3, Earth, 1.0, 1.0,true,""
20-
4, Mars, 0.107, 1.5,false,""
17+
1, Mercury, 0.055, 0.4,false,"2006-01-02T15:04:05+09:00"
18+
2, Venus, 0.815, 0.7,false,"2006-01-02T15:04:05+09:00"
19+
3, Earth, 1.0, 1.0,true,"2006-01-02T15:04:05+09:00"
20+
4, Mars, 0.107, 1.5,false,"2006-01-02T15:04:05+09:00"
2121
`
22-
tsv1 = `1 Mercury 0.055 0.4 false ""
23-
2 Venus 0.815 0.7 false ""
24-
3 Earth 1.0 1.0 true ""
25-
4 Mars 0.107 1.5 false ""
22+
tsv1 = `1 Mercury 0.055 0.4 false "2006-01-02T15:04:05+09:00"
23+
2 Venus 0.815 0.7 false "2006-01-02T15:04:05+09:00"
24+
3 Earth 1.0 1.0 true "2006-01-02T15:04:05+09:00"
25+
4 Mars 0.107 1.5 false "2006-01-02T15:04:05+09:00"
2626
`
2727
)
2828

2929
func TestWithNil(t *testing.T) {
30-
r := csvdata.NewRows((*csvdata.Reader)(nil).WithComma(',').WithLazyQuotes(true).WithTrimLeadingSpace(true).WithFieldsPerRecord(1), true)
30+
r := csvdata.NewRows((*csvdata.Reader)(nil).WithComma(',').WithTrimSpace(true).WithLazyQuotes(true).WithTrimLeadingSpace(true).WithFieldsPerRecord(1), true)
3131
defer r.Close() //dummy
3232
if err := r.Next(); !errors.Is(err, csvdata.ErrNullPointer) {
3333
t.Errorf("Next() is \"%+v\", want \"%+v\".", err, csvdata.ErrNullPointer)
@@ -65,7 +65,7 @@ func TestWithNil(t *testing.T) {
6565

6666
func TestErrReader(t *testing.T) {
6767
errtest := errors.New("test")
68-
r := csvdata.NewRows(csvdata.New(iotest.ErrReader(errtest)).WithComma(',').WithLazyQuotes(true).WithTrimLeadingSpace(true).WithFieldsPerRecord(1), true)
68+
r := csvdata.NewRows(csvdata.New(iotest.ErrReader(errtest)).WithComma(',').WithTrimSpace(true).WithLazyQuotes(true).WithTrimLeadingSpace(true).WithFieldsPerRecord(1), true)
6969
defer r.Close() //dummy
7070
if err := r.Next(); !errors.Is(err, errtest) {
7171
t.Errorf("Next() is \"%+v\", want \"%+v\".", err, errtest)
@@ -107,7 +107,7 @@ func TestNormal(t *testing.T) {
107107
}
108108

109109
for _, tc := range testCases {
110-
rc := csvdata.NewRows(csvdata.New(tc.inp).WithComma(tc.sep).WithLazyQuotes(true).WithTrimLeadingSpace(true).WithFieldsPerRecord(tc.size), tc.headerFlag)
110+
rc := csvdata.NewRows(csvdata.New(tc.inp).WithComma(tc.sep).WithLazyQuotes(true).WithTrimSpace(true).WithTrimLeadingSpace(true).WithFieldsPerRecord(tc.size), tc.headerFlag)
111111
if err := rc.Next(); err != nil {
112112
t.Errorf("Next() is \"%+v\", want nil.", err)
113113
} else {
@@ -140,7 +140,7 @@ func TestNormal(t *testing.T) {
140140
t.Errorf("Column() is \"%v\", want \"%v\".", name, tc.name2)
141141
}
142142
//bool
143-
if _, err = rc.GetBool(5); !errors.Is(err, csvdata.ErrNullValue) {
143+
if _, err = rc.GetBool(5); !errors.Is(err, strconv.ErrSyntax) {
144144
t.Errorf("GetBool() is \"%+v\", want \"%+v\".", err, strconv.ErrSyntax)
145145
}
146146
if _, err = rc.ColumnBool("name"); !errors.Is(err, strconv.ErrSyntax) && !errors.Is(err, tc.err) {
@@ -161,7 +161,7 @@ func TestNormal(t *testing.T) {
161161
t.Errorf("ColumnBool() is \"%+v\", want \"%+v\".", flagBool, tc.flagBool)
162162
}
163163
//float
164-
if _, err = rc.GetFloat64(5); !errors.Is(err, csvdata.ErrNullValue) {
164+
if _, err = rc.GetFloat64(5); !errors.Is(err, strconv.ErrSyntax) {
165165
t.Errorf("GetFloat() is \"%+v\", want \"%+v\".", err, strconv.ErrSyntax)
166166
}
167167
if _, err = rc.ColumnFloat64("name"); !errors.Is(err, strconv.ErrSyntax) && !errors.Is(err, tc.err) {
@@ -182,7 +182,7 @@ func TestNormal(t *testing.T) {
182182
t.Errorf("ColumnNullFloat64() is \"%+v\", want \"%+v\".", massFloat64, tc.massFloat64)
183183
}
184184
//int
185-
if _, err = rc.GetInt64(5, 10); !errors.Is(err, csvdata.ErrNullValue) {
185+
if _, err = rc.GetInt64(5, 10); !errors.Is(err, strconv.ErrSyntax) {
186186
t.Errorf("GetInt64() is \"%+v\", want \"%+v\".", err, strconv.ErrSyntax)
187187
}
188188
if _, err = rc.ColumnInt64("name", 10); !errors.Is(err, strconv.ErrSyntax) && !errors.Is(err, tc.err) {
@@ -251,6 +251,13 @@ func TestNormal(t *testing.T) {
251251
if err == nil && orderInt64 != tc.orderInt64 {
252252
t.Errorf("ColumnNullInt64() is \"%+v\", want \"%+v\".", orderInt64, tc.orderInt64)
253253
}
254+
// time
255+
if _, err = rc.ColumnNullTime("note", ""); !errors.Is(err, tc.err) {
256+
t.Errorf("ColumnNullTime() is \"%+v\", want \"%+v\".", err, tc.err)
257+
}
258+
if _, err = rc.ColumnTime("note", ""); !errors.Is(err, tc.err) {
259+
t.Errorf("ColumnNullTime() is \"%+v\", want \"%+v\".", err, tc.err)
260+
}
254261
}
255262
}
256263
}

exceldata/exceldata.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func New(xlsx *excelize.File, sheetName string) (*Reader, error) {
4040
return &Reader{rows}, nil
4141
}
4242

43+
// TrimSpace returns false.
44+
func (r *Reader) TrimSpace() bool {
45+
return false
46+
}
47+
4348
// LazyQuotes returns true.
4449
func (r *Reader) LazyQuotes() bool {
4550
return true

rows.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math"
66
"strconv"
77
"strings"
8+
"time"
89

910
"github.com/goark/errs"
1011
)
@@ -13,6 +14,7 @@ import (
1314
type RowsReader interface {
1415
Read() ([]string, error)
1516
Close() error
17+
TrimSpace() bool
1618
LazyQuotes() bool
1719
}
1820

@@ -29,6 +31,11 @@ func NewRows(rr RowsReader, headerFlag bool) *Rows {
2931
return &Rows{reader: rr, headerFlag: headerFlag, headerMap: map[string]int{}}
3032
}
3133

34+
// TrimSpace returns TrimSpace option value.
35+
func (r *Rows) TrimSpace() bool {
36+
return r.reader.TrimSpace()
37+
}
38+
3239
// LazyQuotes returns LazyQuotes option value.
3340
func (r *Rows) LazyQuotes() bool {
3441
return r.reader.LazyQuotes()
@@ -83,7 +90,10 @@ func (r *Rows) GetString(i int) (string, error) {
8390
if i < 0 || i >= len(r.rowdata) {
8491
return "", errs.Wrap(ErrOutOfIndex, errs.WithContext("index", i))
8592
}
86-
s := strings.TrimSpace(r.rowdata[i])
93+
s := r.rowdata[i]
94+
if r.TrimSpace() {
95+
s = strings.TrimSpace(s)
96+
}
8797
if r.LazyQuotes() {
8898
return s, nil
8999
}
@@ -147,6 +157,9 @@ func (r *Rows) GetBool(i int) (bool, error) {
147157
if err != nil {
148158
return false, errs.Wrap(err)
149159
}
160+
if !r.LazyQuotes() {
161+
s = strings.TrimSpace(s)
162+
}
150163
if len(s) == 0 {
151164
return false, errs.Wrap(ErrNullValue)
152165
}
@@ -188,6 +201,9 @@ func (r *Rows) GetFloat64(i int) (float64, error) {
188201
if err != nil {
189202
return 0, errs.Wrap(err)
190203
}
204+
if !r.LazyQuotes() {
205+
s = strings.TrimSpace(s)
206+
}
191207
if len(s) == 0 {
192208
return 0, errs.Wrap(ErrNullValue)
193209
}
@@ -229,6 +245,9 @@ func (r *Rows) GetInt64(i int, base int) (int64, error) {
229245
if err != nil {
230246
return 0, errs.Wrap(err)
231247
}
248+
if !r.LazyQuotes() {
249+
s = strings.TrimSpace(s)
250+
}
232251
if len(s) == 0 {
233252
return 0, errs.Wrap(ErrNullValue)
234253
}
@@ -351,6 +370,53 @@ func (r *Rows) ColumnByte(s string, base int) (byte, error) {
351370
return 0, errs.Wrap(ErrNullValue)
352371
}
353372

373+
// GetTime method returns type time.Time data in current row.
374+
func (r *Rows) GetTime(i int, layout string) (time.Time, error) {
375+
s, err := r.GetString(i)
376+
if err != nil {
377+
return time.Time{}, errs.Wrap(err)
378+
}
379+
if !r.LazyQuotes() {
380+
s = strings.TrimSpace(s)
381+
}
382+
if len(s) == 0 {
383+
return time.Time{}, errs.Wrap(ErrNullValue)
384+
}
385+
if len(layout) == 0 {
386+
layout = time.RFC3339
387+
}
388+
tm, err := time.Parse(layout, s)
389+
if err != nil {
390+
return time.Time{}, errs.Wrap(err)
391+
}
392+
return tm, nil
393+
}
394+
395+
// ColumnNullTime method returns sql.NullTime data in current row.
396+
func (r *Rows) ColumnNullTime(s, layout string) (sql.NullTime, error) {
397+
i, err := r.indexOf(s)
398+
if err != nil {
399+
return sql.NullTime{}, errs.Wrap(err)
400+
}
401+
res, err := r.GetTime(i, layout)
402+
if err != nil && !errs.Is(err, ErrNullValue) {
403+
return sql.NullTime{}, errs.Wrap(err)
404+
}
405+
return sql.NullTime{Time: res, Valid: err == nil}, nil
406+
}
407+
408+
// ColumnTime method returns type ime.Time data in current row.
409+
func (r *Rows) ColumnTime(s, layout string) (time.Time, error) {
410+
res, err := r.ColumnNullTime(s, layout)
411+
if err != nil {
412+
return time.Time{}, errs.Wrap(err)
413+
}
414+
if res.Valid {
415+
return res.Time, nil
416+
}
417+
return time.Time{}, errs.Wrap(ErrNullValue)
418+
}
419+
354420
// Close method is closing RowsReader instance.
355421
func (r *Rows) Close() error {
356422
return r.reader.Close()

0 commit comments

Comments
 (0)