util

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// OrderASC defines the ascending order keyword
	OrderASC = "asc"
	// OrderDESC defines the descending order keyword
	OrderDESC = "desc"
)
View Source
var (
	// DefaultPageSize is the number NewPage
	DefaultPageSize = 50
	// DefaultMaxPageSize is the max size a page can be by default, set to -1 to
	// ignore maximum sizes
	DefaultMaxPageSize = 100
)

Functions

func EmptyOkHandler

func EmptyOkHandler(w http.ResponseWriter, r *http.Request)

EmptyOkHandler is an empty 200 response, often used for OPTIONS requests that responds with headers set in addCorsHeaders

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler is a basic "hey I'm fine" for load balancers & co TODO - add Database connection & proper configuration checks here for more accurate health reporting

func NotFoundHandler

func NotFoundHandler(w http.ResponseWriter, r *http.Request)

NotFoundHandler is a JSON 404 response

func ReqParamBool

func ReqParamBool(r *http.Request, key string, def bool) bool

ReqParamBool pulls a boolean parameter from a request form value

func ReqParamInt

func ReqParamInt(r *http.Request, key string, def int) int

ReqParamInt extracts an integer parameter from a request form value

func RespondWithDispatchTypeError added in v0.10.0

func RespondWithDispatchTypeError(w http.ResponseWriter, got interface{})

RespondWithDispatchTypeError writes an error describing a type mismatch error from using dispatch

func RespondWithError

func RespondWithError(w http.ResponseWriter, err error)

RespondWithError writes the error, with meaningful text, to the http response

func WriteErrResponse

func WriteErrResponse(w http.ResponseWriter, code int, err error) error

WriteErrResponse writes a JSON error response message & HTTP status

func WriteMessageResponse

func WriteMessageResponse(w http.ResponseWriter, message string, data interface{}) error

WriteMessageResponse includes a message with a data response

func WritePageResponse

func WritePageResponse(w http.ResponseWriter, data interface{}, r *http.Request, p Page) error

WritePageResponse wraps response data and pagination information in an envelope and writes it

func WriteResponse

func WriteResponse(w http.ResponseWriter, data interface{}) error

WriteResponse wraps response data in an envelope & writes it

Types

type APIError

type APIError struct {
	Code    int
	Message string
}

APIError is an error that specifies its http status code

func NewAPIError

func NewAPIError(code int, message string) *APIError

NewAPIError returns a new APIError

func (*APIError) Error

func (err *APIError) Error() string

Error renders the APIError as a string

type Meta added in v0.10.0

type Meta struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
	Error   string `json:"error,omitempty"`
}

Meta is the JSON API response meta object wrapper

type Order

type Order struct {
	Key       string
	Direction string
}

Order represents ordering information for a single key

func NewOrder

func NewOrder(key, orderDirection string) Order

NewOrder constructs a basic order struct

func (Order) String

func (o Order) String() string

String implements the stringer interface for Order

type OrderBy

type OrderBy []Order

OrderBy represents ordering information

func NewOrderByFromString

func NewOrderByFromString(orderBy string, validKeys []string) OrderBy

NewOrderByFromString converts a commaa delimited string to an OrderBy struct

func OrderByFromRequest

func OrderByFromRequest(r *http.Request) OrderBy

OrderByFromRequest extracts orderBy params from an http request

func OrderByFromRequestWithKeys

func OrderByFromRequestWithKeys(r *http.Request, validKeys []string) OrderBy

OrderByFromRequestWithKeys extracts orderBy params from an http request and only takes the specified keys

func (OrderBy) SetQueryParams

func (o OrderBy) SetQueryParams(u *url.URL) *url.URL

SetQueryParams adds order by info to a url as query parameters

func (OrderBy) String

func (o OrderBy) String() string

String implements the stringer interface for OrderBy

type Page

type Page struct {
	Number      int    `json:"page,omitempty"`
	Size        int    `json:"pageSize,omitempty"`
	ResultCount int    `json:"resultCount,omitempty"`
	NextURL     string `json:"nextUrl"`
	PrevURL     string `json:"prevUrl"`
}

Page represents pagination information

func NewPage

func NewPage(number, size int) Page

NewPage constructs a basic page struct, setting sensible defaults

func NewPageFromOffsetAndLimit

func NewPageFromOffsetAndLimit(offset, limit int) Page

NewPageFromOffsetAndLimit converts a offset and Limit to a Page struct

func PageFromRequest

func PageFromRequest(r *http.Request) Page

PageFromRequest extracts pagination params from an http request

func (Page) Limit

func (p Page) Limit() int

Limit is a convenience accessor for page size

func (Page) Next

func (p Page) Next() Page

Next returns a page with the number advanced by one

func (Page) NextPageExists

func (p Page) NextPageExists() bool

NextPageExists returns false if the next page.ResultCount is a postive number and the starting offset of the next page exceeds page.ResultCount

func (Page) Offset

func (p Page) Offset() int

Offset calculates the starting index for pagination based on page size & number

func (Page) Prev

func (p Page) Prev() Page

Prev returns a page with the number decremented by 1

func (Page) PrevPageExists

func (p Page) PrevPageExists() bool

PrevPageExists returns false if the page number is 1

func (Page) SetQueryParams

func (p Page) SetQueryParams(u *url.URL) *url.URL

SetQueryParams adds pagination info to a url as query parameters

type Response added in v0.10.0

type Response struct {
	Data       interface{} `json:"data,omitempty"`
	Meta       *Meta       `json:"meta,omitempty"`
	Pagination *Page       `json:"pagination,omitempty"`
}

Response is the JSON API response object wrapper

Jump to

Keyboard shortcuts

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