Documentation ¶
Overview ¶
Package customsort provides types and methods to handle the `sort` query param. It provides two types: `Sort` and `SortMap`. The `Sort` type is a string, and the `SortMap` type is a map of `fieldName:order` pairs.
SortMap provides a custom marshaler which helps Echo to properly convert the `sort` query to a `SortMap` type. In this case, it not only converts but also validates that. The downside of using it is that it can only be used with Echo's `Bind` method.
The `Sort` type is more powerful and flexible, it's framework agnostic, can be easily extended adding more formats. It's used under-the-hood by the `SortMap` type.
Index ¶
Constants ¶
const ( // Asc is the ascending order. Asc = "asc" // Desc is the descending order. Desc = "desc" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Sort ¶
type Sort string
Sort is the raw sort from the request.
func NewFromMap ¶
NewFromMap creates a new `Sort` from a map[fieldName]order. It first sorts the map by the keys, then it joins the pairs using the `,` separator.
func (Sort) ToAnyString ¶
func (s Sort) ToAnyString(desiredBetweenKVSeparator, desiredBetweenEntriesSeparator string) (string, error)
ToAnyString function receives two parameters: `desiredBetweenKVSeparator` and `desiredBetweenEntriesSeparator`, which are used to format the `sort` output. It uses `sortRegex` to match and validate `fieldName:order` pairs.
The function returns an error if no matches were found, indicating that the `sort` string format is not correct. The error message specifies the expected format -> `key:order`. If the `sort` string is formatted correctly, it joins the pairs using the `desiredBetweenEntriesSeparator`.
Finally, it replaces the `:` separator with the `desiredBetweenKVSeparator`. If the function executes successfully, it returns the formatted sort string along with a nil error value.