1
0
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:
3timeslazy
2019-10-15 21:06:48 +03:00
parent 55e82a9791
commit ad2ec2e817
12 changed files with 231 additions and 207 deletions

View File

@ -11,15 +11,19 @@ import (
)
func HTTPHelpers(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"GET": httpGet,
})
}),
)
}
func Assertions(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"EXPECT": expect,
})
}),
)
}
func expect(_ context.Context, args ...core.Value) (core.Value, error) {

View File

@ -60,8 +60,10 @@ func (nc *NamespaceContainer) RemoveFunction(name string) {
nc.funcs.Unset(nc.makeFullName(name))
}
func (nc *NamespaceContainer) RegisterFunctions(funcs core.FunctionsMap) error {
for name, fun := range funcs {
func (nc *NamespaceContainer) RegisterFunctions(funcs *core.Functions) error {
for _, name := range funcs.Names() {
fun, _ := funcs.Get(name)
if err := nc.RegisterFunction(name, fun); err != nil {
return err
}

View File

@ -12,7 +12,7 @@ type (
Namespace interface {
Namespace(name string) Namespace
RegisterFunction(name string, fun Function) error
RegisterFunctions(funs FunctionsMap) error
RegisterFunctions(funs *Functions) error
RegisteredFunctions() []string
RemoveFunction(name string)
}
@ -37,12 +37,9 @@ func ValidateArgs(args []Value, minimum, maximum int) error {
type (
// Functions is a container for functions.
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 = func(ctx context.Context, args ...Value) (Value, error)
)
@ -50,10 +47,22 @@ type (
// NewFunctions returns new empty Functions.
func NewFunctions() *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
// does not exist it returns nil, false.
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.
// But just in case, if someone creates differently
if fns.functions == nil {
fns.functions = make(FunctionsMap, 1)
fns.functions = make(map[string]Function, 1)
}
fns.functions[strings.ToUpper(name)] = fn

View File

@ -6,7 +6,8 @@ import (
)
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"APPEND": Append,
"FIRST": First,
"FLATTEN": Flatten,
@ -29,7 +30,7 @@ func RegisterLib(ns core.Namespace) error {
"UNION_DISTINCT": UnionDistinct,
"UNIQUE": Unique,
"UNSHIFT": Unshift,
})
}))
}
func ToUniqueArray(arr *values.Array) *values.Array {

View File

@ -3,8 +3,9 @@ package collections
import "github.com/MontFerret/ferret/pkg/runtime/core"
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"LENGTH": Length,
"REVERSE": Reverse,
})
}))
}

View File

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

View File

@ -11,7 +11,8 @@ import (
)
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"ATTR_GET": AttributeGet,
"ATTR_REMOVE": AttributeRemove,
"ATTR_SET": AttributeSet,
@ -68,7 +69,7 @@ func RegisterLib(ns core.Namespace) error {
"WAIT_NO_STYLE_ALL": WaitNoStyleAll,
"WAIT_NAVIGATION": WaitNavigation,
"XPATH": XPath,
})
}))
}
func OpenOrCastPage(ctx context.Context, value core.Value) (drivers.HTMLPage, bool, error) {

View File

@ -16,7 +16,8 @@ const (
)
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"ABS": Abs,
"ACOS": Acos,
"ASIN": Asin,
@ -50,7 +51,7 @@ func RegisterLib(ns core.Namespace) error {
"TAN": Tan,
"VARIANCE_POPULATION": PopulationVariance,
"VARIANCE_SAMPLE": SampleVariance,
})
}))
}
func toFloat(arg core.Value) float64 {

View File

@ -3,7 +3,8 @@ package objects
import "github.com/MontFerret/ferret/pkg/runtime/core"
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"HAS": Has,
"KEYS": Keys,
"KEEP_KEYS": KeepKeys,
@ -11,5 +12,5 @@ func RegisterLib(ns core.Namespace) error {
"ZIP": Zip,
"VALUES": Values,
"MERGE_RECURSIVE": MergeRecursive,
})
}))
}

View File

@ -3,7 +3,8 @@ package strings
import "github.com/MontFerret/ferret/pkg/runtime/core"
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"CONCAT": Concat,
"CONCAT_SEPARATOR": ConcatWithSeparator,
"CONTAINS": Contains,
@ -37,5 +38,5 @@ func RegisterLib(ns core.Namespace) error {
"UPPER": Upper,
"FMT": Fmt,
"UNESCAPE_HTML": UnescapeHTML,
})
}))
}

View File

@ -6,7 +6,8 @@ import (
)
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"TO_BOOL": ToBool,
"TO_INT": ToInt,
"TO_FLOAT": ToFloat,
@ -26,7 +27,7 @@ func RegisterLib(ns core.Namespace) error {
"IS_BINARY": IsBinary,
"IS_NAN": IsNaN,
"TYPENAME": TypeName,
})
}))
}
func isTypeof(value core.Value, ctype core.Type) core.Value {

View File

@ -3,8 +3,10 @@ package utils
import "github.com/MontFerret/ferret/pkg/runtime/core"
func RegisterLib(ns core.Namespace) error {
return ns.RegisterFunctions(core.FunctionsMap{
return ns.RegisterFunctions(
core.NewFunctionsFromMap(map[string]core.Function{
"WAIT": Wait,
"PRINT": Print,
})
}),
)
}