validator_funcs.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. )
  20. // ProduceErrInfo Index is starting from 0
  21. func ProduceErrInfo(index int, expect string) (err error) {
  22. index++
  23. err = fmt.Errorf("Expect %s type for parameter %d", expect, index)
  24. return
  25. }
  26. func ValidateLen(exp, actual int) error {
  27. if actual != exp {
  28. return fmt.Errorf("Expect %d arguments but found %d.", exp, actual)
  29. }
  30. return nil
  31. }
  32. func ValidateAtLeast(min, actual int) error {
  33. if actual < min {
  34. return fmt.Errorf("At least has %d argument but found %d.", min, actual)
  35. }
  36. return nil
  37. }
  38. // Shared validating functions
  39. func ValidateNoArg(_ api.FunctionContext, args []ast.Expr) error {
  40. return ValidateLen(0, len(args))
  41. }
  42. func ValidateOneArg(_ api.FunctionContext, args []ast.Expr) error {
  43. return ValidateLen(1, len(args))
  44. }
  45. func ValidateOneNumberArg(_ api.FunctionContext, args []ast.Expr) error {
  46. if err := ValidateLen(1, len(args)); err != nil {
  47. return err
  48. }
  49. if ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  50. return ProduceErrInfo(0, "number - float or int")
  51. }
  52. return nil
  53. }
  54. func ValidateTwoNumberArg(_ api.FunctionContext, args []ast.Expr) error {
  55. if err := ValidateLen(2, len(args)); err != nil {
  56. return err
  57. }
  58. if ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  59. return ProduceErrInfo(0, "number - float or int")
  60. }
  61. if ast.IsStringArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) {
  62. return ProduceErrInfo(1, "number - float or int")
  63. }
  64. return nil
  65. }
  66. func ValidateTwoIntArg(_ api.FunctionContext, args []ast.Expr) error {
  67. if err := ValidateLen(2, len(args)); err != nil {
  68. return err
  69. }
  70. if ast.IsFloatArg(args[0]) || ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  71. return ProduceErrInfo(0, "int")
  72. }
  73. if ast.IsFloatArg(args[1]) || ast.IsStringArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) {
  74. return ProduceErrInfo(1, "int")
  75. }
  76. return nil
  77. }
  78. func ValidateTwoStrArg(_ api.FunctionContext, args []ast.Expr) error {
  79. if err := ValidateLen(2, len(args)); err != nil {
  80. return err
  81. }
  82. for i := 0; i < 2; i++ {
  83. if ast.IsNumericArg(args[i]) || ast.IsTimeArg(args[i]) || ast.IsBooleanArg(args[i]) {
  84. return ProduceErrInfo(i, "string")
  85. }
  86. }
  87. return nil
  88. }
  89. func ValidateOneStrArg(_ api.FunctionContext, args []ast.Expr) error {
  90. if err := ValidateLen(1, len(args)); err != nil {
  91. return err
  92. }
  93. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  94. return ProduceErrInfo(0, "string")
  95. }
  96. return nil
  97. }
  98. func ValidateOneStrOneInt(_ api.FunctionContext, args []ast.Expr) error {
  99. if err := ValidateLen(2, len(args)); err != nil {
  100. return err
  101. }
  102. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  103. return ProduceErrInfo(0, "string")
  104. }
  105. if ast.IsFloatArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) || ast.IsStringArg(args[1]) {
  106. return ProduceErrInfo(1, "int")
  107. }
  108. return nil
  109. }
  110. func ValidateJsonFunc(_ api.FunctionContext, args []ast.Expr) error {
  111. l := len(args)
  112. if err := ValidateLen(2, l); err != nil {
  113. return err
  114. }
  115. if !ast.IsStringArg(args[1]) {
  116. return ProduceErrInfo(1, "string")
  117. }
  118. return nil
  119. }