func-bolerplate.bash: generate Go source for a new functions module

This commit is contained in:
Celestino Amoroso 2024-05-01 07:13:52 +02:00
parent 288e93d708
commit c0c2ab8b4e

71
tools/func-bolerplate.bash Executable file
View File

@ -0,0 +1,71 @@
#!/usr/bin/env bash
if [ $# -lt 2 ]; then
echo >&2 "Usage: $(basename "${0}") <module-name> <func-name>..."
exit 1
fi
MODULE_NAME=${1,,}
GO_FILE="func-${MODULE_NAME//[-.]/_}.go"
shift
FUNC_LIST=
i=0
for name; do
if [ ${i} -gt 0 ]; then
if [ $((i+1)) -eq $# ]; then
FUNC_LIST+=" and "
else
FUNC_LIST+=", "
fi
fi
FUNC_LIST+="${name}()"
((i++))
done
cat > "${GO_FILE}" <<EOF
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// func-${MODULE_NAME}.go
package expr
import (
)
// --- Start of function definitions
$(
for name; do
cat <<IEOF
func ${name}Func(ctx ExprContext, name string, args []any) (result any, err error) {
return
}
IEOF
done
)
// --- End of function definitions
// Import above functions in the context
func Import${MODULE_NAME^}Funcs(ctx ExprContext) {
$(
for name; do
cat <<IEOF
ctx.RegisterFunc("${name}", &simpleFunctor{f: ${name}Func}, 1, -1)
IEOF
done
)
}
// Register the import function in the import-register.
// That will allow to import all function of this module by the "builtin" operator."
func init() {
registerImport("${MODULE_NAME}", Import${name}Funcs, "The \"${MODULE_NAME}\" module implements the ${FUNC_LIST} function(s)")
}
EOF