Files
chaisql/internal/expr/functions/math.go
Jean Hadrien Chabran e556fc3048 Add functions packages support (#419)
* Add packaged functions support

* Add tests for math.floor func

* Export FunctionsTable

* Extract func stuff into its own package

* Rename stuff

* Fix tests

* Move doc package to cmd/genji

* Adjust naming, typos
2021-07-17 13:07:16 +02:00

33 lines
710 B
Go

package functions
import (
"math"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/internal/stringutil"
)
// MathFunctions returns all math package functions.
func MathFunctions() Definitions {
return mathFunctions
}
var mathFunctions = Definitions{
"floor": floorFunc,
}
var floorFunc = &ScalarDefinition{
name: "floor",
arity: 1,
callFn: func(args ...document.Value) (document.Value, error) {
switch args[0].Type {
case document.DoubleValue:
return document.NewDoubleValue(math.Floor(args[0].V.(float64))), nil
case document.IntegerValue:
return args[0], nil
default:
return document.Value{}, stringutil.Errorf("floor(arg1) expects arg1 to be a number")
}
},
}