func_invoker.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2021-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 xsql
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/binder/function"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. )
  21. func validateFuncs(funcName string, args []ast.Expr) error {
  22. f, err := function.Function(funcName)
  23. if f != nil {
  24. var targs []interface{}
  25. for _, arg := range args {
  26. targs = append(targs, arg)
  27. }
  28. if mf, ok := f.(MultiFunc); ok {
  29. return mf.ValidateWithName(args, funcName)
  30. } else {
  31. return f.Validate(targs)
  32. }
  33. } else {
  34. if err != nil {
  35. return err
  36. } else {
  37. return fmt.Errorf("function %s not found", funcName)
  38. }
  39. }
  40. }
  41. func ExecFunc(funcName string, f api.Function, args []interface{}, fctx api.FunctionContext) (interface{}, bool) {
  42. if mf, ok := f.(MultiFunc); ok {
  43. return mf.ExecWithName(args, fctx, funcName)
  44. } else {
  45. return f.Exec(args, fctx)
  46. }
  47. }
  48. // MultiFunc hack for builtin functions that works for multiple functions
  49. type MultiFunc interface {
  50. ValidateWithName(args []ast.Expr, name string) error
  51. ExecWithName(args []interface{}, ctx api.FunctionContext, name string) (interface{}, bool)
  52. }