Use govaluate to evaluate expressions in portfolio entry

pull/149/head
Simon Roberts 3 years ago
parent 5916c49972
commit b23e65cf28

@ -12,6 +12,7 @@ import (
"time"
"unicode/utf8"
"github.com/Knetic/govaluate"
"github.com/miguelmota/cointop/pkg/asciitable"
"github.com/miguelmota/cointop/pkg/humanize"
"github.com/miguelmota/cointop/pkg/pad"
@ -414,16 +415,24 @@ func (ct *Cointop) SetPortfolioHoldings() error {
return nil
}
value := normalizeFloatString(string(b), true)
shouldDelete := value == ""
var holdings float64
if !shouldDelete {
holdings, err = strconv.ParseFloat(value, 64)
var holdings float64 = 0
input := strings.TrimSpace(string(b[:n])) // remove trailing \0s
if input != "" {
expression, err := govaluate.NewEvaluableExpression(input)
if err != nil {
return err
return nil // invalid expression - don't change anything
}
result, err := expression.Evaluate(nil)
if err != nil {
return nil // could not evaluate - don't change anything
}
var ok bool
holdings, ok = result.(float64)
if !ok {
return nil // not a float64 - don't change anything
}
}
shouldDelete := holdings == 0
idx := ct.GetPortfolioCoinIndex(coin)
if err := ct.SetPortfolioEntry(coin.Name, holdings); err != nil {

@ -2,6 +2,7 @@ module github.com/miguelmota/cointop
require (
github.com/BurntSushi/toml v0.3.1
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/anaskhan96/soup v1.1.1 // indirect
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect

@ -1,6 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=

Loading…
Cancel
Save