refactoring csv package

This commit is contained in:
Meutel 2017-07-27 18:31:24 +02:00
parent d383baba8a
commit 909b315bea
2 changed files with 80 additions and 71 deletions

View File

@ -1,31 +1,24 @@
package main
import (
"encoding/csv"
"html/template"
"io"
"log"
"net/http"
"os"
"strconv"
"meutel.net/meutel/go-examples/tpl-ex3/records"
)
var COLUMNS []string
type FinancialData struct {
Date string
Open, High, Low, Close, Adj_Close, Volume float64
}
type TplData struct {
Columns []string
Rows []FinancialData
Rows []records.FinancialData
}
func printHTML(tpl *template.Template, data []FinancialData, out io.Writer) {
func printHTML(tpl *template.Template, cols []string, rows []records.FinancialData, out io.Writer) {
tplData := TplData{
COLUMNS,
data,
cols,
rows,
}
err := tpl.ExecuteTemplate(out, "tpl.html", tplData)
if err != nil {
@ -33,62 +26,6 @@ func printHTML(tpl *template.Template, data []FinancialData, out io.Writer) {
}
}
func readCsvLine(line []string) *FinancialData {
data := new(FinancialData)
for i, col := range line {
switch COLUMNS[i] {
case "Date":
data.Date = col
case "Open":
data.Open = toFloat(col)
case "High":
data.High = toFloat(col)
case "Low":
data.Low = toFloat(col)
case "Close":
data.Close = toFloat(col)
case "Adj Close":
data.Adj_Close = toFloat(col)
case "Volume":
data.Volume = toFloat(col)
}
}
return data
}
func toFloat(v string) float64 {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Fatalln(err)
}
return f
}
func readCsvHeader(line []string) {
for _, col := range line {
COLUMNS = append(COLUMNS, col)
}
}
func readCsv(in io.Reader) []FinancialData {
data := []FinancialData{}
csvReader := csv.NewReader(in)
for {
line, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
if len(COLUMNS) == 0 {
readCsvHeader(line)
} else {
data = append(data, *readCsvLine(line))
}
}
return data
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage go-financial <file>")
@ -102,7 +39,7 @@ func main() {
defer csv.Close()
// parse data
data := readCsv(csv)
cols, rows := records.ReadCsv(csv)
tpl, err := template.New("").Funcs(template.FuncMap{
"class": func(index int) string {
@ -116,7 +53,7 @@ func main() {
panic(err)
}
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
printHTML(tpl, data, res)
printHTML(tpl, cols, rows, res)
})
http.ListenAndServe(":9000", nil)
}

View File

@ -0,0 +1,72 @@
package records
import (
"encoding/csv"
"io"
"log"
"strconv"
)
type FinancialData struct {
Date string
Open, High, Low, Close, Adj_Close, Volume float64
}
func readCsvLine(cols []string, line []string) *FinancialData {
data := new(FinancialData)
for i, col := range line {
switch cols[i] {
case "Date":
data.Date = col
case "Open":
data.Open = toFloat(col)
case "High":
data.High = toFloat(col)
case "Low":
data.Low = toFloat(col)
case "Close":
data.Close = toFloat(col)
case "Adj Close":
data.Adj_Close = toFloat(col)
case "Volume":
data.Volume = toFloat(col)
}
}
return data
}
func toFloat(v string) float64 {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Fatalln(err)
}
return f
}
func readCsvHeader(line []string) []string {
cols := []string{}
for _, col := range line {
cols = append(cols, col)
}
return cols
}
func ReadCsv(in io.Reader) ([]string, []FinancialData) {
cols := []string{}
data := []FinancialData{}
csvReader := csv.NewReader(in)
for {
line, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
if len(cols) == 0 {
cols = readCsvHeader(line)
} else {
data = append(data, *readCsvLine(cols, line))
}
}
return cols, data
}