expr/func-base.go

60 lines
1.2 KiB
Go
Raw Normal View History

2024-04-20 06:52:33 +02:00
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// func-builtins.go
package expr
import (
2024-04-20 07:29:42 +02:00
"math"
"strconv"
2024-04-20 06:52:33 +02:00
)
func isNilFunc(ctx ExprContext, name string, args []any) (result any, err error) {
if len(args) == 1 {
result = args[0] == nil
} else {
2024-04-20 07:29:42 +02:00
err = errOneParam(name)
}
return
}
func intFunc(ctx ExprContext, name string, args []any) (result any, err error) {
if len(args) == 1 {
switch v := args[0].(type) {
case int64:
result = v
case float64:
result = int64(math.Trunc(v))
case bool:
if v {
result = int64(1)
} else {
result = int64(0)
}
case string:
var i int
if i, err = strconv.Atoi(v); err == nil {
result = int64(i)
}
default:
err = errCantConvert(name, v, "int")
}
} else {
err = errOneParam(name)
2024-04-20 06:52:33 +02:00
}
return
}
func iteratorFunc(ctx ExprContext, name string, args []any) (result any, err error) {
return
}
2024-04-20 06:52:33 +02:00
func ImportBuiltinsFuncs(ctx ExprContext) {
ctx.RegisterFunc("isNil", &simpleFunctor{f: isNilFunc}, 1, -1)
2024-04-20 07:29:42 +02:00
ctx.RegisterFunc("int", &simpleFunctor{f: intFunc}, 1, -1)
2024-04-20 06:52:33 +02:00
}
func init() {
registerImport("base", ImportBuiltinsFuncs, "Base expression tools like isNil(), int(), etc.")
2024-04-20 06:52:33 +02:00
}