1
0

operator.go 3.7 KB

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