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 NewFromString ¶ added in v0.1.9
func NewFromString( s, betweenEntriesSeparator string, ascSymbol, descSymbol string, ) (Sort, error)
NewFromString creates a new Sort instance from a string representation of sort criteria. It supports flexible sort direction symbols:
- Both ascSymbol and descSymbol can be defined: "+name,-age"
- Only ascSymbol defined: "+name,age" (where age defaults to desc)
- Only descSymbol defined: "name,-age" (where name defaults to asc)
- Neither can be undefined (will return error)
Parameters:
- s: The input string containing sort criteria
- betweenEntriesSeparator: Character used to separate multiple sort fields
- ascSymbol: Optional symbol for ascending sort (e.g., "+"). If empty and descSymbol is defined, ascending is implicit (no prefix)
- descSymbol: Optional symbol for descending sort (e.g., "-"). If empty and ascSymbol is defined, descending is implicit (no prefix)
Returns:
- Sort: A properly formatted Sort instance ("field:asc,field:desc")
- error: Invalid input errors for empty string, empty entries, invalid parts, or when both symbols are undefined
Examples:
NewFromString("name,-age", ",", "", "-") // name:asc,age:desc NewFromString("+name,age", ",", "+", "") // name:asc,age:desc NewFromString("+name,-age", ",", "+", "-") // name:asc,age:desc
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.