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 | |
parent | b059e3ffb9a37fc0a941458d2cd143fad67a105f (diff) |
more encapsulation
Diffstat (limited to '')
-rw-r--r-- | pkg/datamaps/db.go | 3 | ||||
-rw-r--r-- | pkg/datamaps/reader.go | 70 | ||||
-rw-r--r-- | pkg/datamaps/reader_test.go | 14 |
3 files changed, 43 insertions, 44 deletions
diff --git a/pkg/datamaps/db.go b/pkg/datamaps/db.go index ad612c4..e3b5e7a 100644 --- a/pkg/datamaps/db.go +++ b/pkg/datamaps/db.go @@ -52,8 +52,7 @@ func setupDB(path string) (*sql.DB, error) { return db, nil } -// DatamapToDB takes a slice of DatamapLine and writes it to a sqlite3 db file. -// func DatafmapToDB(d_path string, data ExtractedDatamapFile, dm_name string, dm_path string) error { +// DatamapToDB takes a slice of datamapLine and writes it to a sqlite3 db file. func DatamapToDB(opts *Options) error { fmt.Printf("Importing datamap file %s and naming it %s.\n", opts.DMPath, opts.DMName) 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)) } diff --git a/pkg/datamaps/reader_test.go b/pkg/datamaps/reader_test.go index e5f2c98..c221b26 100644 --- a/pkg/datamaps/reader_test.go +++ b/pkg/datamaps/reader_test.go @@ -89,7 +89,7 @@ func TestReadXLSX(t *testing.T) { // t.Errorf("Unable to write datamap to database file because %v.", err) // } -// d := ExtractDBDatamap("First Datamap", "testdata/test_template.xlsx") +// d := extractDBDatamap("First Datamap", "testdata/test_template.xlsx") // } func TestDMLSliceFromDatabase(t *testing.T) { @@ -183,7 +183,7 @@ func TestExtractUsingDBDM(t *testing.T) { } func TestExtract(t *testing.T) { - d := Extract("testdata/datamap.csv", "testdata/test_template.xlsx") + d := extract("testdata/datamap.csv", "testdata/test_template.xlsx") cases := []struct { sheet, cellref, val string }{ @@ -207,7 +207,7 @@ func TestExtract(t *testing.T) { } } -// func TestGetTargetFiles(t *testing.T) { +// func TestgetTargetFiles(t *testing.T) { // // This is not a suitable test for parameterisation, but doing it this way anyway. // type args struct { // path string @@ -218,7 +218,7 @@ func TestExtract(t *testing.T) { // want []string // wantErr bool // }{ -// {"TestGetTargetFiles", +// {"TestgetTargetFiles", // args{"/home/lemon/go/src/github.com/hammerheadlemon/datamaps-go/reader/testdata/"}, // []string{"/home/lemon/go/src/github.com/hammerheadlemon/datamaps-go/reader/testdata/test_template.xlsx"}, // false, @@ -226,13 +226,13 @@ func TestExtract(t *testing.T) { // } // for _, tt := range tests { // t.Run(tt.name, func(t *testing.T) { -// got, err := GetTargetFiles(tt.args.path) +// got, err := getTargetFiles(tt.args.path) // if (err != nil) != tt.wantErr { -// t.Errorf("GetTargetFiles() error = %v, wantErr %v", err, tt.wantErr) +// t.Errorf("getTargetFiles() error = %v, wantErr %v", err, tt.wantErr) // return // } // if !reflect.DeepEqual(got, tt.want) { -// t.Errorf("GetTargetFiles() = %v, want %v", got, tt.want) +// t.Errorf("getTargetFiles() = %v, want %v", got, tt.want) // } // }) // } |