First pass at changing the way we map for filtering in filter.go

Doing the right type conversion.  Works, but not debugged.
master
Adam Hayes 3 years ago
parent 22a26d2432
commit 59aa481703
  1. 76
      filter.go

@ -4,7 +4,11 @@
package mop
import "strings"
import (
"strings"
"log"
"strconv"
)
// Filter gets called to sort stock quotes by one of the columns. The
// setup is rather lengthy; there should probably be more concise way
@ -26,30 +30,60 @@ func (filter *Filter) Apply(stocks []Stock) []Stock {
var filteredStocks []Stock
for _, stock := range stocks {
var values = map[string]interface{}{
"ticker": strings.TrimSpace(stock.Ticker),
"last": m(stock.LastTrade),
"change": c(stock.Change),
"changePercent": c(stock.ChangePct),
"open": m(stock.Open),
"low": m(stock.Low),
"high": m(stock.High),
"low52": m(stock.Low52),
"high52": m(stock.High52),
"volume": m(stock.Volume),
"avgVolume": m(stock.AvgVolume),
"pe": m(stock.PeRatio),
"peX": m(stock.PeRatioX),
"dividend": m(stock.Dividend),
"yield": m(stock.Yield),
"mktCap": m(stock.MarketCap),
"mktCapX": m(stock.MarketCapX),
"advancing": stock.Advancing,
}
//var values = map[string]interface{}{
// "ticker": strings.TrimSpace(stock.Ticker),
// "last": m(stock.LastTrade),
// "change": c(stock.Change),
// "changePercent": c(stock.ChangePct),
// "open": m(stock.Open),
// "low": m(stock.Low),
// "high": m(stock.High),
// "low52": m(stock.Low52),
// "high52": m(stock.High52),
// "volume": m(stock.Volume),
// "avgVolume": m(stock.AvgVolume),
// "pe": m(stock.PeRatio),
// "peX": m(stock.PeRatioX),
// "dividend": m(stock.Dividend),
// "yield": m(stock.Yield),
// "mktCap": m(stock.MarketCap),
// "mktCapX": m(stock.MarketCapX),
// "advancing": stock.Advancing,
//}
var values = make(map[string]interface{})
//values["pe"] = 8;
var err error
values["ticker"] = strings.TrimSpace(stock.Ticker)
values["last"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.LastTrade),"$",""),64)
values["change"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.Change),"$",""),64)
values["changePercent"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.ChangePct),"$",""),64)
values["open"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.Open),"$",""),64)
values["low"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.Low),"$",""),64)
values["high"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.High),"$",""),64)
values["low52"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.Low52),"$",""),64)
values["high52"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.High52),"$",""),64)
values["volume"],err = strconv.ParseFloat(stock.Volume,64)
values["avgVolume"],err = strconv.ParseFloat(stock.AvgVolume,64)
values["pe"],err = strconv.ParseFloat(stock.PeRatio,64)
values["peX"],err = strconv.ParseFloat(stock.PeRatioX,64)
values["dividend"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.Dividend),"$",""),64)
values["yield"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.Yield),"$",""),64)
values["mktCap"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.MarketCap),"$",""),64)
values["mktCapX"],err = strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(stock.MarketCapX),"$",""),64)
values["advancing"] = stock.Advancing
log.Println(stock.Dividend)
log.Println("m9: ",m("$9"))
log.Printf("type m($9) is %T\n",m("$9"))
log.Println("dividend:",stock.Dividend,m(stock.Dividend))
log.Printf("dividend is %T\n",stock.Dividend)
log.Printf("m(dividend) is %T\n",m(stock.Dividend))
log.Printf("values[pe] is type %T\n",values["pe"])
result, err := filter.profile.filterExpression.Evaluate(values)
if err != nil {
log.Println("In filter.go, err:",err)
// The filter isn't working, so reset to no filter.
filter.profile.Filter = ""
// Return an empty list. The next main loop cycle will

Loading…
Cancel
Save