From 7ae2ef1d5618188f10d0d522c2aa91cbcc1e4d69 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Fri, 13 Oct 2023 07:37:28 +0100 Subject: [PATCH] add sort parsing --- sort.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sort.go 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 +}