-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
197 additions
and
23 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
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/csv" | ||
"io" | ||
"log" | ||
"strings" | ||
) | ||
|
||
|
||
func ParseCsvSubQueryResults(content string) []SubQueryResults { | ||
return []SubQueryResults{ParseCsvSubQueryResult(content)} | ||
} | ||
|
||
func ParseCsvSubQueryResult(content string) SubQueryResults { | ||
reader := csv.NewReader(strings.NewReader(content)) | ||
rows := make([]map[string]interface{}, 0) | ||
var headerAcc HeaderAccumulator | ||
var headers []string | ||
|
||
first := true | ||
for { | ||
record, err := reader.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
log.Println("an error occurred parsing a csv line", err) | ||
} | ||
if first { | ||
headerAcc = *NewHeaderAccumulator(true, record) | ||
headers = record | ||
first = false | ||
continue | ||
} | ||
|
||
if len(record) != len(headers) { | ||
log.Println("csv record has different number of columns than header") | ||
continue | ||
} | ||
row := make(map[string]interface{}) | ||
for i, header := range headers { | ||
row[header] = record[i] | ||
} | ||
|
||
headerAcc.inspectRow(row) | ||
rows = append(rows, row) | ||
} | ||
return SubQueryResults{ | ||
Header: headerAcc.ToDataHeaders(), | ||
Content: rows, | ||
Prefix: "", | ||
Type: "csv", | ||
} | ||
} |
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,42 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParseCsvSubQueryResult(t *testing.T) { | ||
content := "name,age,email\nJohn,25,john@example.com\nJane,30,jane@example.com" | ||
expectedHeader := []DataHeader{ | ||
DataHeader{Name: "name", InferredType: "string"}, | ||
DataHeader{Name: "age", InferredType: "number"}, | ||
DataHeader{Name: "email", InferredType: "string"}, | ||
} | ||
expectedContent := []map[string]interface{}{ | ||
{"name": "John", "age": "25", "email": "john@example.com"}, | ||
{"name": "Jane", "age": "30", "email": "jane@example.com"}, | ||
} | ||
|
||
result := ParseCsvSubQueryResult(content) | ||
|
||
for i, header := range result.Header { | ||
if header.Name != expectedHeader[i].Name { | ||
t.Errorf("ParseCsvSubQueryResult() failed, expected header name: %s, got: %s", expectedHeader[i].Name, header.Name) | ||
} | ||
if header.InferredType != expectedHeader[i].InferredType { | ||
t.Errorf("ParseCsvSubQueryResult() failed, expected header inferred type: %s, got: %s", expectedHeader[i].InferredType, header.InferredType) | ||
} | ||
} | ||
|
||
if !reflect.DeepEqual(result.Content, expectedContent) { | ||
t.Errorf("ParseCsvSubQueryResult() failed, expected content: %v, got: %v", expectedContent, result.Content) | ||
} | ||
|
||
if result.Prefix != "" { | ||
t.Errorf("ParseCsvSubQueryResult() failed, expected prefix to be empty, got: %s", result.Prefix) | ||
} | ||
|
||
if result.Type != "csv" { | ||
t.Errorf("ParseCsvSubQueryResult() failed, expected type to be 'csv', got: %s", result.Type) | ||
} | ||
} |
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
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,5 @@ | ||
name,birth date,fav number | ||
dave,1980-01-01,7 | ||
jane,1981-02-02,8 | ||
fred,1982-03-03,9 | ||
mary,1983-04-04,42 |
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,16 @@ | ||
{ | ||
"name": "John Doe", | ||
"age": 30, | ||
"email": "johndoe@example.com", | ||
"address": { | ||
"street": "123 Main St", | ||
"city": "New York", | ||
"state": "NY", | ||
"zip": "10001" | ||
}, | ||
"interests": [ | ||
"programming", | ||
"reading", | ||
"traveling" | ||
] | ||
} |
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