mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-12 11:15:14 +02:00
make core.Functions a struct
This commit is contained in:
parent
e5860934a4
commit
83dc0d2811
@ -11,13 +11,13 @@ import (
|
||||
)
|
||||
|
||||
func HTTPHelpers(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"GET": httpGet,
|
||||
})
|
||||
}
|
||||
|
||||
func Assertions(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"EXPECT": expect,
|
||||
})
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Compiler struct {
|
||||
func New(setters ...Option) *Compiler {
|
||||
c := &Compiler{}
|
||||
c.NamespaceContainer = newRootNamespace()
|
||||
c.funcs = make(core.Functions)
|
||||
c.funcs = core.NewFunctions()
|
||||
|
||||
opts := &Options{}
|
||||
|
||||
|
@ -20,7 +20,7 @@ type NamespaceContainer struct {
|
||||
|
||||
func newRootNamespace() *NamespaceContainer {
|
||||
ns := new(NamespaceContainer)
|
||||
ns.funcs = make(core.Functions)
|
||||
ns.funcs = core.NewFunctions()
|
||||
|
||||
return ns
|
||||
}
|
||||
@ -60,7 +60,7 @@ func (nc *NamespaceContainer) RemoveFunction(name string) {
|
||||
nc.funcs.Unset(nc.makeFullName(name))
|
||||
}
|
||||
|
||||
func (nc *NamespaceContainer) RegisterFunctions(funcs core.Functions) error {
|
||||
func (nc *NamespaceContainer) RegisterFunctions(funcs core.FunctionsMap) error {
|
||||
for name, fun := range funcs {
|
||||
if err := nc.RegisterFunction(name, fun); err != nil {
|
||||
return err
|
||||
@ -71,16 +71,17 @@ func (nc *NamespaceContainer) RegisterFunctions(funcs core.Functions) error {
|
||||
}
|
||||
|
||||
func (nc *NamespaceContainer) RegisteredFunctions() []string {
|
||||
res := make([]string, 0, len(nc.funcs))
|
||||
fnames := nc.funcs.Names()
|
||||
res := make([]string, 0, len(fnames))
|
||||
|
||||
// root namespace, return all functions
|
||||
if nc.name == emptyNS {
|
||||
for k := range nc.funcs {
|
||||
for _, k := range fnames {
|
||||
res = append(res, k)
|
||||
}
|
||||
} else {
|
||||
nsPrefix := nc.name + separator
|
||||
for k := range nc.funcs {
|
||||
for _, k := range fnames {
|
||||
if strings.HasPrefix(k, nsPrefix) {
|
||||
res = append(res, k)
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ type (
|
||||
Namespace interface {
|
||||
Namespace(name string) Namespace
|
||||
RegisterFunction(name string, fun Function) error
|
||||
RegisterFunctions(funs Functions) error
|
||||
RegisterFunctions(funs FunctionsMap) error
|
||||
RegisteredFunctions() []string
|
||||
RemoveFunction(name string)
|
||||
}
|
||||
@ -35,27 +35,56 @@ func ValidateArgs(args []Value, minimum, maximum int) error {
|
||||
}
|
||||
|
||||
type (
|
||||
// Functions is a map of functions and their names.
|
||||
Functions map[string]Function
|
||||
// Functions is a container for functions.
|
||||
Functions struct {
|
||||
functions FunctionsMap
|
||||
}
|
||||
|
||||
// FunctionsMap is a map of functions and their names.
|
||||
FunctionsMap map[string]Function
|
||||
|
||||
// Function is a common interface for all functions of FQL.
|
||||
Function = func(ctx context.Context, args ...Value) (Value, error)
|
||||
)
|
||||
|
||||
// NewFunctions returns new empty Functions.
|
||||
func NewFunctions() Functions {
|
||||
return Functions{
|
||||
functions: make(FunctionsMap),
|
||||
}
|
||||
}
|
||||
|
||||
// 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)]
|
||||
fn, exists := fns.functions[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
|
||||
// the preferred way to create Functions is NewFunctions.
|
||||
// But just in case, if someone creates differently
|
||||
if fns.functions == nil {
|
||||
fns.functions = make(FunctionsMap)
|
||||
}
|
||||
|
||||
fns.functions[strings.ToUpper(name)] = fn
|
||||
}
|
||||
|
||||
// Unset delete the function with the given name.
|
||||
func (fns Functions) Unset(name string) {
|
||||
delete(fns, strings.ToUpper(name))
|
||||
delete(fns.functions, strings.ToUpper(name))
|
||||
}
|
||||
|
||||
// Names returns the names of the internal functions.
|
||||
func (fns Functions) Names() []string {
|
||||
names := make([]string, 0, len(fns.functions))
|
||||
|
||||
for name := range fns.functions {
|
||||
names = append(names, name)
|
||||
}
|
||||
|
||||
return names
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"APPEND": Append,
|
||||
"FIRST": First,
|
||||
"FLATTEN": Flatten,
|
||||
|
@ -3,7 +3,7 @@ package collections
|
||||
import "github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"LENGTH": Length,
|
||||
"REVERSE": Reverse,
|
||||
})
|
||||
|
@ -2,8 +2,8 @@ package datetime
|
||||
|
||||
import "github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
|
||||
func NewLib() core.Functions {
|
||||
return core.Functions{
|
||||
func NewLib() core.FunctionsMap {
|
||||
return core.FunctionsMap{
|
||||
"NOW": Now,
|
||||
"DATE": Date,
|
||||
"DATE_DAYOFWEEK": DateDayOfWeek,
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"ATTR_GET": AttributeGet,
|
||||
"ATTR_REMOVE": AttributeRemove,
|
||||
"ATTR_SET": AttributeSet,
|
||||
|
@ -16,7 +16,7 @@ const (
|
||||
)
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"ABS": Abs,
|
||||
"ACOS": Acos,
|
||||
"ASIN": Asin,
|
||||
|
@ -3,7 +3,7 @@ package objects
|
||||
import "github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"HAS": Has,
|
||||
"KEYS": Keys,
|
||||
"KEEP_KEYS": KeepKeys,
|
||||
|
@ -3,7 +3,7 @@ package strings
|
||||
import "github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"CONCAT": Concat,
|
||||
"CONCAT_SEPARATOR": ConcatWithSeparator,
|
||||
"CONTAINS": Contains,
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"TO_BOOL": ToBool,
|
||||
"TO_INT": ToInt,
|
||||
"TO_FLOAT": ToFloat,
|
||||
|
@ -3,7 +3,7 @@ package utils
|
||||
import "github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.RegisterFunctions(core.Functions{
|
||||
return ns.RegisterFunctions(core.FunctionsMap{
|
||||
"WAIT": Wait,
|
||||
"PRINT": Print,
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user