1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-11-06 08:39:09 +02:00

move strings.ToUpper logic from code into core.Functions

This commit is contained in:
3timeslazy
2019-10-14 21:47:31 +03:00
parent 95698ca3fb
commit e5860934a4
6 changed files with 41 additions and 15 deletions

View File

@@ -16,7 +16,7 @@ type Compiler struct {
func New(setters ...Option) *Compiler {
c := &Compiler{}
c.NamespaceContainer = newRootNamespace()
c.funcs = make(map[string]core.Function)
c.funcs = make(core.Functions)
opts := &Options{}

View File

@@ -1,10 +1,11 @@
package compiler
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/pkg/errors"
"regexp"
"strings"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/pkg/errors"
)
var fnNameValidation = regexp.MustCompile("^[a-zA-Z]+[a-zA-Z0-9_]*(::[a-zA-Z]+[a-zA-Z0-9_]*)*$")
@@ -34,7 +35,8 @@ func (nc *NamespaceContainer) Namespace(name string) core.Namespace {
func (nc *NamespaceContainer) RegisterFunction(name string, fun core.Function) error {
nsName := nc.makeFullName(name)
_, exists := nc.funcs[nsName]
_, exists := nc.funcs.Get(nsName)
if exists {
return errors.Errorf("function already exists: %s", name)
@@ -49,13 +51,13 @@ func (nc *NamespaceContainer) RegisterFunction(name string, fun core.Function) e
return errors.Errorf("invalid function or namespace name: %s", nsName)
}
nc.funcs[strings.ToUpper(nsName)] = fun
nc.funcs.Set(nsName, fun)
return nil
}
func (nc *NamespaceContainer) RemoveFunction(name string) {
delete(nc.funcs, strings.ToUpper(nc.makeFullName(name)))
nc.funcs.Unset(nc.makeFullName(name))
}
func (nc *NamespaceContainer) RegisterFunctions(funcs core.Functions) error {

View File

@@ -24,11 +24,11 @@ type (
visitor struct {
*fql.BaseFqlParserVisitor
src string
funcs map[string]core.Function
funcs core.Functions
}
)
func newVisitor(src string, funcs map[string]core.Function) *visitor {
func newVisitor(src string, funcs core.Functions) *visitor {
return &visitor{
&fql.BaseFqlParserVisitor{},
src,
@@ -1104,7 +1104,7 @@ func (v *visitor) doVisitFunctionCallExpression(context *fql.FunctionCallExpress
name += context.Identifier().GetText()
fun, exists := v.funcs[name]
fun, exists := v.funcs.Get(name)
if !exists {
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("function: '%s'", name))

View File

@@ -3,15 +3,12 @@ package core
import (
"context"
"fmt"
"strings"
)
const MaxArgs = 65536
type (
Function = func(ctx context.Context, args ...Value) (Value, error)
Functions map[string]Function
Namespace interface {
Namespace(name string) Namespace
RegisterFunction(name string, fun Function) error
@@ -36,3 +33,29 @@ func ValidateArgs(args []Value, minimum, maximum int) error {
return nil
}
type (
// Functions is a map of functions and their names.
Functions map[string]Function
// Function is a common interface for all functions of FQL.
Function = func(ctx context.Context, args ...Value) (Value, error)
)
// Get returns the function with the given name. If the function
// does not exist it returns nil, false.
func (fns Functions) Get(name string) (Function, bool) {
fn, exists := fns[strings.ToUpper(name)]
return fn, exists
}
// Set sets the function with the given name. If the function
// with the such name already exists it will be overwritten.
func (fns Functions) Set(name string, fn Function) {
fns[strings.ToUpper(name)] = fn
}
// Unset delete the function with the given name.
func (fns Functions) Unset(name string) {
delete(fns, strings.ToUpper(name))
}

View File

@@ -2,8 +2,8 @@ package datetime
import "github.com/MontFerret/ferret/pkg/runtime/core"
func NewLib() map[string]core.Function {
return map[string]core.Function{
func NewLib() core.Functions {
return core.Functions{
"NOW": Now,
"DATE": Date,
"DATE_DAYOFWEEK": DateDayOfWeek,