diff --git a/sort.go b/sort.go new file mode 100644 index 0000000..1ef9025 --- /dev/null +++ b/sort.go @@ -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 +}