add sort parsing

This commit is contained in:
Aneurin Barker Snook
2023-10-13 07:37:28 +01:00
parent ba919a17e9
commit 7ae2ef1d56
+35
View File
@@ -0,0 +1,35 @@
package arango
import (
"errors"
)
// Operator error.
var (
ErrInvalidSortDirection = errors.New("invalid sort direction")
)
var (
sorts = map[string]string{
// Idempotent
"ASC": "ASC",
"DESC": "DESC",
// Compatible with Sort.Direction in github.com/recipeer/go/qs
//
// Although lowercase keywords can be used in AQL, uppercase is favoured for stylistic consistency.
"asc": "ASC",
"desc": "DESC",
}
)
// ParseSortDirection returns the valid AQL operator for an arbitrary direction string.
// This supports different inputs, such as Sort.Direction in github.com/recipeer/go/qs
//
// If the input operator cannot be mapped to AQL, this function returns ErrInvalidSortDirection.
func ParseSortDirection(op string) (string, error) {
if sorts[op] == "" {
return "", ErrInvalidSortDirection
}
return sorts[op], nil
}