Load the directory model from the layered Veracross import
This commit is contained in:
+11
-16
@@ -3,40 +3,35 @@ package data
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Source interface {
|
||||
Table(app, name string) ([]map[string]string, error)
|
||||
Table(app, name string) ([]string, []map[string]string, error)
|
||||
}
|
||||
|
||||
type Dir struct {
|
||||
Root string
|
||||
}
|
||||
|
||||
func (d Dir) Table(app, name string) ([]map[string]string, error) {
|
||||
func (d Dir) Table(app, name string) ([]string, []map[string]string, error) {
|
||||
f, err := os.Open(filepath.Join(d.Root, app, name+".csv"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
rows, err := csv.NewReader(f).ReadAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return nil, fmt.Errorf("table %s/%s has no header row", app, name)
|
||||
}
|
||||
header := rows[0]
|
||||
records := []map[string]string{}
|
||||
for _, row := range rows[1:] {
|
||||
record := map[string]string{}
|
||||
for i, column := range header {
|
||||
record[column] = row[i]
|
||||
values := make([][]interface{}, len(rows))
|
||||
for i, row := range rows {
|
||||
cells := make([]interface{}, len(row))
|
||||
for j, cell := range row {
|
||||
cells[j] = cell
|
||||
}
|
||||
records = append(records, record)
|
||||
values[i] = cells
|
||||
}
|
||||
return records, nil
|
||||
return parseTable(name, values)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user