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:
1
go.sum
1
go.sum
@@ -96,6 +96,7 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc h1:N3zlSgxkefUH/ecsl37RWTkESTB026kmXzNly8TuZCI=
|
||||||
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type Compiler struct {
|
|||||||
func New(setters ...Option) *Compiler {
|
func New(setters ...Option) *Compiler {
|
||||||
c := &Compiler{}
|
c := &Compiler{}
|
||||||
c.NamespaceContainer = newRootNamespace()
|
c.NamespaceContainer = newRootNamespace()
|
||||||
c.funcs = make(map[string]core.Function)
|
c.funcs = make(core.Functions)
|
||||||
|
|
||||||
opts := &Options{}
|
opts := &Options{}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package compiler
|
package compiler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"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_]*)*$")
|
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 {
|
func (nc *NamespaceContainer) RegisterFunction(name string, fun core.Function) error {
|
||||||
nsName := nc.makeFullName(name)
|
nsName := nc.makeFullName(name)
|
||||||
_, exists := nc.funcs[nsName]
|
|
||||||
|
_, exists := nc.funcs.Get(nsName)
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
return errors.Errorf("function already exists: %s", name)
|
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)
|
return errors.Errorf("invalid function or namespace name: %s", nsName)
|
||||||
}
|
}
|
||||||
|
|
||||||
nc.funcs[strings.ToUpper(nsName)] = fun
|
nc.funcs.Set(nsName, fun)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nc *NamespaceContainer) RemoveFunction(name string) {
|
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 {
|
func (nc *NamespaceContainer) RegisterFunctions(funcs core.Functions) error {
|
||||||
|
|||||||
@@ -24,11 +24,11 @@ type (
|
|||||||
visitor struct {
|
visitor struct {
|
||||||
*fql.BaseFqlParserVisitor
|
*fql.BaseFqlParserVisitor
|
||||||
src string
|
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{
|
return &visitor{
|
||||||
&fql.BaseFqlParserVisitor{},
|
&fql.BaseFqlParserVisitor{},
|
||||||
src,
|
src,
|
||||||
@@ -1104,7 +1104,7 @@ func (v *visitor) doVisitFunctionCallExpression(context *fql.FunctionCallExpress
|
|||||||
|
|
||||||
name += context.Identifier().GetText()
|
name += context.Identifier().GetText()
|
||||||
|
|
||||||
fun, exists := v.funcs[name]
|
fun, exists := v.funcs.Get(name)
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("function: '%s'", name))
|
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("function: '%s'", name))
|
||||||
|
|||||||
@@ -3,15 +3,12 @@ package core
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MaxArgs = 65536
|
const MaxArgs = 65536
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Function = func(ctx context.Context, args ...Value) (Value, error)
|
|
||||||
|
|
||||||
Functions map[string]Function
|
|
||||||
|
|
||||||
Namespace interface {
|
Namespace interface {
|
||||||
Namespace(name string) Namespace
|
Namespace(name string) Namespace
|
||||||
RegisterFunction(name string, fun Function) error
|
RegisterFunction(name string, fun Function) error
|
||||||
@@ -36,3 +33,29 @@ func ValidateArgs(args []Value, minimum, maximum int) error {
|
|||||||
|
|
||||||
return nil
|
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))
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package datetime
|
|||||||
|
|
||||||
import "github.com/MontFerret/ferret/pkg/runtime/core"
|
import "github.com/MontFerret/ferret/pkg/runtime/core"
|
||||||
|
|
||||||
func NewLib() map[string]core.Function {
|
func NewLib() core.Functions {
|
||||||
return map[string]core.Function{
|
return core.Functions{
|
||||||
"NOW": Now,
|
"NOW": Now,
|
||||||
"DATE": Date,
|
"DATE": Date,
|
||||||
"DATE_DAYOFWEEK": DateDayOfWeek,
|
"DATE_DAYOFWEEK": DateDayOfWeek,
|
||||||
|
|||||||
Reference in New Issue
Block a user