sort.go 840 B

1234567891011121314151617181920212223242526272829303132333435
  1. package arango
  2. import (
  3. "errors"
  4. )
  5. // Operator error.
  6. var (
  7. ErrInvalidSortDirection = errors.New("invalid sort direction")
  8. )
  9. var (
  10. sorts = map[string]string{
  11. // Idempotent
  12. "ASC": "ASC",
  13. "DESC": "DESC",
  14. // Compatible with Sort.Direction in github.com/annybs/go/qs
  15. //
  16. // Although lowercase keywords can be used in AQL, uppercase is favoured for stylistic consistency.
  17. "asc": "ASC",
  18. "desc": "DESC",
  19. }
  20. )
  21. // ParseSortDirection returns the valid AQL operator for an arbitrary direction string.
  22. // This supports different inputs, such as Sort.Direction in github.com/annybs/go/qs
  23. //
  24. // If the input operator cannot be mapped to AQL, this function returns ErrInvalidSortDirection.
  25. func ParseSortDirection(op string) (string, error) {
  26. if sorts[op] == "" {
  27. return "", ErrInvalidSortDirection
  28. }
  29. return sorts[op], nil
  30. }