Skip to content

Commit

Permalink
Merge pull request #441 from MUzairS15/MUzairS15/csv-parser/improvemnts
Browse files Browse the repository at this point in the history
send errors to channel and handle nil checks for columnToNameMapping map
  • Loading branch information
MUzairS15 committed Jan 2, 2024
2 parents 6ca7bbe + 9863652 commit b998ab4
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions utils/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *CSV[E]) ExtractCols(lineForColNo int) ([]string, error) {
return data, nil
}

func (c *CSV[E]) Parse(ch chan E) error {
func (c *CSV[E]) Parse(ch chan E, errorChan chan error) error {
defer func() {
c.cancel()
}()
Expand All @@ -77,20 +77,23 @@ func (c *CSV[E]) Parse(ch chan E) error {

if c.predicateFunc != nil && c.predicateFunc(columnNames, values) {
for index, value := range values {
var attribute string
if index < size {
attribute = strings.ReplaceAll(strings.ToLower(columnNames[index]), " ", "_")
if c.columnToNameMapping != nil {
attribute, ok := c.columnToNameMapping[columnNames[index]]
if !ok {
attribute = strings.ReplaceAll(strings.ToLower(columnNames[index]), " ", "_")
key, ok := c.columnToNameMapping[columnNames[index]]
if ok {
attribute = key
}
data[attribute] = value
}
data[attribute] = value
}
}

parsedData, err := utils.MarshalAndUnmarshal[map[string]interface{}, E](data)
if err != nil {
return utils.ErrReadFile(err, c.filePath)
errorChan <- err
continue
}
ch <- parsedData
}
Expand Down

0 comments on commit b998ab4

Please sign in to comment.