Added int caster to gval functions
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user