customsort

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 4 Imported by: 4

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

View Source
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

func NewFromMap(m map[string]string) Sort

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) IsValid

func (s Sort) IsValid() bool

IsValid checks if the `s` is formatted correctly.

func (Sort) String

func (s Sort) String() string

String is the Stringer interface implementation.

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.

func (Sort) ToMap

func (s Sort) ToMap() (map[string]string, error)

ToMap converts and validates `s` to a map[fieldName]order.

type SortMap

type SortMap map[string]string

SortMap created to satisfy the Echo's `BindUnmarshaler` interface. Powered by the `Sort` type.

func (*SortMap) ToSort

func (sM *SortMap) ToSort() Sort

ToSort converts a SortMap to a raw `Sort` type.

func (*SortMap) UnmarshalParam

func (sM *SortMap) UnmarshalParam(src string) error

UnmarshalParam is the `BindUnmarshaler` implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL