Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit 08e81ca

Browse files
committed
Add subpackage for expression string eval
1 parent 1f0f6d3 commit 08e81ca

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

cointop/portfolio.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"time"
1313
"unicode/utf8"
1414

15-
"github.com/Knetic/govaluate"
1615
"github.com/miguelmota/cointop/pkg/asciitable"
16+
"github.com/miguelmota/cointop/pkg/eval"
1717
"github.com/miguelmota/cointop/pkg/humanize"
1818
"github.com/miguelmota/cointop/pkg/pad"
1919
"github.com/miguelmota/cointop/pkg/table"
@@ -435,22 +435,11 @@ func (ct *Cointop) SetPortfolioHoldings() error {
435435
return nil
436436
}
437437

438-
var holdings float64 = 0
439-
input := strings.TrimSpace(string(b[:n])) // remove trailing \0s
440-
if input != "" {
441-
expression, err := govaluate.NewEvaluableExpression(input)
442-
if err != nil {
443-
return nil // invalid expression - don't change anything
444-
}
445-
result, err := expression.Evaluate(nil)
446-
if err != nil {
447-
return nil // could not evaluate - don't change anything
448-
}
449-
var ok bool
450-
holdings, ok = result.(float64)
451-
if !ok {
452-
return nil // not a float64 - don't change anything
453-
}
438+
input := string(b[:n])
439+
holdings, err := eval.EvaluateExpressionToFloat64(input)
440+
if err != nil {
441+
// leave value as is if expression can't be evaluated
442+
return nil
454443
}
455444
shouldDelete := holdings == 0
456445

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/miguelmota/cointop
22

33
require (
44
github.com/BurntSushi/toml v0.3.1
5-
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
5+
github.com/Knetic/govaluate v3.0.0+incompatible
66
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
77
github.com/anaskhan96/soup v1.1.1 // indirect
88
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect

pkg/eval/eval.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package eval
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
"github.com/Knetic/govaluate"
8+
)
9+
10+
// EvaluateExpression ...
11+
func EvaluateExpressionToFloat64(expressionInput string) (float64, error) {
12+
expressionInput = strings.TrimSpace(expressionInput) // remove trailing \0s
13+
if expressionInput == "" {
14+
return 0, nil
15+
}
16+
expression, err := govaluate.NewEvaluableExpression(expressionInput)
17+
if err != nil {
18+
return 0, err
19+
}
20+
result, err := expression.Evaluate(nil)
21+
if err != nil {
22+
return 0, err
23+
}
24+
f64, ok := result.(float64)
25+
if !ok {
26+
return 0, errors.New("could not type assert float64")
27+
}
28+
return f64, nil
29+
}

0 commit comments

Comments
 (0)