validator_funcs.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Shared validating functions
  33. func ValidateNoArg(_ api.FunctionContext, args []ast.Expr) error {
  34. return ValidateLen(0, len(args))
  35. }
  36. func ValidateOneArg(_ api.FunctionContext, args []ast.Expr) error {
  37. return ValidateLen(1, len(args))
  38. }
  39. func ValidateOneNumberArg(_ api.FunctionContext, args []ast.Expr) error {
  40. if err := ValidateLen(1, len(args)); err != nil {
  41. return err
  42. }
  43. if ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  44. return ProduceErrInfo(0, "number - float or int")
  45. }
  46. return nil
  47. }
  48. func ValidateTwoNumberArg(_ api.FunctionContext, args []ast.Expr) error {
  49. if err := ValidateLen(2, len(args)); err != nil {
  50. return err
  51. }
  52. if ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  53. return ProduceErrInfo(0, "number - float or int")
  54. }
  55. if ast.IsStringArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) {
  56. return ProduceErrInfo(1, "number - float or int")
  57. }
  58. return nil
  59. }
  60. func ValidateTwoIntArg(_ api.FunctionContext, args []ast.Expr) error {
  61. if err := ValidateLen(2, len(args)); err != nil {
  62. return err
  63. }
  64. if ast.IsFloatArg(args[0]) || ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  65. return ProduceErrInfo(0, "int")
  66. }
  67. if ast.IsFloatArg(args[1]) || ast.IsStringArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) {
  68. return ProduceErrInfo(1, "int")
  69. }
  70. return nil
  71. }
  72. func ValidateTwoStrArg(_ api.FunctionContext, args []ast.Expr) error {
  73. if err := ValidateLen(2, len(args)); err != nil {
  74. return err
  75. }
  76. for i := 0; i < 2; i++ {
  77. if ast.IsNumericArg(args[i]) || ast.IsTimeArg(args[i]) || ast.IsBooleanArg(args[i]) {
  78. return ProduceErrInfo(i, "string")
  79. }
  80. }
  81. return nil
  82. }
  83. func ValidateOneStrArg(_ api.FunctionContext, args []ast.Expr) error {
  84. if err := ValidateLen(1, len(args)); err != nil {
  85. return err
  86. }
  87. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  88. return ProduceErrInfo(0, "string")
  89. }
  90. return nil
  91. }
  92. func ValidateOneStrOneInt(_ api.FunctionContext, args []ast.Expr) error {
  93. if err := ValidateLen(2, len(args)); err != nil {
  94. return err
  95. }
  96. if ast.IsNumericArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  97. return ProduceErrInfo(0, "string")
  98. }
  99. if ast.IsFloatArg(args[1]) || ast.IsTimeArg(args[1]) || ast.IsBooleanArg(args[1]) || ast.IsStringArg(args[1]) {
  100. return ProduceErrInfo(1, "int")
  101. }
  102. return nil
  103. }
  104. func ValidateJsonFunc(_ api.FunctionContext, args []ast.Expr) error {
  105. l := len(args)
  106. if err := ValidateLen(2, l); err != nil {
  107. return err
  108. }
  109. if !ast.IsStringArg(args[1]) {
  110. return ProduceErrInfo(1, "string")
  111. }
  112. return nil
  113. }