Skip to content

Commit

Permalink
[csv] Add more helpers for working with CSV row data
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed May 6, 2024
1 parent f33045a commit 1c19686
Show file tree
Hide file tree
Showing 5 changed files with 369 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 12.123.0

- `[csv]` Added method `Reader.Line`
- `[csv]` Added more helpers for working with CSV row data

### 12.122.0

Expand Down
111 changes: 108 additions & 3 deletions csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,119 @@ func (r Row) GetI(index int) (int, error) {
return strconv.Atoi(r.Get(index))
}

// GetI8 returns cell value as int8
func (r Row) GetI8(index int) (int8, error) {
i, err := strconv.ParseInt(r.Get(index), 10, 8)

if err != nil {
return 0, err
}

return int8(i), nil
}

// GetI16 returns cell value as int16
func (r Row) GetI16(index int) (int16, error) {
i, err := strconv.ParseInt(r.Get(index), 10, 16)

if err != nil {
return 0, err
}

return int16(i), nil
}

// GetI32 returns cell value as int32
func (r Row) GetI32(index int) (int32, error) {
i, err := strconv.ParseInt(r.Get(index), 10, 32)

if err != nil {
return 0, err
}

return int32(i), nil
}

// GetI64 returns cell value as int64
func (r Row) GetI64(index int) (int64, error) {
i, err := strconv.ParseInt(r.Get(index), 10, 64)

if err != nil {
return 0, err
}

return int64(i), nil
}

// GetF returns cell value as float
func (r Row) GetF(index int) (float64, error) {
return strconv.ParseFloat(r.Get(index), 64)
}

// GetU returns cell value as uint64
func (r Row) GetU(index int) (uint64, error) {
return strconv.ParseUint(r.Get(index), 10, 64)
// GetF32 returns cell value as float32
func (r Row) GetF32(index int) (float32, error) {
f, err := strconv.ParseFloat(r.Get(index), 32)

if err != nil {
return 0, err
}

return float32(f), nil
}

// GetU returns cell value as uint
func (r Row) GetU(index int) (uint, error) {
u, err := strconv.ParseUint(r.Get(index), 10, 32)

if err != nil {
return 0, err
}

return uint(u), nil
}

// GetU8 returns cell value as uint8
func (r Row) GetU8(index int) (uint8, error) {
u, err := strconv.ParseUint(r.Get(index), 10, 8)

if err != nil {
return 0, err
}

return uint8(u), nil
}

// GetU16 returns cell value as uint16
func (r Row) GetU16(index int) (uint16, error) {
u, err := strconv.ParseUint(r.Get(index), 10, 16)

if err != nil {
return 0, err
}

return uint16(u), nil
}

// GetU32 returns cell value as uint32
func (r Row) GetU32(index int) (uint32, error) {
u, err := strconv.ParseUint(r.Get(index), 10, 32)

if err != nil {
return 0, err
}

return uint32(u), nil
}

// GetU64 returns cell value as uint64
func (r Row) GetU64(index int) (uint64, error) {
u, err := strconv.ParseUint(r.Get(index), 10, 64)

if err != nil {
return 0, err
}

return uint64(u), nil
}

// ForEach executes given function for every cell in a row
Expand Down
66 changes: 57 additions & 9 deletions csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (s *CSVSuite) TestNil(c *C) {
}

func (s *CSVSuite) TestRow(c *C) {
r := Row{"test", "", "1234", "12.34", "Yes"}
r := Row{"test", "", "33", "12.34", "Yes"}
c.Assert(r.Size(), Equals, 5)
c.Assert(r.Cells(), Equals, 4)
c.Assert(r.IsEmpty(), Equals, false)
Expand All @@ -166,16 +166,43 @@ func (s *CSVSuite) TestRow(c *C) {
c.Assert(r.Get(0), Equals, "test")
c.Assert(r.Get(10), Equals, "")

ci, err := r.GetI(2)
c.Assert(ci, Equals, 1234)
i, err := r.GetI(2)
c.Assert(i, Equals, 33)
c.Assert(err, IsNil)
i8, err := r.GetI8(2)
c.Assert(i8, Equals, int8(33))
c.Assert(err, IsNil)
i16, err := r.GetI16(2)
c.Assert(i16, Equals, int16(33))
c.Assert(err, IsNil)
i32, err := r.GetI32(2)
c.Assert(i32, Equals, int32(33))
c.Assert(err, IsNil)
i64, err := r.GetI64(2)
c.Assert(i64, Equals, int64(33))
c.Assert(err, IsNil)

cf, err := r.GetF(3)
c.Assert(cf, Equals, 12.34)
f64, err := r.GetF(3)
c.Assert(f64, Equals, 12.34)
c.Assert(err, IsNil)
f32, err := r.GetF32(3)
c.Assert(f32, Equals, float32(12.34))
c.Assert(err, IsNil)

cu, err := r.GetU(2)
c.Assert(cu, Equals, uint64(1234))
u, err := r.GetU(2)
c.Assert(u, Equals, uint(33))
c.Assert(err, IsNil)
u8, err := r.GetU8(2)
c.Assert(u8, Equals, uint8(33))
c.Assert(err, IsNil)
u16, err := r.GetU16(2)
c.Assert(u16, Equals, uint16(33))
c.Assert(err, IsNil)
u32, err := r.GetU32(2)
c.Assert(u32, Equals, uint32(33))
c.Assert(err, IsNil)
u64, err := r.GetU64(2)
c.Assert(u64, Equals, uint64(33))
c.Assert(err, IsNil)

c.Assert(r.GetB(1), Equals, false)
Expand All @@ -187,8 +214,29 @@ func (s *CSVSuite) TestRow(c *C) {
dummyFn = func(i int, v string) error { return errors.New("1") }
c.Assert(r.ForEach(dummyFn), NotNil)

c.Assert(r.ToString(';'), Equals, "test;;1234;12.34;Yes")
c.Assert(string(r.ToBytes(';')), Equals, "test;;1234;12.34;Yes")
c.Assert(r.ToString(';'), Equals, "test;;33;12.34;Yes")
c.Assert(string(r.ToBytes(';')), Equals, "test;;33;12.34;Yes")

_, err = r.GetI8(0)
c.Assert(err, NotNil)
_, err = r.GetI16(0)
c.Assert(err, NotNil)
_, err = r.GetI32(0)
c.Assert(err, NotNil)
_, err = r.GetI64(0)
c.Assert(err, NotNil)
_, err = r.GetU(0)
c.Assert(err, NotNil)
_, err = r.GetU8(0)
c.Assert(err, NotNil)
_, err = r.GetU16(0)
c.Assert(err, NotNil)
_, err = r.GetU32(0)
c.Assert(err, NotNil)
_, err = r.GetU64(0)
c.Assert(err, NotNil)
_, err = r.GetF32(0)
c.Assert(err, NotNil)
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
Loading

0 comments on commit 1c19686

Please sign in to comment.