funcArgsValidator.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 ast
  15. import "fmt"
  16. // ProduceErrInfo Index is starting from 0
  17. func ProduceErrInfo(name string, index int, expect string) (err error) {
  18. index++
  19. err = fmt.Errorf("Expect %s type for %d parameter of function %s.", expect, index, name)
  20. return
  21. }
  22. func ValidateLen(funcName string, exp, actual int) error {
  23. if actual != exp {
  24. return fmt.Errorf("The arguments for %s should be %d.", funcName, exp)
  25. }
  26. return nil
  27. }
  28. func IsNumericArg(arg Expr) bool {
  29. if _, ok := arg.(*NumberLiteral); ok {
  30. return true
  31. } else if _, ok := arg.(*IntegerLiteral); ok {
  32. return true
  33. }
  34. return false
  35. }
  36. func IsIntegerArg(arg Expr) bool {
  37. if _, ok := arg.(*IntegerLiteral); ok {
  38. return true
  39. }
  40. return false
  41. }
  42. func IsFloatArg(arg Expr) bool {
  43. if _, ok := arg.(*NumberLiteral); ok {
  44. return true
  45. }
  46. return false
  47. }
  48. func IsBooleanArg(arg Expr) bool {
  49. switch t := arg.(type) {
  50. case *BooleanLiteral:
  51. return true
  52. case *BinaryExpr:
  53. switch t.OP {
  54. case AND, OR, EQ, NEQ, LT, LTE, GT, GTE:
  55. return true
  56. default:
  57. return false
  58. }
  59. default:
  60. return false
  61. }
  62. }
  63. func IsStringArg(arg Expr) bool {
  64. if _, ok := arg.(*StringLiteral); ok {
  65. return true
  66. }
  67. return false
  68. }
  69. func IsTimeArg(arg Expr) bool {
  70. if _, ok := arg.(*TimeLiteral); ok {
  71. return true
  72. }
  73. return false
  74. }