mirror of
https://github.com/chaisql/chai.git
synced 2025-10-05 23:57:01 +08:00

* 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
33 lines
710 B
Go
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")
|
|
}
|
|
},
|
|
}
|