operator.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package arango
  2. import (
  3. "errors"
  4. )
  5. // Operator error.
  6. var (
  7. ErrInvalidOperator = errors.New("invalid operator")
  8. )
  9. var (
  10. operators = map[string]string{
  11. // Idempotent
  12. "==": "==",
  13. "!=": "!=",
  14. ">": ">",
  15. ">=": ">=",
  16. "<": "<",
  17. "<=": "<=",
  18. "IN": "IN",
  19. "NOT IN": "NOT IN",
  20. "LIKE": "LIKE",
  21. "NOT LIKE": "NOT LIKE",
  22. // Compatible with Filter.Operator in github.com/recipeer/go/qs
  23. "eq": "==",
  24. "neq": "!=",
  25. "gt": ">",
  26. "gte": ">=",
  27. "lt": "<",
  28. "lte": "<=",
  29. "in": "IN",
  30. "not in": "NOT IN",
  31. "like": "LIKE",
  32. "not like": "NOT LIKE",
  33. }
  34. arrayOperators = []string{"IN", "NOT IN"}
  35. boolOperators = []string{"==", "!="}
  36. numberOperators = []string{"==", "!=", ">", ">=", "<", "<="}
  37. stringOperators = []string{"==", "!=", ">", ">=", "<", "<=", "LIKE", "NOT LIKE"}
  38. )
  39. // IsArrayOperator returns true if the given operator can be used with an array value.
  40. func IsArrayOperator(op string) bool {
  41. op, _ = ParseOperator(op)
  42. if op == "" {
  43. return false
  44. }
  45. for _, arrOp := range arrayOperators {
  46. if arrOp == op {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. // IsBoolOperator returns true if the given operator can be used with a Boolean value.
  53. func IsBoolOperator(op string) bool {
  54. op, _ = ParseOperator(op)
  55. if op == "" {
  56. return false
  57. }
  58. for _, boolOp := range boolOperators {
  59. if boolOp == op {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. // IsNumberOperator returns true if the given operator can be used with a numeric value.
  66. func IsNumberOperator(op string) bool {
  67. op, _ = ParseOperator(op)
  68. if op == "" {
  69. return false
  70. }
  71. for _, numOp := range numberOperators {
  72. if numOp == op {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. // IsStringOperator returns true if the given operator can be used with a string value.
  79. func IsStringOperator(op string) bool {
  80. op, _ = ParseOperator(op)
  81. if op == "" {
  82. return false
  83. }
  84. for _, strOp := range stringOperators {
  85. if strOp == op {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. // ParseOperator returns the valid AQL operator for an arbitrary operator string.
  92. // This supports different inputs, such as Filter.Operator in github.com/recipeer/go/qs
  93. //
  94. // If the input operator cannot be mapped to AQL, this function returns ErrInvalidOperator.
  95. func ParseOperator(op string) (string, error) {
  96. if operators[op] == "" {
  97. return "", ErrInvalidOperator
  98. }
  99. return operators[op], nil
  100. }