Skip to content

Commit

Permalink
fix csv parsing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl authored and jchappelow committed Dec 4, 2024
1 parent 3f357ed commit 04487fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/kwil-cli/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func cleanHeader(header string) string {
// remove leading and trailing quotes
str = strings.Trim(str, "\"")

// remove leading and trailing spaces
str = strings.TrimSpace(str)

return str
}

Expand All @@ -82,6 +85,7 @@ func cleanHeader(header string) string {
// action input name.
func (c *CSV) BuildInputs(inputNames map[string]string) ([]map[string]string, error) {
resultMap := make([]map[string]string, 0)

err := c.ForEachRecord(func(record []CSVCell) error {
input := make(map[string]string)

Expand Down Expand Up @@ -140,7 +144,6 @@ type CSVCell struct {
func (c *CSV) buildCSVCells(record []string) []CSVCell {
csvVals := make([]CSVCell, len(record))
for i, column := range record {

csvVals[i] = CSVCell{
Column: &c.Header[i],
Value: column,
Expand Down
20 changes: 20 additions & 0 deletions cmd/kwil-cli/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/kwilteam/kwil-db/cmd/kwil-cli/csv"
"github.com/stretchr/testify/require"
)

func Test_CSV(t *testing.T) {
Expand Down Expand Up @@ -110,3 +111,22 @@ func Test_ReadDoubleQuotes(t *testing.T) {
t.Fatal(`expected 2 columns, got: `, len(res[0]))
}
}

func Test_JSON(t *testing.T) {
jsonCsv, err := os.Open("./json.csv")
if err != nil {
t.Fatal(err)
}

c, err := csv.Read(jsonCsv, csv.ContainsHeader)
if err != nil {
t.Fatal(err)
}

require.Equal(t, c.Header, []string{"col1", "col2"})

require.Equal(t, 1, len(c.Records))
require.Equal(t, 2, len(c.Records[0]))
require.Equal(t, "test", c.Records[0][0])
require.Equal(t, `{"id":"6814e549-34f2-42db-9db3-61659b12708d","type":"human","level":"human","status":"approved"}`, c.Records[0][1])
}
2 changes: 2 additions & 0 deletions cmd/kwil-cli/csv/json.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
col1, col2
test,"{""id"":""6814e549-34f2-42db-9db3-61659b12708d"",""type"":""human"",""level"":""human"",""status"":""approved""}"

0 comments on commit 04487fa

Please sign in to comment.