1
0
Aneurin Barker Snook 1 жил өмнө
parent
commit
7ae2ef1d56
1 өөрчлөгдсөн 35 нэмэгдсэн , 0 устгасан
  1. 35 0
      sort.go

+ 35 - 0
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
+}