This repository has been archived on 2026-10-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
aql/operator.go
T

132 lines
3.7 KiB
Go
Raw Normal View History

2023-10-12 23:50:45 +01:00
package arango
import (
"errors"
)
// Operator error.
var (
2023-10-13 07:34:18 +01:00
ErrInvalidOperator = errors.New("invalid operator")
ErrInvalidOperatorForType = errors.New("invalid operator for type")
2023-10-12 23:50:45 +01:00
)
var (
operators = map[string]string{
// Idempotent
"==": "==",
"!=": "!=",
">": ">",
">=": ">=",
"<": "<",
"<=": "<=",
"IN": "IN",
"NOT IN": "NOT IN",
"LIKE": "LIKE",
"NOT LIKE": "NOT LIKE",
2024-04-25 22:00:39 +01:00
// Compatible with Filter.Operator in github.com/annybs/go/qs
2023-10-12 23:50:45 +01:00
"eq": "==",
"neq": "!=",
"gt": ">",
"gte": ">=",
"lt": "<",
"lte": "<=",
"in": "IN",
"not in": "NOT IN",
"like": "LIKE",
"not like": "NOT LIKE",
}
2023-10-13 07:34:18 +01:00
arrayOperators = map[string]bool{"IN": true, "NOT IN": true}
boolOperators = map[string]bool{"==": true, "!=": true}
numberOperators = map[string]bool{"==": true, "!=": true, ">": true, ">=": true, "<": true, "<=": true}
stringOperators = map[string]bool{"==": true, "!=": true, ">": true, ">=": true, "<": true, "<=": true, "LIKE": true, "NOT LIKE": true}
2023-10-12 23:50:45 +01:00
)
// IsArrayOperator returns true if the given operator can be used with an array value.
func IsArrayOperator(op string) bool {
2023-10-13 07:34:18 +01:00
_, err := ParseArrayOperator(op)
return err == nil
2023-10-12 23:50:45 +01:00
}
// IsBoolOperator returns true if the given operator can be used with a Boolean value.
func IsBoolOperator(op string) bool {
2023-10-13 07:34:18 +01:00
_, err := ParseBoolOperator(op)
return err == nil
2023-10-12 23:50:45 +01:00
}
// IsNumberOperator returns true if the given operator can be used with a numeric value.
func IsNumberOperator(op string) bool {
2023-10-13 07:34:18 +01:00
_, err := ParseNumberOperator(op)
return err == nil
2023-10-12 23:50:45 +01:00
}
// IsStringOperator returns true if the given operator can be used with a string value.
func IsStringOperator(op string) bool {
2023-10-13 07:34:18 +01:00
_, err := ParseStringOperator(op)
return err == nil
2023-10-12 23:50:45 +01:00
}
2023-10-13 00:00:12 +01:00
// ParseArrayOperator returns the valid AQL operator for an array operator.
// It returns an error if the operator cannot be mapped to AQL or does not support arrays.
func ParseArrayOperator(op string) (string, error) {
2023-10-13 07:34:18 +01:00
op, err := ParseOperator(op)
if err != nil {
return op, err
2023-10-13 00:00:12 +01:00
}
2023-10-13 07:34:18 +01:00
if !arrayOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
2023-10-13 00:00:12 +01:00
}
// ParseBoolOperator returns the valid AQL operator for a Boolean operator.
// It returns an error if the operator cannot be mapped to AQL or does not support Booleans.
func ParseBoolOperator(op string) (string, error) {
2023-10-13 07:34:18 +01:00
op, err := ParseOperator(op)
if err != nil {
return op, err
2023-10-13 00:00:12 +01:00
}
2023-10-13 07:34:18 +01:00
if !boolOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
2023-10-13 00:00:12 +01:00
}
// ParseNumberOperator returns the valid AQL operator for a numeric operator.
// It returns an error if the operator cannot be mapped to AQL or does not support numbers.
func ParseNumberOperator(op string) (string, error) {
2023-10-13 07:34:18 +01:00
op, err := ParseOperator(op)
if err != nil {
return op, err
2023-10-13 00:00:12 +01:00
}
2023-10-13 07:34:18 +01:00
if !numberOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
2023-10-13 00:00:12 +01:00
}
2023-10-12 23:50:45 +01:00
// ParseOperator returns the valid AQL operator for an arbitrary operator string.
2024-04-25 22:00:39 +01:00
// This supports different inputs, such as Filter.Operator in github.com/annybs/go/qs
2023-10-12 23:50:45 +01:00
//
// If the input operator cannot be mapped to AQL, this function returns ErrInvalidOperator.
func ParseOperator(op string) (string, error) {
if operators[op] == "" {
return "", ErrInvalidOperator
}
return operators[op], nil
}
2023-10-13 00:00:12 +01:00
// ParseStringOperator returns the valid AQL operator for a string operator.
// It returns an error if the operator cannot be mapped to AQL or does not support strings.
func ParseStringOperator(op string) (string, error) {
2023-10-13 07:34:18 +01:00
op, err := ParseOperator(op)
if err != nil {
return op, err
2023-10-13 00:00:12 +01:00
}
2023-10-13 07:34:18 +01:00
if !stringOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
2023-10-13 00:00:12 +01:00
}