diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2019-11-12 09:24:36 +0000 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2019-11-12 09:24:36 +0000 |
commit | a48f4347b4f51bbb779a52b8ead5d77152752f91 (patch) | |
tree | 913109c8eb9cf61dab64959558c94d14d72ff109 | |
parent | 9e0ad33a35572e0166984c568689b0ce923b14df (diff) |
tidying
-rw-r--r-- | reader/reader.go | 58 | ||||
-rw-r--r-- | reader/reader_test.go | 20 |
2 files changed, 41 insertions, 37 deletions
diff --git a/reader/reader.go b/reader/reader.go index c24adc2..f331022 100644 --- a/reader/reader.go +++ b/reader/reader.go @@ -17,6 +17,13 @@ const ( maxAlphabets = (maxCols / 26) - 1 ) +type ( + // Data from the sheet. + SheetData map[string]ExtractedCell + // Data from the file. + FileData map[string]SheetData +) + var colstream = cols(maxAlphabets) //DatamapLine - a line from the datamap. @@ -26,21 +33,21 @@ type DatamapLine struct { Cellref string } -//ExtractedCell is Data pulled from a cell +//ExtractedCell is data pulled from a cell. type ExtractedCell struct { - cell *xlsx.Cell - colL string - rowLidx int - value string + Cell *xlsx.Cell + ColL string + RowLidx int + Value string } -//Keylens returns the length of a key +//Keylens returns the length of a key. func Keylens(dml DatamapLine) (int, int) { return len(dml.Key), len(dml.Sheet) } -// sheetInSlice is a helper which returns true -// if a string is in a slice of strings +//sheetInSlice is a helper which returns true +// if a string is in a slice of strings. func sheetInSlice(list []string, key string) bool { for _, x := range list { if x == key { @@ -50,8 +57,8 @@ func sheetInSlice(list []string, key string) bool { return false } -// getSheetNames returns the number of Sheet field entries -// in a slice of DatamapLine structs +//getSheetNames returns the number of Sheet field entries +// in a slice of DatamapLine structs. func getSheetNames(dmls []DatamapLine) []string { var sheetNames []string for _, dml := range dmls { @@ -63,7 +70,7 @@ func getSheetNames(dmls []DatamapLine) []string { return sheetNames } -//ReadDML returns a slice of DatamapLine structs +//ReadDML returns a slice of DatamapLine structs. func ReadDML(path string) ([]DatamapLine, error) { var s []DatamapLine data, err := ioutil.ReadFile(path) @@ -92,7 +99,7 @@ func ReadDML(path string) ([]DatamapLine, error) { return s, nil } -//alphabet generates all the letters of the alphabet +//alphabet generates all the letters of the alphabet. func alphabet() []string { letters := make([]string, 26) for idx := range letters { @@ -116,11 +123,8 @@ func cols(n int) []string { return out } -type ( - SheetData map[string]ExtractedCell - FileData map[string]SheetData -) - +//ReadXLSToMap returns the file's data a map, +// keyed on sheet name. func ReadXLSToMap(dm string, tm string) FileData { // open the files @@ -144,11 +148,11 @@ func ReadXLSToMap(dm string, tm string) FileData { for rowLidx, row := range sheet.Rows { for colLidx, cell := range row.Cells { ex := ExtractedCell{ - cell: cell, - colL: colstream[colLidx], - rowLidx: rowLidx + 1, - value: cell.Value} - cellref := fmt.Sprintf("%s%d", ex.colL, ex.rowLidx) + Cell: cell, + ColL: colstream[colLidx], + RowLidx: rowLidx + 1, + Value: cell.Value} + cellref := fmt.Sprintf("%s%d", ex.ColL, ex.RowLidx) data[cellref] = ex } output[sheet.Name] = data @@ -168,14 +172,14 @@ func ReadXLSX(fn string) []ExtractedCell { for rowLidx, row := range sheet.Rows { for colLidx, cell := range row.Cells { ex := ExtractedCell{ - cell: cell, - colL: colstream[colLidx], - rowLidx: rowLidx + 1, - value: cell.Value} + Cell: cell, + ColL: colstream[colLidx], + RowLidx: rowLidx + 1, + Value: cell.Value} out = append(out, ex) text := cell.String() log.Printf("Sheet: %s Row: %d Col: %q Value: %s\n", - sheet.Name, ex.rowLidx, colstream[colLidx], text) + sheet.Name, ex.RowLidx, colstream[colLidx], text) } } } diff --git a/reader/reader_test.go b/reader/reader_test.go index c6e5a11..232e67d 100644 --- a/reader/reader_test.go +++ b/reader/reader_test.go @@ -41,14 +41,14 @@ func TestBadDMLLine(t *testing.T) { func TestReadXLSX(t *testing.T) { data := ReadXLSX("testdata/test_template.xlsx") - if data[0].colL != "A" { - t.Errorf("Expected data[0].colL to be A, got %v", data[0].colL) + if data[0].ColL != "A" { + t.Errorf("Expected data[0].colL to be A, got %v", data[0].ColL) } - if data[0].value != "Date:" { - t.Errorf("Expected data[0].value to be Date:, got %v", data[0].value) + if data[0].Value != "Date:" { + t.Errorf("Expected data[0].value to be Date:, got %v", data[0].Value) } - if data[0].rowLidx != 2 { - t.Errorf("Expected data[0].rowLidx to be 2, got %v", data[0].rowLidx) + if data[0].RowLidx != 2 { + t.Errorf("Expected data[0].rowLidx to be 2, got %v", data[0].RowLidx) } } @@ -103,11 +103,11 @@ func TestGetSheetsFromDM(t *testing.T) { func TestReadXLSXToMap(t *testing.T) { d := ReadXLSToMap("testdata/datamap.csv", "testdata/test_template.xlsx") - if d["Summary"]["A2"].value != "Date:" { - t.Errorf("Expected A2 in Summary sheet to be 'Date:' - instead it is %s", d["Summary"]["A2"].value) + if d["Summary"]["A2"].Value != "Date:" { + t.Errorf("Expected A2 in Summary sheet to be 'Date:' - instead it is %s", d["Summary"]["A2"].Value) } // TODO we actually want this to return an integer - do we? - if d["Another Sheet"]["F5"].value != "4.2" { - t.Errorf("Expected F5 in Another Sheet sheet to be 4.2 - instead it is %s", d["Another Sheet"]["F5"].value) + if d["Another Sheet"]["F5"].Value != "4.2" { + t.Errorf("Expected F5 in Another Sheet sheet to be 4.2 - instead it is %s", d["Another Sheet"]["F5"].Value) } } |