You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mop/filter.go

76 lines
2.4 KiB

// Copyright (c) 2013-2019 by Michael Dvorkin and contributors. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
package mop
import "strings"
// Filter gets called to sort stock quotes by one of the columns. The
// setup is rather lengthy; there should probably be more concise way
// that uses reflection and avoids hardcoding the column names.
type Filter struct {
profile *Profile // Pointer to where we store sort column and order.
}
// Returns new Filter struct.
func NewFilter(profile *Profile) *Filter {
return &Filter{
profile: profile,
}
}
// Apply builds a list of sort interface based on current sort
// order, then calls sort.Sort to do the actual job.
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,
}
result, err := filter.profile.filterExpression.Evaluate(values)
if err != nil {
// The filter isn't working, so reset to no filter.
filter.profile.Filter = ""
// Return an empty list. The next main loop cycle will
// show unfiltered.
return filteredStocks
}
truthy, ok := result.(bool)
if !ok {
// The filter isn't working, so reset to no filter.
filter.profile.Filter = ""
// Return an empty list. The next main loop cycle will
// show unfiltered.
return filteredStocks
}
if truthy {
filteredStocks = append(filteredStocks, stock)
}
}
return filteredStocks
}