funcValuer.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package xsql
  2. import (
  3. "github.com/emqx/kuiper/pkg/api"
  4. "github.com/emqx/kuiper/pkg/ast"
  5. "github.com/emqx/kuiper/pkg/errorx"
  6. "strings"
  7. )
  8. type FunctionRegister interface {
  9. HasFunction(name string) bool
  10. Function(name string) (api.Function, error)
  11. }
  12. // ONLY use NewFunctionValuer function to initialize
  13. type FunctionValuer struct {
  14. runtime *funcRuntime
  15. }
  16. //Should only be called by stream to make sure a single instance for an operation
  17. func NewFunctionValuer(p *funcRuntime) *FunctionValuer {
  18. fv := &FunctionValuer{
  19. runtime: p,
  20. }
  21. return fv
  22. }
  23. func (*FunctionValuer) Value(string) (interface{}, bool) {
  24. return nil, false
  25. }
  26. func (*FunctionValuer) Meta(string) (interface{}, bool) {
  27. return nil, false
  28. }
  29. func (*FunctionValuer) AppendAlias(string, interface{}) bool {
  30. return false
  31. }
  32. func (fv *FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
  33. lowerName := strings.ToLower(name)
  34. switch ast.FuncFinderSingleton().FuncType(lowerName) {
  35. case ast.NotFoundFunc:
  36. nf, fctx, err := fv.runtime.Get(name)
  37. switch err {
  38. case errorx.NotFoundErr:
  39. return nil, false
  40. case nil:
  41. // do nothing, continue
  42. default:
  43. return err, false
  44. }
  45. if nf.IsAggregate() {
  46. return nil, false
  47. }
  48. logger := fctx.GetLogger()
  49. logger.Debugf("run func %s", name)
  50. return nf.Exec(args, fctx)
  51. case ast.AggFunc:
  52. return nil, false
  53. case ast.MathFunc:
  54. return mathCall(lowerName, args)
  55. case ast.ConvFunc:
  56. return convCall(lowerName, args)
  57. case ast.StrFunc:
  58. return strCall(lowerName, args)
  59. case ast.HashFunc:
  60. return hashCall(lowerName, args)
  61. case ast.JsonFunc:
  62. return jsonCall(lowerName, args)
  63. case ast.OtherFunc:
  64. return otherCall(lowerName, args)
  65. default:
  66. return nil, false
  67. }
  68. }