Add random function (#514)

This commit is contained in:
agonist
2023-11-25 00:26:03 +08:00
committed by GitHub
parent 79af2d01b1
commit 71477da66c
2 changed files with 20 additions and 8 deletions

View File

@@ -18,6 +18,7 @@ var builtinDocs = functionDocs{
"typeof": "The typeof function returns the type of arg1.", "typeof": "The typeof function returns the type of arg1.",
"len": "The len function returns length of the arg1 expression if arg1 evals to string, array or document, either returns NULL.", "len": "The len function returns length of the arg1 expression if arg1 evals to string, array or document, either returns NULL.",
"coalesce": "The coalesce function returns the first non-null argument. NULL is returned if all arguments are null.", "coalesce": "The coalesce function returns the first non-null argument. NULL is returned if all arguments are null.",
"random": "The random function returns a random number between math.MinInt64 and math.MaxInt64.",
} }
var mathDocs = functionDocs{ var mathDocs = functionDocs{

View File

@@ -3,6 +3,7 @@ package functions
import ( import (
"fmt" "fmt"
"math" "math"
"math/rand"
"github.com/genjidb/genji/document" "github.com/genjidb/genji/document"
"github.com/genjidb/genji/types" "github.com/genjidb/genji/types"
@@ -22,6 +23,7 @@ var mathFunctions = Definitions{
"asinh": asinh, "asinh": asinh,
"atan": atan, "atan": atan,
"atan2": atan2, "atan2": atan2,
"random": random,
} }
var floor = &ScalarDefinition{ var floor = &ScalarDefinition{
@@ -164,3 +166,12 @@ var atan2 = &ScalarDefinition{
return types.NewDoubleValue(res), nil return types.NewDoubleValue(res), nil
}, },
} }
var random = &ScalarDefinition{
name: "random",
arity: 0,
callFn: func(args ...types.Value) (types.Value, error) {
randomNum := rand.Int63()
return types.NewIntegerValue(randomNum), nil
},
}