milk

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrCodeRequired     = "required"       // Value is required
	ErrCodeValueTooHigh = "value-too-high" // Numeric/date value is too high. Text field is too long.
	ErrCodeValueTooLow  = "value-too-low"  // Numeric/date value is too low. Text field is too short.
	ErrCodeDuplicate    = "duplicate"      // Duplicate. Eg username is taken or entity already exists
	ErrCodeSyntaxError  = "syntax-error"   // Invalid value. Unparseable/unreadable/not following conventions
	ErrCodeInvalidState = "invalid-state"  // Value represents/would lead to an invalid state for the object
	ErrCodeNotFound     = "not-found"      // The resource pointed by the value was not found. Eg reference to non-existing entity
)
View Source
const StatusValidationError = 422

Variables

View Source
var (
	ErrUnauthorized = NewError(http.StatusUnauthorized, "")
	ErrForbidden    = NewError(http.StatusForbidden, "")
	ErrNotFound     = NewError(http.StatusNotFound, "")
	ErrConflict     = NewError(http.StatusConflict, "")
	ErrBadRequest   = NewError(http.StatusBadRequest, "")
)
View Source
var DateFormat = "2006-01-02"

DateFormat is the date format used when parsing a date in Params.GetDate()

Functions

This section is empty.

Types

type Context

type Context struct {
	context.Context

	// R is the original http request object of the handler
	R *http.Request

	// W will write to the original response writer of the handler
	W http.ResponseWriter

	// Result holds the data to be returned to the client
	Result interface{}

	// Params holds the parameters of the url and querystring of the context's request.
	Params *Params

	// Values holds context specific values
	Values Values
	// contains filtered or unexported fields
}

func (*Context) Err

func (this *Context) Err() error

Err() returns any errors returned by the handlers If there are multiple errors, an error of type Errors is returned, allowing access to each individual error in the order that they were generated.

func (*Context) Next

func (this *Context) Next()

Next() calls the next handler in the chain of handlers, if any. It can be used by middleware handlers to continue processing other handlers and delay execution of code until after these have finished.

func (*Context) OnEvent

func (this *Context) OnEvent(event Event, fn func(*Context))

func (*Context) ParseBody

func (this *Context) ParseBody(dst interface{}) error

ParseBody parses the body of the request as a JSON string and unmarshals it into dst.

func (*Context) Stop

func (this *Context) Stop()

Stop() stops the context from calling any remaining handlers that have not yet run. Note that handlers that have already run before the handler calling Stop() and that have called Next() on the context will still execute the code that comes after Next().

Example:

func Middleware(c Context) error {
	c.Next() // the next handler calls c.Stop()
	c.Debugf("Hello") // this will still get called
	return nil
}

type CreateContextFn

type CreateContextFn func(r *http.Request) context.Context

type Error

type Error struct {
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message,omitempty"`
}

func NewError

func NewError(statusCode int, message string) *Error

func (*Error) Error

func (this *Error) Error() string

type Errors

type Errors []error

Errors holds one or more errors returned by the handler functions executed on the context. The errors are arranged ordered by when they occured.

func (Errors) Error

func (this Errors) Error() string

type Event

type Event int
const (
	OnResponseCompleted Event = iota
)

type FieldError

type FieldError struct {
	FieldName string      `json:"key"`
	ErrorCode string      `json:"errorCode"`
	Message   string      `json:"message,omitempty"`
	Data      interface{} `json:"data,omitempty"`
}

type HandlerFunc

type HandlerFunc func(c *Context) error

type Params

type Params struct {
	// contains filtered or unexported fields
}

Params provides access to parameters in the URL and querystring of a request.

func (*Params) Get

func (this *Params) Get(key string) string

Get returns the given key's value from the request path parameters or querystring. The request path is searched first and overrides any querystring values with the same key.

func (*Params) GetDate

func (this *Params) GetDate(key string) time.Time

GetDate returns the given key's value as a time.Time instance, parsed by the format set in the DateFormat variable. Returns the zero value for invalid or missing values. The request path is searched first and overrides any querystring values with the same key.

func (*Params) GetInt

func (this *Params) GetInt(key string) int

GetInt64 returns the given key's value as an int. Returns 0 for invalid or missing values. The request path is searched first and overrides any querystring values with the same key.

func (*Params) GetInt64

func (this *Params) GetInt64(key string) int64

GetInt64 returns the given key's value as an int64. Returns 0 for invalid or missing values. The request path is searched first and overrides any querystring values with the same key.

func (*Params) Override

func (this *Params) Override(key string, value string)

type Router

type Router struct {
	CreateContext CreateContextFn
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) Delete

func (this *Router) Delete(path string, fns ...HandlerFunc)

func (*Router) Get

func (this *Router) Get(path string, fns ...HandlerFunc)

func (*Router) Post

func (this *Router) Post(path string, fns ...HandlerFunc)

func (*Router) Put

func (this *Router) Put(path string, fns ...HandlerFunc)

func (*Router) ServeHTTP

func (this *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Router) SubRouter

func (this *Router) SubRouter(path string) *Router

func (*Router) Use

func (this *Router) Use(middleware HandlerFunc)

type ValidationError

type ValidationError struct {
	Errors []*FieldError `json:"errors,omitempty"`
}

func NewValidationError

func NewValidationError() *ValidationError

func (*ValidationError) AddError

func (this *ValidationError) AddError(key string, errorCode string)

func (*ValidationError) AddErrorDetailed

func (this *ValidationError) AddErrorDetailed(key string, errorCode string, data interface{}, hint string, hintArgs ...interface{})

func (*ValidationError) Error

func (this *ValidationError) Error() string

func (*ValidationError) HasErrors

func (this *ValidationError) HasErrors() bool

type Values

type Values map[interface{}]interface{}

func (Values) Get

func (this Values) Get(key interface{}) interface{}

Get returns the given key's value from the request path parameters or querystring. The request path is searched first, and overrides any querystring values with the same key.

func (Values) GetInt

func (this Values) GetInt(key interface{}) int

GetInt64 returns the given key's value as an int. Returns 0 for invalid or missing values.

func (Values) GetInt64

func (this Values) GetInt64(key interface{}) int64

GetInt64 returns the given key's value as an int64. Returns 0 for invalid or missing values.

func (Values) GetString

func (this Values) GetString(key interface{}) string

func (Values) Set

func (this Values) Set(key interface{}, val interface{})

Jump to

Keyboard shortcuts

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