binder.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. func ConvName(name string) (string, bool) {
  67. for _, sf := range funcFactories {
  68. r, ok := sf.ConvName(name)
  69. if ok {
  70. return r, ok
  71. }
  72. }
  73. return name, false
  74. }
  75. type multiAggFunc interface {
  76. IsAggregateWithName(name string) bool
  77. }
  78. func IsAggFunc(funcName string) bool {
  79. f, _ := Function(funcName)
  80. if f != nil {
  81. if mf, ok := f.(multiAggFunc); ok {
  82. return mf.IsAggregateWithName(funcName)
  83. } else {
  84. return f.IsAggregate()
  85. }
  86. }
  87. return false
  88. }