diff options
author | Matthew Lemon <matt@matthewlemon.com> | 2020-07-26 21:00:08 +0100 |
---|---|---|
committer | Matthew Lemon <matt@matthewlemon.com> | 2020-07-26 21:00:08 +0100 |
commit | e1903f6a9445cd5e90809844d7e08be3ca55d16d (patch) | |
tree | d8481894371ec23e410822b5e01ac6a1d3ccd65c /pkg/datamaps/reader.go | |
parent | b059e3ffb9a37fc0a941458d2cd143fad67a105f (diff) |
more encapsulation
Diffstat (limited to 'pkg/datamaps/reader.go')
-rw-r--r-- | pkg/datamaps/reader.go | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go index 311dceb..3f1e26d 100644 --- a/pkg/datamaps/reader.go +++ b/pkg/datamaps/reader.go @@ -22,32 +22,32 @@ import ( ) type ( - // SheetData is the data from the sheet. - SheetData map[string]ExtractedCell + // sheetData is the data from the sheet. + sheetData map[string]extractedCell - // FileData is the data from the file. - FileData map[string]SheetData + // fileData is the data from the file. + fileData map[string]sheetData - // ExtractedData is the Extracted data from the file, filtered by a Datamap. - ExtractedData map[string]map[string]xlsx.Cell + // extractedData is the Extracted data from the file, filtered by a Datamap. + extractedData map[string]map[string]xlsx.Cell ) -//DatamapLine - a line from the datamap. -type DatamapLine struct { +//datamapLine - a line from the datamap. +type datamapLine struct { Key string Sheet string Cellref string } -//ExtractedCell is data pulled from a cell. -type ExtractedCell struct { +//extractedCell is data pulled from a cell. +type extractedCell struct { Cell *xlsx.Cell Col string Row int Value string } -type ExtractedDatamapFile []DatamapLine +type extractedDatamapFile []datamapLine //sheetInSlice is a helper which returns true // if a string is in a slice of strings. @@ -62,8 +62,8 @@ func sheetInSlice(list []string, key string) bool { } //getSheetNames returns the number of Sheet field entries -// in a slice of DatamapLine structs. -func getSheetNames(dmls ExtractedDatamapFile) []string { +// in a slice of datamapLine structs. +func getSheetNames(dmls extractedDatamapFile) []string { var sheetNames []string for _, dml := range dmls { @@ -75,10 +75,10 @@ func getSheetNames(dmls ExtractedDatamapFile) []string { return sheetNames } -// ReadDML returns a slice of DatamapLine structs given a +// ReadDML returns a slice of datamapLine structs given a // path to a datamap file. -func ReadDML(path string) (ExtractedDatamapFile, error) { - var s ExtractedDatamapFile +func ReadDML(path string) (extractedDatamapFile, error) { + var s extractedDatamapFile data, err := ioutil.ReadFile(path) @@ -103,7 +103,7 @@ func ReadDML(path string) (ExtractedDatamapFile, error) { continue } - dml := DatamapLine{ + dml := datamapLine{ Key: strings.Trim(record[0], " "), Sheet: strings.Trim(record[1], " "), Cellref: strings.Trim(record[2], " ")} @@ -116,18 +116,18 @@ func ReadDML(path string) (ExtractedDatamapFile, error) { // ReadXLSX returns a file at path's data as a map, // keyed on sheet name. All values are returned as strings. // Paths to a datamap and the spreadsheet file required. -func ReadXLSX(path string) FileData { +func ReadXLSX(path string) fileData { // open the files data, err := xlsx.OpenFile(path) if err != nil { log.Fatal(err) } - outer := make(FileData, 1) + outer := make(fileData, 1) // get the data for _, sheet := range data.Sheets { - inner := make(SheetData) + inner := make(sheetData) for rowLidx, row := range sheet.Rows { for colLidx, cell := range row.Cells { @@ -136,7 +136,7 @@ func ReadXLSX(path string) FileData { log.Fatal(err) } - ex := ExtractedCell{ + ex := extractedCell{ Cell: cell, Col: colStr, Row: rowLidx + 1, @@ -152,13 +152,13 @@ func ReadXLSX(path string) FileData { return outer } -// DatamapFromDB creates an ExtractedDatamapFile from the database given +// DatamapFromDB creates an extractedDatamapFile from the database given // the name of a datamap. Of course, in this instance, the data is not // coming from a datamap file (such as datamap.csv) but from datamap data // previous stored in the database by DatamapToDB or similar. -func DatamapFromDB(name string, db *sql.DB) (ExtractedDatamapFile, error) { +func DatamapFromDB(name string, db *sql.DB) (extractedDatamapFile, error) { - var out ExtractedDatamapFile + var out extractedDatamapFile query := ` select @@ -182,7 +182,7 @@ func DatamapFromDB(name string, db *sql.DB) (ExtractedDatamapFile, error) { return nil, err } - out = append(out, DatamapLine{Key: key, Sheet: sheet, Cellref: cellref}) + out = append(out, datamapLine{Key: key, Sheet: sheet, Cellref: cellref}) } return out, nil @@ -190,16 +190,16 @@ func DatamapFromDB(name string, db *sql.DB) (ExtractedDatamapFile, error) { // ExtractDBDatamap uses a datamap named from the database db to extract values // from the populated spreadsheet file file. -func ExtractDBDatamap(name string, file string, db *sql.DB) (ExtractedData, error) { +func ExtractDBDatamap(name string, file string, db *sql.DB) (extractedData, error) { xdata := ReadXLSX(file) - ddata, err := DatamapFromDB(name, db) // this will need to return an ExtractedDatamapFile + ddata, err := DatamapFromDB(name, db) // this will need to return an extractedDatamapFile if err != nil { return nil, err } names := getSheetNames(ddata) - outer := make(ExtractedData, len(names)) + outer := make(extractedData, len(names)) inner := make(map[string]xlsx.Cell) for _, i := range ddata { @@ -215,11 +215,11 @@ func ExtractDBDatamap(name string, file string, db *sql.DB) (ExtractedData, erro return outer, nil } -//Extract returns the file at path's data as a map, +// extract returns the file at path's data as a map, // using the datamap as a filter, keyed on sheet name. All values -// are returned as strings. -// Paths to a datamap and the spreadsheet file required. -func Extract(dm string, path string) ExtractedData { +// are returned as strings. (Currently deprecated in favour of +// ExtractDBDatamap. +func extract(dm string, path string) extractedData { xdata := ReadXLSX(path) ddata, err := ReadDML(dm) @@ -228,7 +228,7 @@ func Extract(dm string, path string) ExtractedData { } names := getSheetNames(ddata) - outer := make(ExtractedData, len(names)) + outer := make(extractedData, len(names)) inner := make(map[string]xlsx.Cell) for _, i := range ddata { @@ -244,8 +244,8 @@ func Extract(dm string, path string) ExtractedData { return outer } -//GetTargetFiles finds all xlsx and xlsm files in directory. -func GetTargetFiles(path string) ([]string, error) { +//getTargetFiles finds all xlsx and xlsm files in directory. +func getTargetFiles(path string) ([]string, error) { if lastchar := path[len(path)-1:]; lastchar != string(filepath.Separator) { return nil, fmt.Errorf("path must end in a %s character", string(filepath.Separator)) } |