operator.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // ParseArrayOperator returns the valid AQL operator for an array operator.
  92. // It returns an error if the operator cannot be mapped to AQL or does not support arrays.
  93. func ParseArrayOperator(op string) (string, error) {
  94. if !IsArrayOperator(op) {
  95. return "", ErrInvalidOperator
  96. }
  97. return ParseOperator(op)
  98. }
  99. // ParseBoolOperator returns the valid AQL operator for a Boolean operator.
  100. // It returns an error if the operator cannot be mapped to AQL or does not support Booleans.
  101. func ParseBoolOperator(op string) (string, error) {
  102. if !IsBoolOperator(op) {
  103. return "", ErrInvalidOperator
  104. }
  105. return ParseOperator(op)
  106. }
  107. // ParseNumberOperator returns the valid AQL operator for a numeric operator.
  108. // It returns an error if the operator cannot be mapped to AQL or does not support numbers.
  109. func ParseNumberOperator(op string) (string, error) {
  110. if !IsNumberOperator(op) {
  111. return "", ErrInvalidOperator
  112. }
  113. return ParseOperator(op)
  114. }
  115. // ParseOperator returns the valid AQL operator for an arbitrary operator string.
  116. // This supports different inputs, such as Filter.Operator in github.com/recipeer/go/qs
  117. //
  118. // If the input operator cannot be mapped to AQL, this function returns ErrInvalidOperator.
  119. func ParseOperator(op string) (string, error) {
  120. if operators[op] == "" {
  121. return "", ErrInvalidOperator
  122. }
  123. return operators[op], nil
  124. }
  125. // ParseStringOperator returns the valid AQL operator for a string operator.
  126. // It returns an error if the operator cannot be mapped to AQL or does not support strings.
  127. func ParseStringOperator(op string) (string, error) {
  128. if !IsStringOperator(op) {
  129. return "", ErrInvalidOperator
  130. }
  131. return ParseOperator(op)
  132. }