Documentation ¶
Overview ¶
Package htadaptor provides generic domain logic adaptors for HTTP handlers. Adaptors come in three flavors:
1. UnaryFunc: func(context, inputStruct) (outputStruct, error) 2. NullaryFunc: func(context) (outputStruct, error) 3. VoidFunc: func(context, inputStruct) error
Each input requires implementation of Validatable for safety. Validation errors are decorated with the correct http.StatusUnprocessableEntity status code.
Index ¶
- Variables
- func Must(h http.Handler, err error) http.Handler
- type CookieValueExtractor
- type Decoder
- type Encoder
- type EncoderFunc
- type Error
- type FuncType
- type HeaderValueExtractor
- type InvalidRequestError
- type NullaryFuncAdaptor
- type Option
- func WithCookieValues(names ...string) Option
- func WithDecoder(d Decoder) Option
- func WithDecoderOptions(withOptions ...reflectd.Option) Option
- func WithDefaultDecoder() Option
- func WithDefaultEncoder() Option
- func WithDefaultResponseHandler() Option
- func WithEncoder(e Encoder) Option
- func WithEncoderFunc(f func(http.ResponseWriter, any) error) Option
- func WithExtractors(exs ...reflectd.RequestValueExtractor) Option
- func WithHeaderValues(names ...string) Option
- func WithHyperTextEncoder(t *template.Template) Option
- func WithMemoryLimit(upto int64) Option
- func WithOptions(withOptions ...Option) Option
- func WithPathValues(names ...string) Option
- func WithQueryValues(names ...string) Option
- func WithReadLimit(upto int64) Option
- func WithResponseHandler(h ResponseHandler) Option
- type PathValueExtractor
- type QueryValueExtractor
- type ResponseHandler
- type StringValueExtractor
- type StringValueExtractorFunc
- type UnaryFuncAdaptor
- type UnaryStringFuncAdaptor
- type Validatable
- type VoidFuncAdaptor
- type VoidStringFuncAdaptor
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrNoStringValue = errors.New("empty value")
Functions ¶
Types ¶
type CookieValueExtractor ¶ added in v0.0.6
type CookieValueExtractor string
func NewCookieValueExtractor ¶ added in v0.0.6
func NewCookieValueExtractor(name string) CookieValueExtractor
func (CookieValueExtractor) ExtractStringValue ¶ added in v0.0.6
func (e CookieValueExtractor) ExtractStringValue(r *http.Request) (string, error)
type EncoderFunc ¶
type EncoderFunc func(http.ResponseWriter, any) error
func (EncoderFunc) Encode ¶
func (f EncoderFunc) Encode(w http.ResponseWriter, v any) error
type HeaderValueExtractor ¶ added in v0.0.4
type HeaderValueExtractor string
func NewHeaderValueExtractor ¶ added in v0.0.4
func NewHeaderValueExtractor(name string) HeaderValueExtractor
func (HeaderValueExtractor) ExtractStringValue ¶ added in v0.0.6
func (e HeaderValueExtractor) ExtractStringValue(r *http.Request) (string, error)
type InvalidRequestError ¶
type InvalidRequestError struct {
// contains filtered or unexported fields
}
func NewInvalidRequestError ¶
func NewInvalidRequestError(fromError error) *InvalidRequestError
func (*InvalidRequestError) Error ¶
func (e *InvalidRequestError) Error() string
func (*InvalidRequestError) HyperTextStatusCode ¶
func (e *InvalidRequestError) HyperTextStatusCode() int
func (*InvalidRequestError) Unwrap ¶
func (e *InvalidRequestError) Unwrap() error
type NullaryFuncAdaptor ¶
type NullaryFuncAdaptor[O any] struct { // contains filtered or unexported fields }
func NewNullaryFuncAdaptor ¶
func (*NullaryFuncAdaptor[O]) ServeHTTP ¶
func (a *NullaryFuncAdaptor[O]) ServeHTTP( w http.ResponseWriter, r *http.Request, )
func (*NullaryFuncAdaptor[O]) ServeHyperText ¶
func (a *NullaryFuncAdaptor[O]) ServeHyperText( w http.ResponseWriter, r *http.Request, ) (err error)
type Option ¶
type Option func(*options) error
func WithCookieValues ¶ added in v0.0.6
func WithDecoder ¶
func WithDecoderOptions ¶
func WithDefaultDecoder ¶
func WithDefaultDecoder() Option
func WithDefaultEncoder ¶
func WithDefaultEncoder() Option
func WithDefaultResponseHandler ¶ added in v0.0.9
func WithDefaultResponseHandler() Option
func WithEncoder ¶
func WithEncoderFunc ¶
func WithEncoderFunc(f func(http.ResponseWriter, any) error) Option
func WithExtractors ¶ added in v0.0.4
func WithExtractors(exs ...reflectd.RequestValueExtractor) Option
func WithHeaderValues ¶ added in v0.0.4
func WithHyperTextEncoder ¶
func WithMemoryLimit ¶ added in v0.0.4
func WithOptions ¶
func WithPathValues ¶ added in v0.0.4
func WithQueryValues ¶ added in v0.0.4
func WithReadLimit ¶ added in v0.0.4
func WithResponseHandler ¶ added in v0.0.9
func WithResponseHandler(h ResponseHandler) Option
type PathValueExtractor ¶ added in v0.0.6
type PathValueExtractor string
func NewPathValueExtractor ¶ added in v0.0.6
func NewPathValueExtractor(name string) PathValueExtractor
func (PathValueExtractor) ExtractStringValue ¶ added in v0.0.6
func (e PathValueExtractor) ExtractStringValue(r *http.Request) (string, error)
type QueryValueExtractor ¶ added in v0.0.4
type QueryValueExtractor string
func NewQueryValueExtractor ¶ added in v0.0.4
func NewQueryValueExtractor(name string) QueryValueExtractor
func (QueryValueExtractor) ExtractStringValue ¶ added in v0.0.6
func (e QueryValueExtractor) ExtractStringValue(r *http.Request) (value string, err error)
type ResponseHandler ¶ added in v0.0.9
type ResponseHandler interface { HandleSuccess(http.ResponseWriter, *http.Request) error HandleError(http.ResponseWriter, *http.Request, error) }
type StringValueExtractor ¶ added in v0.0.6
type StringValueExtractorFunc ¶ added in v0.0.6
func (StringValueExtractorFunc) ExtractStringValue ¶ added in v0.0.6
func (f StringValueExtractorFunc) ExtractStringValue(r *http.Request) (string, error)
type UnaryFuncAdaptor ¶
type UnaryFuncAdaptor[ T any, V Validatable[T], O any, ] struct { // contains filtered or unexported fields }
func NewUnaryFuncAdaptor ¶
func NewUnaryFuncAdaptor[ T any, V Validatable[T], O any, ]( domainCall func(context.Context, V) (O, error), withOptions ...Option, ) (*UnaryFuncAdaptor[T, V, O], error)
func (*UnaryFuncAdaptor[T, V, O]) ServeHTTP ¶
func (a *UnaryFuncAdaptor[T, V, O]) ServeHTTP( w http.ResponseWriter, r *http.Request, )
func (*UnaryFuncAdaptor[T, V, O]) ServeHyperText ¶
func (a *UnaryFuncAdaptor[T, V, O]) ServeHyperText( w http.ResponseWriter, r *http.Request, ) (err error)
type UnaryStringFuncAdaptor ¶ added in v0.0.2
type UnaryStringFuncAdaptor[O any] struct { // contains filtered or unexported fields }
func NewUnaryStringFuncAdaptor ¶ added in v0.0.2
func NewUnaryStringFuncAdaptor[O any]( domainCall func(context.Context, string) (O, error), stringExtractor StringValueExtractor, withOptions ...Option, ) (*UnaryStringFuncAdaptor[O], error)
func (*UnaryStringFuncAdaptor[O]) ServeHTTP ¶ added in v0.0.2
func (a *UnaryStringFuncAdaptor[O]) ServeHTTP( w http.ResponseWriter, r *http.Request, )
func (*UnaryStringFuncAdaptor[O]) ServeHyperText ¶ added in v0.0.2
func (a *UnaryStringFuncAdaptor[O]) ServeHyperText( w http.ResponseWriter, r *http.Request, ) (err error)
type Validatable ¶
Validatable constrains a domain request. Validation errors are wrapped as InvalidRequestError by the adapter.
type VoidFuncAdaptor ¶
type VoidFuncAdaptor[ T any, V Validatable[T], ] struct { // contains filtered or unexported fields }
func NewVoidFuncAdaptor ¶
func NewVoidFuncAdaptor[ T any, V Validatable[T], ]( domainCall func(context.Context, V) error, withOptions ...Option, ) (*VoidFuncAdaptor[T, V], error)
func (*VoidFuncAdaptor[T, V]) ServeHTTP ¶
func (a *VoidFuncAdaptor[T, V]) ServeHTTP( w http.ResponseWriter, r *http.Request, )
func (*VoidFuncAdaptor[T, V]) ServeHyperText ¶
func (a *VoidFuncAdaptor[T, V]) ServeHyperText( w http.ResponseWriter, r *http.Request, ) (err error)
type VoidStringFuncAdaptor ¶ added in v0.0.2
type VoidStringFuncAdaptor struct {
// contains filtered or unexported fields
}
func NewVoidStringFuncAdaptor ¶ added in v0.0.2
func NewVoidStringFuncAdaptor( domainCall func(context.Context, string) error, stringExtractor StringValueExtractor, withOptions ...Option, ) (*VoidStringFuncAdaptor, error)
func (*VoidStringFuncAdaptor) ServeHTTP ¶ added in v0.0.2
func (a *VoidStringFuncAdaptor) ServeHTTP( w http.ResponseWriter, r *http.Request, )
func (*VoidStringFuncAdaptor) ServeHyperText ¶ added in v0.0.2
func (a *VoidStringFuncAdaptor) ServeHyperText( w http.ResponseWriter, r *http.Request, ) (err error)
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
chivalues
module
|
|
examples
|
|
basic
Package main demonstrates application of generic domain adaptors to satisfy [http.Handler] interface.
|
Package main demonstrates application of generic domain adaptors to satisfy [http.Handler] interface. |
extract
|
|
chivalues
Module
|
|
Package reflectd provides configurable HTTP form body to struct decoder that depends on Gorilla's schema reflection package.
|
Package reflectd provides configurable HTTP form body to struct decoder that depends on Gorilla's schema reflection package. |
schema
Package schema fills a struct with form values.
|
Package schema fills a struct with form values. |
Package slogrh handles domain logic reponses by logging them using [slog.Logger] and printing a simple error message.
|
Package slogrh handles domain logic reponses by logging them using [slog.Logger] and printing a simple error message. |
TODO: move this into root package.
|
TODO: move this into root package. |
Click to show internal directories.
Click to hide internal directories.