Skip to content

Commit

Permalink
Merge pull request #469 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.123.0
  • Loading branch information
andyone authored May 6, 2024
2 parents de98747 + 1c19686 commit c907182
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 38 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ jobs:
with:
path: ${{env.SRC_DIR}}

- uses: actions/cache@v4
- name: Use cache for dependencies data
uses: actions/cache@v4
id: cache-deps
with:
path: |
Expand Down Expand Up @@ -95,7 +96,8 @@ jobs:
with:
path: ${{env.SRC_DIR}}

- uses: actions/cache@v4
- name: Use cache for dependencies data
uses: actions/cache@v4
id: cache-deps
with:
path: |
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

### 12.123.0

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

### 12.122.0

- `[csv]` Added helpers for working with CSV row
Expand Down
133 changes: 129 additions & 4 deletions csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// COMMA is default separator for CSV cells
const COMMA = ';'

// ////////////////////////////////////////////////////////////////////////////////// //

// Reader is CSV reader struct
type Reader struct {
Comma rune
SkipHeader bool

headerSkipped bool
currentLine int

br *bufio.Reader
}
Expand All @@ -45,7 +51,7 @@ var ErrNilReader = errors.New("Reader is nil")
// NewReader create new CSV reader
func NewReader(r io.Reader) *Reader {
return &Reader{
Comma: ';',
Comma: COMMA,
SkipHeader: false,

br: bufio.NewReader(r),
Expand All @@ -63,6 +69,7 @@ func (r *Reader) Read() (Row, error) {
if r.SkipHeader && !r.headerSkipped {
r.br.ReadLine()
r.headerSkipped = true
r.currentLine++
}

str, _, err := r.br.ReadLine()
Expand All @@ -71,6 +78,8 @@ func (r *Reader) Read() (Row, error) {
return nil, err
}

r.currentLine++

return strings.Split(string(str), string(r.Comma)), nil
}

Expand All @@ -87,6 +96,7 @@ func (r *Reader) ReadTo(dst Row) error {
if r.SkipHeader && !r.headerSkipped {
r.br.ReadLine()
r.headerSkipped = true
r.currentLine++
}

str, _, err := r.br.ReadLine()
Expand All @@ -96,6 +106,7 @@ func (r *Reader) ReadTo(dst Row) error {
}

parseAndFill(string(str), dst, string(r.Comma))
r.currentLine++

return nil
}
Expand All @@ -122,6 +133,15 @@ func (r *Reader) WithHeaderSkip(flag bool) *Reader {
return r
}

// Line returns number of the last line read
func (r *Reader) Line() int {
if r == nil {
return 0
}

return r.currentLine
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Size returns size of the row
Expand Down Expand Up @@ -176,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
71 changes: 62 additions & 9 deletions csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (s *CSVSuite) TestRead(c *C) {

line++
}

c.Assert(reader.Line(), Equals, 5)
}

func (s *CSVSuite) TestReadTo(c *C) {
Expand Down Expand Up @@ -124,6 +126,8 @@ func (s *CSVSuite) TestReadTo(c *C) {

line++
}

c.Assert(reader.Line(), Equals, 5)
}

func (s *CSVSuite) TestLineParser(c *C) {
Expand All @@ -149,10 +153,11 @@ func (s *CSVSuite) TestNil(c *C) {
c.Assert(r.ReadTo(b), DeepEquals, ErrNilReader)
c.Assert(r.WithComma('X'), IsNil)
c.Assert(r.WithHeaderSkip(false), IsNil)
c.Assert(r.Line(), Equals, 0)
}

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 @@ -161,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 @@ -182,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 c907182

Please sign in to comment.