diff options
Diffstat (limited to 'pkg/datamaps')
-rw-r--r-- | pkg/datamaps/config.go | 10 | ||||
-rw-r--r-- | pkg/datamaps/db.go | 2 | ||||
-rw-r--r-- | pkg/datamaps/reader.go | 40 |
3 files changed, 27 insertions, 25 deletions
diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go index d7d2820..fe7a45e 100644 --- a/pkg/datamaps/config.go +++ b/pkg/datamaps/config.go @@ -25,8 +25,8 @@ const ( // for testing purposes. type getUserConfigDir func() (string, error) -// dbPathChecker contains the func used to create the user config dir. -type dbPathChecker struct { +// DBPathChecker contains the func used to create the user config dir. +type DBPathChecker struct { userConfig getUserConfigDir } @@ -34,13 +34,13 @@ type dbPathChecker struct { // func you want as the argument, as long as it matches the // type os.UserConfigDir. This makes it convenient for testing // and was done as an experiment here to practice mocking in Go. -func NewDBPathChecker(h getUserConfigDir) *dbPathChecker { - return &dbPathChecker{userConfig: h} +func NewDBPathChecker(h getUserConfigDir) *DBPathChecker { + return &DBPathChecker{userConfig: h} } // Check returns true if the necessary config files (including // the database) are in place - false if not -func (db *dbPathChecker) Check() bool { +func (db *DBPathChecker) Check() bool { userConfig, err := db.userConfig() if err != nil { log.Fatal(err) diff --git a/pkg/datamaps/db.go b/pkg/datamaps/db.go index 0e44c3e..d0f957e 100644 --- a/pkg/datamaps/db.go +++ b/pkg/datamaps/db.go @@ -231,7 +231,7 @@ func importXLSXtoDB(dmName string, returnName string, file string, db *sql.DB) e var dmlID *int if err := dmlIDRow.Scan(&dmlID); err != nil { - err := fmt.Errorf("cannot find a datamap_line row for %s and %s: %s.\n", sheetName, cellRef, err) + err := fmt.Errorf("cannot find a datamap_line row for %s and %s: %s", sheetName, cellRef, err) log.Println(err.Error()) } diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go index 9643fcd..7758822 100644 --- a/pkg/datamaps/reader.go +++ b/pkg/datamaps/reader.go @@ -25,11 +25,11 @@ type ( // 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. @@ -47,7 +47,9 @@ type extractedCell struct { Value string } -type extractedDatamapFile []datamapLine +// ExtractedDatamapFile is a slice of datamapLine structs, each of which encodes a single line +// in the datamap file/database table. +type ExtractedDatamapFile []datamapLine //sheetInSlice is a helper which returns true // if a string is in a slice of strings. @@ -63,7 +65,7 @@ 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 { +func getSheetNames(dmls ExtractedDatamapFile) []string { var sheetNames []string for _, dml := range dmls { @@ -77,8 +79,8 @@ func getSheetNames(dmls extractedDatamapFile) []string { // 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) @@ -116,14 +118,14 @@ 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 { @@ -152,13 +154,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 @@ -190,18 +192,18 @@ 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) { - ddata, err := DatamapFromDB(name, db) // this will need to return an extractedDatamapFile +func ExtractDBDatamap(name string, file string, db *sql.DB) (ExtractedData, error) { + ddata, err := DatamapFromDB(name, db) // this will need to return an ExtractedDatamapFile if err != nil { return nil, err } if len(ddata) == 0 { - return nil, fmt.Errorf("There is no datamap in the database matching name '%s'. Try running 'datamaps datamap --import...'.", name) + return nil, fmt.Errorf("there is no datamap in the database matching name '%s'. Try running 'datamaps datamap --import...'", name) } xdata := ReadXLSX(file) names := getSheetNames(ddata) - outer := make(extractedData, len(names)) + outer := make(ExtractedData, len(names)) // var inner map[string]xlsx.Cell for _, s := range names { @@ -224,7 +226,7 @@ func ExtractDBDatamap(name string, file string, db *sql.DB) (extractedData, erro // using the datamap as a filter, keyed on sheet name. All values // are returned as strings. (Currently deprecated in favour of // ExtractDBDatamap. -func extract(dm string, path string) extractedData { +func extract(dm string, path string) ExtractedData { xdata := ReadXLSX(path) ddata, err := ReadDML(dm) @@ -233,7 +235,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 { |