make operator parsing more efficient

This commit is contained in:
Aneurin Barker Snook
2023-10-13 07:34:18 +01:00
parent 3839b6d1b0
commit ba919a17e9
+41 -56
View File
@@ -7,6 +7,7 @@ import (
// Operator error. // Operator error.
var ( var (
ErrInvalidOperator = errors.New("invalid operator") ErrInvalidOperator = errors.New("invalid operator")
ErrInvalidOperatorForType = errors.New("invalid operator for type")
) )
var ( var (
@@ -36,93 +37,73 @@ var (
"not like": "NOT LIKE", "not like": "NOT LIKE",
} }
arrayOperators = []string{"IN", "NOT IN"} arrayOperators = map[string]bool{"IN": true, "NOT IN": true}
boolOperators = []string{"==", "!="} boolOperators = map[string]bool{"==": true, "!=": true}
numberOperators = []string{"==", "!=", ">", ">=", "<", "<="} numberOperators = map[string]bool{"==": true, "!=": true, ">": true, ">=": true, "<": true, "<=": true}
stringOperators = []string{"==", "!=", ">", ">=", "<", "<=", "LIKE", "NOT LIKE"} stringOperators = map[string]bool{"==": true, "!=": true, ">": true, ">=": true, "<": true, "<=": true, "LIKE": true, "NOT LIKE": true}
) )
// IsArrayOperator returns true if the given operator can be used with an array value. // IsArrayOperator returns true if the given operator can be used with an array value.
func IsArrayOperator(op string) bool { func IsArrayOperator(op string) bool {
op, _ = ParseOperator(op) _, err := ParseArrayOperator(op)
if op == "" { return err == nil
return false
}
for _, arrOp := range arrayOperators {
if arrOp == op {
return true
}
}
return false
} }
// IsBoolOperator returns true if the given operator can be used with a Boolean value. // IsBoolOperator returns true if the given operator can be used with a Boolean value.
func IsBoolOperator(op string) bool { func IsBoolOperator(op string) bool {
op, _ = ParseOperator(op) _, err := ParseBoolOperator(op)
if op == "" { return err == nil
return false
}
for _, boolOp := range boolOperators {
if boolOp == op {
return true
}
}
return false
} }
// IsNumberOperator returns true if the given operator can be used with a numeric value. // IsNumberOperator returns true if the given operator can be used with a numeric value.
func IsNumberOperator(op string) bool { func IsNumberOperator(op string) bool {
op, _ = ParseOperator(op) _, err := ParseNumberOperator(op)
if op == "" { return err == nil
return false
}
for _, numOp := range numberOperators {
if numOp == op {
return true
}
}
return false
} }
// IsStringOperator returns true if the given operator can be used with a string value. // IsStringOperator returns true if the given operator can be used with a string value.
func IsStringOperator(op string) bool { func IsStringOperator(op string) bool {
op, _ = ParseOperator(op) _, err := ParseStringOperator(op)
if op == "" { return err == nil
return false
}
for _, strOp := range stringOperators {
if strOp == op {
return true
}
}
return false
} }
// ParseArrayOperator returns the valid AQL operator for an array operator. // 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. // It returns an error if the operator cannot be mapped to AQL or does not support arrays.
func ParseArrayOperator(op string) (string, error) { func ParseArrayOperator(op string) (string, error) {
if !IsArrayOperator(op) { op, err := ParseOperator(op)
return "", ErrInvalidOperator if err != nil {
return op, err
} }
return ParseOperator(op) if !arrayOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
} }
// ParseBoolOperator returns the valid AQL operator for a Boolean operator. // 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. // It returns an error if the operator cannot be mapped to AQL or does not support Booleans.
func ParseBoolOperator(op string) (string, error) { func ParseBoolOperator(op string) (string, error) {
if !IsBoolOperator(op) { op, err := ParseOperator(op)
return "", ErrInvalidOperator if err != nil {
return op, err
} }
return ParseOperator(op) if !boolOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
} }
// ParseNumberOperator returns the valid AQL operator for a numeric operator. // 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. // It returns an error if the operator cannot be mapped to AQL or does not support numbers.
func ParseNumberOperator(op string) (string, error) { func ParseNumberOperator(op string) (string, error) {
if !IsNumberOperator(op) { op, err := ParseOperator(op)
return "", ErrInvalidOperator if err != nil {
return op, err
} }
return ParseOperator(op) if !numberOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
} }
// ParseOperator returns the valid AQL operator for an arbitrary operator string. // ParseOperator returns the valid AQL operator for an arbitrary operator string.
@@ -139,8 +120,12 @@ func ParseOperator(op string) (string, error) {
// ParseStringOperator returns the valid AQL operator for a string operator. // 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. // It returns an error if the operator cannot be mapped to AQL or does not support strings.
func ParseStringOperator(op string) (string, error) { func ParseStringOperator(op string) (string, error) {
if !IsStringOperator(op) { op, err := ParseOperator(op)
return "", ErrInvalidOperator if err != nil {
return op, err
} }
return ParseOperator(op) if !stringOperators[op] {
return op, ErrInvalidOperatorForType
}
return op, nil
} }