3
0

Added int caster to gval functions

This commit is contained in:
Peter Grlica
2021-12-08 07:50:54 +01:00
parent eecf867090
commit 3473a26793
2 changed files with 30 additions and 2 deletions

View File

@@ -1,10 +1,12 @@
package expr
import (
"github.com/PaesslerAG/gval"
"github.com/pkg/errors"
"math"
"math/rand"
"github.com/PaesslerAG/gval"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
func NumericFunctions() []gval.Language {
@@ -21,6 +23,7 @@ func NumericFunctions() []gval.Language {
gval.Function("sum", sum),
gval.Function("average", average),
gval.Function("random", random),
gval.Function("int", toInt64),
}
}
@@ -141,3 +144,7 @@ func random(v ...float64) (out float64, err error) {
out = from + rand.Float64()*(to-from)
return
}
func toInt64(aa interface{}) (i int64) {
return cast.ToInt64(aa)
}

View File

@@ -90,3 +90,24 @@ func Example_randomWithTwoInput() {
// output:
// 1
}
func Example_castStringToInt() {
eval(`int("abc")`, nil)
// output:
// 0
}
func Example_castStringNumberToInt() {
eval(`int("42690")`, nil)
// output:
// 42690
}
func Example_castFloatToInt() {
eval(`int("42.690")`, nil)
// output:
// 0
}