support

package
v0.0.57 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 18 Imported by: 1

Documentation

Overview

Package support is full of helper functions and types for the code generator

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoBody is returned from a handler and expected to be handled by
	// ErrorHandler in some useful way for the application.
	ErrNoBody = errors.New("no body")
)

Functions

func ExplodedFormArrayToSlice added in v0.0.37

func ExplodedFormArrayToSlice[T any](formArray []string, convert func(s string) (T, error)) ([]T, error)

ExplodedFormArrayToSlice simply takes an array gathered from color=blue&color=black and converts it into []T using a conversion function

func FlatFormArrayToSlice added in v0.0.37

func FlatFormArrayToSlice[T any](formArray []string, convert func(s string) (T, error)) ([]T, error)

FlatFormArrayToSlice takes the first value from the form color=blue,black&color=yellow (ie. [blue,black]) and converts it into []T using a conversion function.

func ReadJSON

func ReadJSON(r *http.Request, object any) error

ReadJSON reads JSON from the body and ensures the body is closed. object should be a pointer in order to deserialize properly. We copy into a pooled buffer to avoid the allocation from ioutil.ReadAll or something similar.

func ReadJSONBuffer added in v0.0.45

func ReadJSONBuffer(r *http.Request, object any) (*bytes.Buffer, error)

ReadJSONBuffer is like ReadJSON but does not put back its buffer, instead it allows further use, must end with a ReturnJSONBuffer call.

If an error occurs the buffer does not need to be returned.

func ReturnJSONBuffer added in v0.0.45

func ReturnJSONBuffer(b *bytes.Buffer)

ReturnJSONBuffer is called to return a buffer to the pool.

func StringNoOp added in v0.0.37

func StringNoOp(s string) (string, error)

StringNoOp is used to prevent extra special cases where other types will require conversion.

func StringToBool

func StringToBool(s string) (bool, error)

StringToBool conversion

func StringToChronoDate added in v0.0.20

func StringToChronoDate(s string) (chrono.Date, error)

StringToChronoDate checks for an RFC3339 "date" production using chrono library

func StringToChronoDateTime added in v0.0.20

func StringToChronoDateTime(s string) (chrono.DateTime, error)

StringToChronoDateTime parses an RFC3339 "date-time" production using chrono library

func StringToChronoTime added in v0.0.20

func StringToChronoTime(s string) (chrono.Time, error)

StringToChronoTime parses an RFC3339 "time" production using chrono library

func StringToDate added in v0.0.20

func StringToDate(s string) (time.Time, error)

StringToDate checks for an RFC3339 "date" production as time.Time

func StringToDateTime added in v0.0.20

func StringToDateTime(s string) (time.Time, error)

StringToDateTime parses an RFC3339 "date-time" production as time.Time

func StringToDecimal added in v0.0.36

func StringToDecimal(s string) (decimal.Decimal, error)

StringToDecimal converts a string to a decimal type

func StringToDuration added in v0.0.20

func StringToDuration(s string) (time.Duration, error)

StringToDuration parses an RFC3339 "duration" production

func StringToFloat

func StringToFloat[T constraints.Float](s string) (T, error)

StringToFloat conversion

func StringToInt

func StringToInt[T constraints.Signed](s string) (T, error)

StringToInt conversion

func StringToString added in v0.0.37

func StringToString[A, B ~string](s A) (B, error)

StringToString is a somewhat useless function but handy for using in tandem with a Map() like function over a slice to convert a string to a specialized version of a string and back.

The error is to make it easier to pass in to a function that expects a conversion may create an error.

In particular this helps eliminate special cases for string where other types will require conversion.

func StringToTime added in v0.0.20

func StringToTime(s string) (time.Time, error)

StringToTime parses an RFC3339 "time" production as time.Time

func StringToUUID added in v0.0.36

func StringToUUID(s string) (uuid.UUID, error)

StringToUUID converts a string to a uuid type

func StringToUint

func StringToUint[T constraints.Unsigned](s string) (T, error)

StringToUint conversion

func ValidateEnum

func ValidateEnum[S ~string](s S, whitelist []string) error

ValidateEnum validates a string against a whitelisted set of values

func ValidateFormatDecimal added in v0.0.36

func ValidateFormatDecimal(s string) error

ValidateFormatDecimal checks it look like it's in a decimal shape

func ValidateFormatUUIDv4

func ValidateFormatUUIDv4(s string) error

ValidateFormatUUIDv4 checks that it look like it's in a UUID v4 shape

func ValidateMaxItems

func ValidateMaxItems[T any](a []T, max int) error

