generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #440 from MUzairS15/MUzairS15/feat/csv-parser
feat: Add support for `csv` parsing.
- Loading branch information
Showing
6 changed files
with
115 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "meshkit", | ||
"type": "library", | ||
"next_error_code": 11106 | ||
"next_error_code": 11107 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package csv | ||
|
||
import ( | ||
"context" | ||
"encoding/csv" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/layer5io/meshkit/utils" | ||
) | ||
|
||
type CSV[E any] struct { | ||
Context context.Context | ||
cancel context.CancelFunc | ||
reader *csv.Reader | ||
filePath string | ||
lineForColNo int | ||
// Stores the mapping for coumn name to golang equivalent attribute name. | ||
// It is optional and default mapping is the lower case representation with spaces replaced with "_" | ||
// eg: ColumnnName: Descritption, equivalent golang attribute to which it will be mapped during unmarshal "description". | ||
columnToNameMapping map[string]string | ||
predicateFunc func(columns []string, currentRow []string) bool | ||
} | ||
|
||
func NewCSVParser[E any](filePath string, lineForColNo int, colToNameMapping map[string]string, predicateFunc func(columns []string, currentRow []string) bool) (*CSV[E], error) { | ||
reader, err := os.Open(filePath) | ||
if err != nil { | ||
return nil, utils.ErrReadFile(err, filePath) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
return &CSV[E]{ | ||
Context: ctx, | ||
cancel: cancel, | ||
reader: csv.NewReader(reader), | ||
filePath: filePath, | ||
columnToNameMapping: colToNameMapping, | ||
lineForColNo: lineForColNo, | ||
predicateFunc: predicateFunc, | ||
}, nil | ||
} | ||
|
||
// "lineForColNo" line number where the columns are defined in the csv | ||
func (c *CSV[E]) ExtractCols(lineForColNo int) ([]string, error) { | ||
data := []string{} | ||
var err error | ||
for i := 0; i <= lineForColNo; i++ { | ||
data, err = c.reader.Read() | ||
if err != nil { | ||
return nil, utils.ErrReadFile(err, c.filePath) | ||
} | ||
} | ||
return data, nil | ||
} | ||
|
||
func (c *CSV[E]) Parse(ch chan E) error { | ||
defer func() { | ||
c.cancel() | ||
}() | ||
|
||
columnNames, err := c.ExtractCols(c.lineForColNo) | ||
size := len(columnNames) | ||
if err != nil { | ||
return utils.ErrReadFile(err, c.filePath) | ||
} | ||
for { | ||
data := make(map[string]interface{}) | ||
values, err := c.reader.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.predicateFunc != nil && c.predicateFunc(columnNames, values) { | ||
for index, value := range values { | ||
if index < size { | ||
if c.columnToNameMapping != nil { | ||
attribute, ok := c.columnToNameMapping[columnNames[index]] | ||
if !ok { | ||
attribute = strings.ReplaceAll(strings.ToLower(columnNames[index]), " ", "_") | ||
} | ||
data[attribute] = value | ||
} | ||
} | ||
} | ||
|
||
parsedData, err := utils.MarshalAndUnmarshal[map[string]interface{}, E](data) | ||
if err != nil { | ||
return utils.ErrReadFile(err, c.filePath) | ||
} | ||
ch <- parsedData | ||
} | ||
|
||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters