binder.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 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. "github.com/lf-edge/ekuiper/internal/binder"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/errorx"
  19. )
  20. var ( // init once and read only
  21. funcFactories []binder.FuncFactory
  22. funcFactoriesNames []string
  23. )
  24. func init() {
  25. f := binder.FactoryEntry{
  26. Name: "built-in",
  27. Factory: GetManager(),
  28. }
  29. applyFactory(f)
  30. }
  31. // Initialize Only call once when server starts
  32. func Initialize(factories []binder.FactoryEntry) error {
  33. for _, f := range factories {
  34. applyFactory(f)
  35. }
  36. return nil
  37. }
  38. func applyFactory(f binder.FactoryEntry) {
  39. if s, ok := f.Factory.(binder.FuncFactory); ok {
  40. funcFactories = append(funcFactories, s)
  41. funcFactoriesNames = append(funcFactoriesNames, f.Name)
  42. }
  43. }
  44. func Function(name string) (api.Function, error) {
  45. e := make(errorx.MultiError)
  46. for i, sf := range funcFactories {
  47. r, err := sf.Function(name)
  48. if err != nil {
  49. e[funcFactoriesNames[i]] = err
  50. }
  51. if r != nil {
  52. return r, e.GetError()
  53. }
  54. }
  55. return nil, e.GetError()
  56. }
  57. func HasFunctionSet(name string) bool {
  58. for _, sf := range funcFactories {
  59. r := sf.HasFunctionSet(name)
  60. if r {
  61. return r
  62. }
  63. }
  64. return false
  65. }
  66. type multiAggFunc interface {
  67. IsAggregateWithName(name string) bool
  68. }
  69. func IsAggFunc(funcName string) bool {
  70. f, _ := Function(funcName)
  71. if f != nil {
  72. if mf, ok := f.(multiAggFunc); ok {
  73. return mf.IsAggregateWithName(funcName)
  74. } else {
  75. return f.IsAggregate()
  76. }
  77. }
  78. return false
  79. }