ValidateMaxItems ensures a array's length is <= max

func ValidateMaxLength

func ValidateMaxLength[S ~string](s S, max int) error

ValidateMaxLength ensures a string's length is <= max

func ValidateMaxNumber

func ValidateMaxNumber[N constraints.Integer | constraints.Float](val, max N, exclusive bool) error

ValidateMaxNumber checks that val <= max or if exclusive then val < max

func ValidateMaxProperties

func ValidateMaxProperties[V any, M ~map[string]V](m M, max int) error

ValidateMaxProperties ensures a map[string]X's length is <= max

func ValidateMaxShopspringDecimal added in v0.0.57

func ValidateMaxShopspringDecimal(val, max decimal.Decimal, exclusive bool) error

ValidateMaxDecimal checks that val <= max or if exclusive then val < max

func ValidateMinItems

func ValidateMinItems[T any](a []T, min int) error

ValidateMinItems ensures an array's length is >= min

func ValidateMinLength

func ValidateMinLength[S ~string](s S, min int) error

ValidateMinLength ensures a string's length is >= min

func ValidateMinNumber

func ValidateMinNumber[N constraints.Integer | constraints.Float](val, min N, exclusive bool) error

ValidateMinNumber checks that val >= min or if exclusive then val > min

func ValidateMinProperties

func ValidateMinProperties[V any, M ~map[string]V](m M, min int) error

ValidateMinProperties ensures a map[string]X's length is >= min

func ValidateMinShopspringDecimal added in v0.0.57

func ValidateMinShopspringDecimal(val, min decimal.Decimal, exclusive bool) error

ValidateMinDecimal checks that val >= min or if exclusive then val > min

func ValidateMultipleOfFloat

func ValidateMultipleOfFloat[N constraints.Float](val, factor N) error

ValidateMultipleOfInt checks that val % factor == 0

func ValidateMultipleOfInt

func ValidateMultipleOfInt[N constraints.Integer](val, factor N) error

ValidateMultipleOfInt checks that val % factor == 0

func ValidatePattern

func ValidatePattern[S ~string](s S, pattern string) error

ValidatePattern validates a string against a pattern

func ValidateUniqueItems

func ValidateUniqueItems(a any) error

ValidateUniqueItems ensures an arrays items are unique. Uses reflect.DeepEqual and a very naive algorithm, not very performant.

func WriteJSON

func WriteJSON(w http.ResponseWriter, object any) error

WriteJSON uses a pool of buffers to write into. This avoids a double allocation from using json.Marshal (json.Marshal uses its own internal pooled buffer and then copies that to a newly allocated []byte, this way we should have pools for both json's internal buffer and our own).

Types

type ErrorHandler

type ErrorHandler interface {
	Wrap(func(w http.ResponseWriter, r *http.Request) error) http.Handler
}

ErrorHandler is an adapter that allows routing to special http.HandlerFuncs that additionally have an error return.

type Errors

type Errors map[string][]string

Errors is how validation errors are given to the ValidationConverter

func AddErrs

func AddErrs(errs Errors, key string, toAdd ...error) Errors

AddErrs adds errors to an error map and returns the map

eg. {"a": ["1"]}, "a", "2" = {"a": ["1", "2"]}

func AddErrsFlatten added in v0.0.24

func AddErrsFlatten(errs Errors, key string, toAdd Errors) Errors

AddErrsFlatten flattens toAdd by adding key on to the errors inside toAdd

eg. {"a": ["1"]}, "key", {"b": ["2"]} = {"a": ["1"], "key.b": ["2"]}

func MergeErrs

func MergeErrs(dst Errors, src Errors) Errors

MergeErrs merges src's keys and values into dst. dst is created if it is nil. Returns dst. Colliding keys will be overwritten by what's in src.

type MW

type MW map[string][]func(http.Handler) http.Handler

MW is a middleware stack divided into tags. The first tag of an operation decides what middleware it belongs to. The empty string is middleware for untagged operations.

type UnmarshalBodyError added in v0.0.54

type UnmarshalBodyError struct {
	UnmarshalError error
}

UnmarshalBodyError is returned from a handler and expected to be handled by ErrorHandler.

func (UnmarshalBodyError) Error added in v0.0.54

func (e UnmarshalBodyError) Error() string

type ValidationConverter

type ValidationConverter func(Errors) error

ValidationConverter is used to convert validation errors to something that will work as a json response for all methods that must return validation errors. It must implement error to be passed back to the ErrorHandler interface.

Jump to

Keyboard shortcuts

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