-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add parse package to parse csv files
- Loading branch information
danielkvist
committed
Jul 22, 2019
1 parent
96d8cb5
commit 3f601c3
Showing
2 changed files
with
115 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package parser | ||
|
||
import ( | ||
"bufio" | ||
"encoding/csv" | ||
"io" | ||
"os" | ||
) | ||
|
||
type Site struct { | ||
Name string | ||
URL string | ||
} | ||
|
||
func Parse(file string) ([]*Site, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r := csv.NewReader(bufio.NewReader(f)) | ||
return parse(r) | ||
} | ||
|
||
func parse(r *csv.Reader) ([]*Site, error) { | ||
var sites []*Site | ||
|
||
for { | ||
line, err := r.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sites = append(sites, &Site{Name: line[0], URL: line[1]}) | ||
} | ||
|
||
return sites, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package parser | ||
|
||
import ( | ||
"encoding/csv" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
input string | ||
expectedResult []*Site | ||
}{ | ||
{ | ||
name: "one line", | ||
input: "500px,https://500px.com/", | ||
expectedResult: []*Site{ | ||
{ | ||
Name: "500px", | ||
URL: "https://500px.com/", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "multiple lines", | ||
input: "500px,https://500px.com/\nGoogle,https://google.com\nGitHub,https://github.com", | ||
expectedResult: []*Site{ | ||
{ | ||
Name: "500px", | ||
URL: "https://500px.com/", | ||
}, | ||
{ | ||
Name: "Google", | ||
URL: "https://google.com", | ||
}, | ||
{ | ||
Name: "GitHub", | ||
URL: "https://github.com", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "empty", | ||
input: "", | ||
expectedResult: []*Site{}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
r := csv.NewReader(strings.NewReader(tc.input)) | ||
sites, err := parse(r) | ||
if err != nil { | ||
t.Fatalf("while parsing csv input: %v", err) | ||
} | ||
|
||
if len(sites) != len(tc.expectedResult) { | ||
t.Fatalf("expected len of parsed sites to be %v. got=%v", len(tc.expectedResult), len(sites)) | ||
} | ||
|
||
for i, es := range tc.expectedResult { | ||
if sites[i].Name != es.Name { | ||
t.Fatalf("expected a parsed site called %q. got a site called %q", es.Name, sites[i].Name) | ||
} | ||
|
||
if sites[i].URL != es.URL { | ||
t.Fatalf("expected a parsed site with URL %q. got a site with URL %q", es.URL, sites[i].URL) | ||
} | ||
} | ||
}) | ||
} | ||
} |