static_executor.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. type funcExecutor struct{}
  21. func (f *funcExecutor) ValidateWithName(args []ast.Expr, name string) error {
  22. fs, ok := builtins[name]
  23. if !ok {
  24. return fmt.Errorf("validate function %s error: unknown name", name)
  25. }
  26. var eargs []ast.Expr
  27. for _, arg := range args {
  28. if t, ok := arg.(ast.Expr); ok {
  29. eargs = append(eargs, t)
  30. } else {
  31. // should never happen
  32. return fmt.Errorf("receive invalid arg %v", arg)
  33. }
  34. }
  35. // TODO pass in ctx
  36. return fs.val(nil, eargs)
  37. }
  38. func (f *funcExecutor) Validate(_ []interface{}) error {
  39. return fmt.Errorf("unknow name")
  40. }
  41. func (f *funcExecutor) Exec(_ []interface{}, _ api.FunctionContext) (interface{}, bool) {
  42. return fmt.Errorf("unknow name"), false
  43. }
  44. func (f *funcExecutor) ExecWithName(args []interface{}, ctx api.FunctionContext, name string) (interface{}, bool) {
  45. fs, ok := builtins[name]
  46. if !ok {
  47. return fmt.Errorf("unknow name"), false
  48. }
  49. if fs.check != nil {
  50. r, skipExec := fs.check(args)
  51. if skipExec {
  52. return r, true
  53. }
  54. }
  55. return fs.exec(ctx, args)
  56. }
  57. func (f *funcExecutor) IsAggregate() bool {
  58. return false
  59. }
  60. func (f *funcExecutor) GetFuncType(name string) ast.FuncType {
  61. fs, ok := builtins[name]
  62. if !ok {
  63. return ast.FuncTypeUnknown
  64. }
  65. return fs.fType
  66. }
  67. var staticFuncExecutor = &funcExecutor{}