funcValuer.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 xsql
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/api"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. "github.com/lf-edge/ekuiper/pkg/errorx"
  19. "strings"
  20. )
  21. type FunctionRegister interface {
  22. HasFunction(name string) bool
  23. Function(name string) (api.Function, error)
  24. }
  25. // ONLY use NewFunctionValuer function to initialize
  26. type FunctionValuer struct {
  27. runtime *funcRuntime
  28. }
  29. //Should only be called by stream to make sure a single instance for an operation
  30. func NewFunctionValuer(p *funcRuntime) *FunctionValuer {
  31. fv := &FunctionValuer{
  32. runtime: p,
  33. }
  34. return fv
  35. }
  36. func (*FunctionValuer) Value(string) (interface{}, bool) {
  37. return nil, false
  38. }
  39. func (*FunctionValuer) Meta(string) (interface{}, bool) {
  40. return nil, false
  41. }
  42. func (*FunctionValuer) AppendAlias(string, interface{}) bool {
  43. return false
  44. }
  45. func (fv *FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
  46. lowerName := strings.ToLower(name)
  47. switch ast.FuncFinderSingleton().FuncType(lowerName) {
  48. case ast.NotFoundFunc:
  49. nf, fctx, err := fv.runtime.Get(name)
  50. switch err {
  51. case errorx.NotFoundErr:
  52. return nil, false
  53. case nil:
  54. // do nothing, continue
  55. default:
  56. return err, false
  57. }
  58. if nf.IsAggregate() {
  59. return nil, false
  60. }
  61. logger := fctx.GetLogger()
  62. logger.Debugf("run func %s", name)
  63. return nf.Exec(args, fctx)
  64. case ast.AggFunc:
  65. return nil, false
  66. case ast.MathFunc:
  67. return mathCall(lowerName, args)
  68. case ast.ConvFunc:
  69. return convCall(lowerName, args)
  70. case ast.StrFunc:
  71. return strCall(lowerName, args)
  72. case ast.HashFunc:
  73. return hashCall(lowerName, args)
  74. case ast.JsonFunc:
  75. return jsonCall(lowerName, args)
  76. case ast.OtherFunc:
  77. return otherCall(lowerName, args)
  78. default:
  79. return nil, false
  80. }
  81. }