function.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2022 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. "strings"
  20. )
  21. type funcExe func(ctx api.FunctionContext, args []interface{}) (interface{}, bool)
  22. type funcVal func(ctx api.FunctionContext, args []ast.Expr) error
  23. type builtinFunc struct {
  24. fType FuncType
  25. exec funcExe
  26. val funcVal
  27. }
  28. var builtins map[string]builtinFunc
  29. func init() {
  30. builtins = make(map[string]builtinFunc)
  31. registerAggFunc()
  32. registerMathFunc()
  33. registerStrFunc()
  34. registerMiscFunc()
  35. registerColsFunc()
  36. }
  37. //var funcWithAsteriskSupportMap = map[string]string{
  38. // "collect": "",
  39. // "count": "",
  40. //}
  41. type funcExecutor struct{}
  42. func (f *funcExecutor) ValidateWithName(args []ast.Expr, name string) error {
  43. fs, ok := builtins[name]
  44. if !ok {
  45. return fmt.Errorf("validate function %s error: unknown name", name)
  46. }
  47. var eargs []ast.Expr
  48. for _, arg := range args {
  49. if t, ok := arg.(ast.Expr); ok {
  50. eargs = append(eargs, t)
  51. } else {
  52. // should never happen
  53. return fmt.Errorf("receive invalid arg %v", arg)
  54. }
  55. }
  56. // TODO pass in ctx
  57. return fs.val(nil, eargs)
  58. }
  59. func (f *funcExecutor) Validate(_ []interface{}) error {
  60. return fmt.Errorf("unknow name")
  61. }
  62. func (f *funcExecutor) Exec(_ []interface{}, _ api.FunctionContext) (interface{}, bool) {
  63. return fmt.Errorf("unknow name"), false
  64. }
  65. func (f *funcExecutor) ExecWithName(args []interface{}, ctx api.FunctionContext, name string) (interface{}, bool) {
  66. fs, ok := builtins[name]
  67. if !ok {
  68. return fmt.Errorf("unknow name"), false
  69. }
  70. return fs.exec(ctx, args)
  71. }
  72. func (f *funcExecutor) IsAggregate() bool {
  73. return false
  74. }
  75. func (f *funcExecutor) GetFuncType(name string) FuncType {
  76. fs, ok := builtins[name]
  77. if !ok {
  78. return FuncTypeUnknown
  79. }
  80. return fs.fType
  81. }
  82. var staticFuncExecutor = &funcExecutor{}
  83. type Manager struct{}
  84. // Function the name is converted to lowercase if needed during parsing
  85. func (m *Manager) Function(name string) (api.Function, error) {
  86. _, ok := builtins[name]
  87. if !ok {
  88. return nil, nil
  89. }
  90. return staticFuncExecutor, nil
  91. }
  92. func (m *Manager) HasFunctionSet(name string) bool {
  93. return name == "internal"
  94. }
  95. func (m *Manager) ConvName(n string) (string, bool) {
  96. name := strings.ToLower(n)
  97. _, ok := builtins[name]
  98. return name, ok
  99. }
  100. var m = &Manager{}
  101. func GetManager() *Manager {
  102. return m
  103. }