aboutsummaryrefslogtreecommitdiffstats
path: root/reader
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2019-10-21 20:19:13 +0100
committerMatthew Lemon <lemon@matthewlemon.com>2019-10-21 20:19:13 +0100
commitcdadb51e3e4ef5fb96da41496bafb11572ec487e (patch)
tree619ae2c290b09dd68e233f3ec8781a0a27f0356c /reader
parent950b406793cf5cd1ff64b9266cc7c91c93da803a (diff)
passing test
Diffstat (limited to 'reader')
-rw-r--r--reader/reader.go15
-rw-r--r--reader/reader_test.go7
2 files changed, 17 insertions, 5 deletions
diff --git a/reader/reader.go b/reader/reader.go
index 46c7144..95ef193 100644
--- a/reader/reader.go
+++ b/reader/reader.go
@@ -17,17 +17,26 @@ type DatamapLine struct {
Cellref string
}
+type fileError struct {
+ file string
+ msg string
+}
+
+func (e *fileError) Error() string {
+ return fmt.Sprintf("%s", e.msg)
+}
+
//Keylens returns the length of a key
func Keylens(dml DatamapLine) (int, int) {
return len(dml.Key), len(dml.Sheet)
}
//ReadDML returns a pointer to a slice of DatamapLine structs
-func ReadDML(path string) *[]DatamapLine {
+func ReadDML(path string) (*[]DatamapLine, error) {
var s []DatamapLine
data, err := ioutil.ReadFile(path)
if err != nil {
- fmt.Printf("Error! Cannot open file %s", err)
+ return &s, &fileError{path, "Cannot open."}
}
r := csv.NewReader(strings.NewReader(string(data)))
for {
@@ -49,7 +58,7 @@ func ReadDML(path string) *[]DatamapLine {
// fmt.Printf("Key length: %d\n", klen)
// fmt.Printf("Sheet length: %d\n\n", slen)
}
- return &s
+ return &s, nil
}
//ReadXLSX reads an XLSX file
diff --git a/reader/reader_test.go b/reader/reader_test.go
index 1c249b5..44c2a56 100644
--- a/reader/reader_test.go
+++ b/reader/reader_test.go
@@ -1,9 +1,12 @@
package reader
-import "testing"
+import (
+ "testing"
+)
func TestReadDML(t *testing.T) {
- dmlData := *ReadDML("/home/lemon/Documents/datamaps/input/datamap.csv")
+ d, _ := ReadDML("/home/lemon/Documents/datamaps/input/datamap.csv")
+ dmlData := *d
if dmlData[0].Key != "Project/Programme Name" {
t.Errorf("dmlData[0].key = %s; want Project/Programme Name", dmlData[0].Key)
}