aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <matt@matthewlemon.com>2020-07-24 10:12:01 +0100
committerMatthew Lemon <matt@matthewlemon.com>2020-07-24 10:12:01 +0100
commit36f3983b039905c54a957f236e4a9718ea7c918f (patch)
tree46c5424ed0930a7dfbf54ca0f77d05924fc96f01
parent3b82b7463987ff637530cb403b6d294978bbd115 (diff)
mostly linting
-rw-r--r--.gitignore1
-rw-r--r--cmd/datamaps/main.go9
-rw-r--r--pkg/datamaps/config.go16
-rw-r--r--pkg/datamaps/db_test.go10
-rw-r--r--pkg/datamaps/reader.go29
-rw-r--r--pkg/datamaps/reader_test.go7
6 files changed, 65 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore
index 7a40cc7..fe8ea92 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ reader/testdata/test.db
go.sum
/datamaps
pkg/datamaps/testdata/test.db
+/main
diff --git a/cmd/datamaps/main.go b/cmd/datamaps/main.go
index 32b7ce8..b0d1c1d 100644
--- a/cmd/datamaps/main.go
+++ b/cmd/datamaps/main.go
@@ -26,34 +26,41 @@ func setUp() (string, error) {
// check if config folder exists
configPath := filepath.Join(dir, configDirName)
dbPath := filepath.Join(configPath, dbName)
+
if _, err := os.Stat(configPath); os.IsNotExist(err) {
log.Println("Config directory does not exist.")
log.Printf("Creating config directory %s\n", configPath)
+
if err := os.Mkdir(filepath.Join(dir, "datamaps-go"), 0700); err != nil {
return "", err
}
} else {
log.Println("Config directory found.")
}
+
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
log.Println("Database does not exist.")
+
_, err = os.Create(dbPath)
+
if err != nil {
return "", err
}
+
log.Printf("Creating database file at %s\n", dbPath)
_, err := datamaps.SetupDB(dbPath)
+
if err != nil {
return "", err
}
} else {
log.Println("Database file found.")
}
+
return dir, nil
}
func main() {
-
opts := datamaps.ParseOptions()
switch opts.Command {
diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go
index 94ce068..dbea8b7 100644
--- a/pkg/datamaps/config.go
+++ b/pkg/datamaps/config.go
@@ -16,7 +16,9 @@ func getUserConfigDir() (string, error) {
if err != nil {
return "", err
}
+
configPath := filepath.Join(dir, configDirName)
+
return configPath, nil
}
@@ -25,10 +27,11 @@ func defaultDMPath() (string, error) {
if err != nil {
return "", err
}
+
return filepath.Join(dir, "Documents", "datamaps"), nil
}
-// Options for the whole CLI application
+// Options for the whole CLI application.
type Options struct {
Command string
DBPath string
@@ -43,13 +46,16 @@ func defaultOptions() *Options {
if err != nil {
log.Fatalf("Unable to get user config directory %v", err)
}
+
dmpath, err := defaultDMPath()
+
if err != nil {
log.Fatalf("Unable to get default datamaps directory %v", err)
}
+
return &Options{
Command: "help",
- DBPath: filepath.Join(dbpath, "datamaps.db"),
+ DBPath: filepath.Join(dbpath, dbName),
DMPath: dmpath,
DMName: "Unnamed Datamap",
DMOverwrite: false,
@@ -57,13 +63,14 @@ func defaultOptions() *Options {
}
}
-// nextString get the next string in a slice
+// nextString get the next string in a slice.
func nextString(args []string, i *int, message string) string {
if len(args) > *i+1 {
*i++
} else {
log.Fatal(message)
}
+
return args[*i]
}
@@ -78,7 +85,9 @@ func processOptions(opts *Options, allArgs []string) {
default:
log.Fatal("No relevant command provided.")
}
+
restArgs := allArgs[1:]
+
for i := 0; i < len(allArgs[1:]); i++ {
arg := restArgs[i]
switch arg {
@@ -98,5 +107,6 @@ func processOptions(opts *Options, allArgs []string) {
func ParseOptions() *Options {
opts := defaultOptions()
processOptions(opts, os.Args[1:])
+
return opts
}
diff --git a/pkg/datamaps/db_test.go b/pkg/datamaps/db_test.go
index a9988d1..4d2179f 100644
--- a/pkg/datamaps/db_test.go
+++ b/pkg/datamaps/db_test.go
@@ -7,19 +7,26 @@ import (
func TestOpenSQLiteFile(t *testing.T) {
db, err := SetupDB("./testdata/test.db")
defer db.Close()
+
if err != nil {
t.Fatal("Expected to be able to set up the database.")
}
+
stmt := `insert into datamap(id, name) values(1,'cock')`
_, err = db.Exec(stmt)
+
if err != nil {
t.Errorf("Cannot add record to db")
}
+
rows, err := db.Query("select name from datamap")
+
if err != nil {
t.Errorf("Cannot run select statement")
}
+
defer rows.Close()
+
for rows.Next() {
var name string
err = rows.Scan(&name)
@@ -32,14 +39,17 @@ func TestOpenSQLiteFile(t *testing.T) {
func TestDatamapGoesIntoDB(t *testing.T) {
db, err := SetupDB("./testdata/test.db")
defer db.Close()
+
if err != nil {
t.Fatal("Expected to be able to set up the database.")
}
+
opts := Options{
DBPath: "./testdata/test.db",
DMName: "First Datamap",
DMPath: "./testdata/datamap.csv",
}
+
if err := DatamapToDB(&opts); err != nil {
t.Errorf("Unable to write datamap to database file because %v.", err)
}
diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go
index 7479206..d27d386 100644
--- a/pkg/datamaps/reader.go
+++ b/pkg/datamaps/reader.go
@@ -51,6 +51,7 @@ func sheetInSlice(list []string, key string) bool {
return true
}
}
+
return false
}
@@ -58,64 +59,76 @@ func sheetInSlice(list []string, key string) bool {
// in a slice of DatamapLine structs.
func getSheetNames(dmls []DatamapLine) []string {
var sheetNames []string
+
for _, dml := range dmls {
- if sheetInSlice(sheetNames, dml.Sheet) == false {
+ if !sheetInSlice(sheetNames, dml.Sheet) {
sheetNames = append(sheetNames, dml.Sheet)
}
}
+
return sheetNames
}
//ReadDML returns a slice of DatamapLine structs.
func ReadDML(path string) ([]DatamapLine, error) {
var s []DatamapLine
+
data, err := ioutil.ReadFile(path)
+
if err != nil {
return s, fmt.Errorf("Cannot find file: %s", path)
}
+
r := csv.NewReader(strings.NewReader(string(data)))
+
for {
record, err := r.Read()
if err == io.EOF {
break
}
+
if err != nil {
return s, errors.New("Cannot read line %s")
}
+
if record[0] == "cell_key" {
// this must be the header
continue
}
+
dml := DatamapLine{
Key: strings.Trim(record[0], " "),
Sheet: strings.Trim(record[1], " "),
Cellref: strings.Trim(record[2], " ")}
s = append(s, dml)
}
+
return s, nil
}
-//ReadXLSX returns the file's data as a map,
+// ReadXLSX returns the file'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(ssheet string) FileData {
-
// open the files
data, err := xlsx.OpenFile(ssheet)
if err != nil {
log.Fatal(err)
}
+
outer := make(FileData, 1)
// get the data
for _, sheet := range data.Sheets {
inner := make(SheetData)
+
for rowLidx, row := range sheet.Rows {
for colLidx, cell := range row.Cells {
colStr, err := coords.ColIndexToAlpha(colLidx)
if err != nil {
log.Fatal(err)
}
+
ex := ExtractedCell{
Cell: cell,
Col: colStr,
@@ -124,9 +137,11 @@ func ReadXLSX(ssheet string) FileData {
cellref := fmt.Sprintf("%s%d", ex.Col, ex.Row)
inner[cellref] = ex
}
+
outer[sheet.Name] = inner
}
}
+
return outer
}
@@ -137,9 +152,11 @@ func ReadXLSX(ssheet string) FileData {
func Extract(dm string, ssheet string) ExtractedData {
xdata := ReadXLSX(ssheet)
ddata, err := ReadDML(dm)
+
if err != nil {
log.Fatal(err)
}
+
names := getSheetNames(ddata)
outer := make(ExtractedData, len(names))
inner := make(map[string]xlsx.Cell)
@@ -147,11 +164,13 @@ func Extract(dm string, ssheet string) ExtractedData {
for _, i := range ddata {
sheet := i.Sheet
cellref := i.Cellref
+
if val, ok := xdata[sheet][cellref]; ok {
inner[cellref] = *val.Cell
outer[sheet] = inner
}
}
+
return outer
}
@@ -160,13 +179,17 @@ 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))
}
+
fullpath := strings.Join([]string{path, "*.xlsx"}, "")
output, err := filepath.Glob(fullpath)
+
if err != nil {
return nil, err
}
+
if output == nil {
return nil, fmt.Errorf("cannot find any xlsx files in %s", path)
}
+
return output, nil
}
diff --git a/pkg/datamaps/reader_test.go b/pkg/datamaps/reader_test.go
index e89ca79..a0b3ffe 100644
--- a/pkg/datamaps/reader_test.go
+++ b/pkg/datamaps/reader_test.go
@@ -14,6 +14,7 @@ func TestReadDML(t *testing.T) {
{1, "Department"},
{2, "Delivery Body"},
}
+
for _, c := range cases {
if got := d[c.idx].Key; got != c.val {
t.Errorf("The test expected %s, got %s.", c.val, d[c.idx].Key)
@@ -25,6 +26,7 @@ func TestNoFileReturnsError(t *testing.T) {
// this file does not exist
_, err := ReadDML("/home/bobbins.csv")
// if we get no error, something has gone wrong
+
if err == nil {
t.Errorf("Should have thrown error %s", err)
}
@@ -32,6 +34,7 @@ func TestNoFileReturnsError(t *testing.T) {
func TestBadDMLLine(t *testing.T) {
_, err := ReadDML("/home/lemon/code/python/bcompiler-engine/tests/resources/datamap_empty_cols.csv")
+
if err == nil {
t.Errorf("No error so test failed.")
}
@@ -40,6 +43,7 @@ func TestBadDMLLine(t *testing.T) {
func TestGetSheetsFromDM(t *testing.T) {
slice, _ := ReadDML("testdata/datamap.csv")
sheetNames := getSheetNames(slice)
+
if len(sheetNames) != 15 {
t.Errorf("The test expected 14 sheets in slice, got %d.",
len(sheetNames))
@@ -56,6 +60,7 @@ func TestReadXLSX(t *testing.T) {
{"Another Sheet", "F5", "4.2"},
{"Another Sheet", "J22", "18"},
}
+
for _, c := range cases {
got := d[c.sheet][c.cellref].Value
if got != c.val {
@@ -75,6 +80,7 @@ func TestExtract(t *testing.T) {
{"Introduction", "A1", "10"},
{"Introduction", "C22", "VUNT"},
}
+
for _, c := range cases {
got := d[c.sheet][c.cellref].Value
if got != c.val {
@@ -83,6 +89,7 @@ func TestExtract(t *testing.T) {
d[c.sheet][c.cellref].Value)
}
}
+
if d["Another Sheet"]["E26"].Value != "Integer:" {
t.Errorf("Expected E26 in Another Sheet sheet to be Integer: - instead it is %s", d["Another Sheet"]["E26"].Value)
}