mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-03 00:46:51 +02:00
build core.Functions with map
This commit is contained in:
@ -11,15 +11,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HTTPHelpers(ns core.Namespace) error {
|
func HTTPHelpers(ns core.Namespace) error {
|
||||||
return ns.RegisterFunctions(core.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"GET": httpGet,
|
"GET": httpGet,
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Assertions(ns core.Namespace) error {
|
func Assertions(ns core.Namespace) error {
|
||||||
return ns.RegisterFunctions(core.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"EXPECT": expect,
|
"EXPECT": expect,
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func expect(_ context.Context, args ...core.Value) (core.Value, error) {
|
func expect(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||||
|
@ -60,8 +60,10 @@ func (nc *NamespaceContainer) RemoveFunction(name string) {
|
|||||||
nc.funcs.Unset(nc.makeFullName(name))
|
nc.funcs.Unset(nc.makeFullName(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nc *NamespaceContainer) RegisterFunctions(funcs core.FunctionsMap) error {
|
func (nc *NamespaceContainer) RegisterFunctions(funcs *core.Functions) error {
|
||||||
for name, fun := range funcs {
|
for _, name := range funcs.Names() {
|
||||||
|
fun, _ := funcs.Get(name)
|
||||||
|
|
||||||
if err := nc.RegisterFunction(name, fun); err != nil {
|
if err := nc.RegisterFunction(name, fun); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -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 FunctionsMap) error
|
RegisterFunctions(funs *Functions) error
|
||||||
RegisteredFunctions() []string
|
RegisteredFunctions() []string
|
||||||
RemoveFunction(name string)
|
RemoveFunction(name string)
|
||||||
}
|
}
|
||||||
@ -37,12 +37,9 @@ func ValidateArgs(args []Value, minimum, maximum int) error {
|
|||||||
type (
|
type (
|
||||||
// Functions is a container for functions.
|
// Functions is a container for functions.
|
||||||
Functions struct {
|
Functions struct {
|
||||||
functions FunctionsMap
|
functions map[string]Function
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
)
|
)
|
||||||
@ -50,10 +47,22 @@ type (
|
|||||||
// NewFunctions returns new empty Functions.
|
// NewFunctions returns new empty Functions.
|
||||||
func NewFunctions() *Functions {
|
func NewFunctions() *Functions {
|
||||||
return &Functions{
|
return &Functions{
|
||||||
functions: make(FunctionsMap),
|
functions: make(map[string]Function),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFunctionsFromMap creates new Functions from map, where
|
||||||
|
// key is the name of the function and value is the function.
|
||||||
|
func NewFunctionsFromMap(funcs map[string]Function) *Functions {
|
||||||
|
fns := NewFunctions()
|
||||||
|
|
||||||
|
for name, fn := range funcs {
|
||||||
|
fns.Set(name, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fns
|
||||||
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
@ -67,7 +76,7 @@ func (fns *Functions) Set(name string, fn Function) {
|
|||||||
// the preferred way to create Functions is NewFunctions.
|
// the preferred way to create Functions is NewFunctions.
|
||||||
// But just in case, if someone creates differently
|
// But just in case, if someone creates differently
|
||||||
if fns.functions == nil {
|
if fns.functions == nil {
|
||||||
fns.functions = make(FunctionsMap, 1)
|
fns.functions = make(map[string]Function, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fns.functions[strings.ToUpper(name)] = fn
|
fns.functions[strings.ToUpper(name)] = fn
|
||||||
|
@ -6,7 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RegisterLib(ns core.Namespace) error {
|
func RegisterLib(ns core.Namespace) error {
|
||||||
return ns.RegisterFunctions(core.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"APPEND": Append,
|
"APPEND": Append,
|
||||||
"FIRST": First,
|
"FIRST": First,
|
||||||
"FLATTEN": Flatten,
|
"FLATTEN": Flatten,
|
||||||
@ -29,7 +30,7 @@ func RegisterLib(ns core.Namespace) error {
|
|||||||
"UNION_DISTINCT": UnionDistinct,
|
"UNION_DISTINCT": UnionDistinct,
|
||||||
"UNIQUE": Unique,
|
"UNIQUE": Unique,
|
||||||
"UNSHIFT": Unshift,
|
"UNSHIFT": Unshift,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToUniqueArray(arr *values.Array) *values.Array {
|
func ToUniqueArray(arr *values.Array) *values.Array {
|
||||||
|
@ -3,8 +3,9 @@ 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.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"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.FunctionsMap {
|
func NewLib() map[string]core.Function {
|
||||||
return core.FunctionsMap{
|
return map[string]core.Function{
|
||||||
"NOW": Now,
|
"NOW": Now,
|
||||||
"DATE": Date,
|
"DATE": Date,
|
||||||
"DATE_DAYOFWEEK": DateDayOfWeek,
|
"DATE_DAYOFWEEK": DateDayOfWeek,
|
||||||
|
@ -11,7 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RegisterLib(ns core.Namespace) error {
|
func RegisterLib(ns core.Namespace) error {
|
||||||
return ns.RegisterFunctions(core.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"ATTR_GET": AttributeGet,
|
"ATTR_GET": AttributeGet,
|
||||||
"ATTR_REMOVE": AttributeRemove,
|
"ATTR_REMOVE": AttributeRemove,
|
||||||
"ATTR_SET": AttributeSet,
|
"ATTR_SET": AttributeSet,
|
||||||
@ -68,7 +69,7 @@ func RegisterLib(ns core.Namespace) error {
|
|||||||
"WAIT_NO_STYLE_ALL": WaitNoStyleAll,
|
"WAIT_NO_STYLE_ALL": WaitNoStyleAll,
|
||||||
"WAIT_NAVIGATION": WaitNavigation,
|
"WAIT_NAVIGATION": WaitNavigation,
|
||||||
"XPATH": XPath,
|
"XPATH": XPath,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenOrCastPage(ctx context.Context, value core.Value) (drivers.HTMLPage, bool, error) {
|
func OpenOrCastPage(ctx context.Context, value core.Value) (drivers.HTMLPage, bool, error) {
|
||||||
|
@ -16,7 +16,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RegisterLib(ns core.Namespace) error {
|
func RegisterLib(ns core.Namespace) error {
|
||||||
return ns.RegisterFunctions(core.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"ABS": Abs,
|
"ABS": Abs,
|
||||||
"ACOS": Acos,
|
"ACOS": Acos,
|
||||||
"ASIN": Asin,
|
"ASIN": Asin,
|
||||||
@ -50,7 +51,7 @@ func RegisterLib(ns core.Namespace) error {
|
|||||||
"TAN": Tan,
|
"TAN": Tan,
|
||||||
"VARIANCE_POPULATION": PopulationVariance,
|
"VARIANCE_POPULATION": PopulationVariance,
|
||||||
"VARIANCE_SAMPLE": SampleVariance,
|
"VARIANCE_SAMPLE": SampleVariance,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func toFloat(arg core.Value) float64 {
|
func toFloat(arg core.Value) float64 {
|
||||||
|
@ -3,7 +3,8 @@ 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.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"HAS": Has,
|
"HAS": Has,
|
||||||
"KEYS": Keys,
|
"KEYS": Keys,
|
||||||
"KEEP_KEYS": KeepKeys,
|
"KEEP_KEYS": KeepKeys,
|
||||||
@ -11,5 +12,5 @@ func RegisterLib(ns core.Namespace) error {
|
|||||||
"ZIP": Zip,
|
"ZIP": Zip,
|
||||||
"VALUES": Values,
|
"VALUES": Values,
|
||||||
"MERGE_RECURSIVE": MergeRecursive,
|
"MERGE_RECURSIVE": MergeRecursive,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,8 @@ 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.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"CONCAT": Concat,
|
"CONCAT": Concat,
|
||||||
"CONCAT_SEPARATOR": ConcatWithSeparator,
|
"CONCAT_SEPARATOR": ConcatWithSeparator,
|
||||||
"CONTAINS": Contains,
|
"CONTAINS": Contains,
|
||||||
@ -37,5 +38,5 @@ func RegisterLib(ns core.Namespace) error {
|
|||||||
"UPPER": Upper,
|
"UPPER": Upper,
|
||||||
"FMT": Fmt,
|
"FMT": Fmt,
|
||||||
"UNESCAPE_HTML": UnescapeHTML,
|
"UNESCAPE_HTML": UnescapeHTML,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RegisterLib(ns core.Namespace) error {
|
func RegisterLib(ns core.Namespace) error {
|
||||||
return ns.RegisterFunctions(core.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"TO_BOOL": ToBool,
|
"TO_BOOL": ToBool,
|
||||||
"TO_INT": ToInt,
|
"TO_INT": ToInt,
|
||||||
"TO_FLOAT": ToFloat,
|
"TO_FLOAT": ToFloat,
|
||||||
@ -26,7 +27,7 @@ func RegisterLib(ns core.Namespace) error {
|
|||||||
"IS_BINARY": IsBinary,
|
"IS_BINARY": IsBinary,
|
||||||
"IS_NAN": IsNaN,
|
"IS_NAN": IsNaN,
|
||||||
"TYPENAME": TypeName,
|
"TYPENAME": TypeName,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func isTypeof(value core.Value, ctype core.Type) core.Value {
|
func isTypeof(value core.Value, ctype core.Type) core.Value {
|
||||||
|
@ -3,8 +3,10 @@ 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.FunctionsMap{
|
return ns.RegisterFunctions(
|
||||||
|
core.NewFunctionsFromMap(map[string]core.Function{
|
||||||
"WAIT": Wait,
|
"WAIT": Wait,
|
||||||
"PRINT": Print,
|
"PRINT": Print,
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user