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