Skip to content

Commit

Permalink
send errors to channel and handle nil checks for columnToNameMapping map
Browse files Browse the repository at this point in the history
Signed-off-by: MUzairS15 <[email protected]>
  • Loading branch information
MUzairS15 committed Jan 2, 2024
1 parent dfda411 commit 9863652
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 9863652

Please sign in to comment.