Documentation ¶
Overview ¶
Package support is full of helper functions and types for the code generator
Index ¶
- Variables
- func ExplodedFormArrayToSlice[T any](formArray []string, convert func(s string) (T, error)) ([]T, error)
- func FlatFormArrayToSlice[T any](formArray []string, convert func(s string) (T, error)) ([]T, error)
- func ReadJSON(r *http.Request, object any) error
- func ReadJSONBuffer(r *http.Request, object any) (*bytes.Buffer, error)
- func ReturnJSONBuffer(b *bytes.Buffer)
- func StringNoOp(s string) (string, error)
- func StringToBool(s string) (bool, error)
- func StringToChronoDate(s string) (chrono.Date, error)
- func StringToChronoDateTime(s string) (chrono.DateTime, error)
- func StringToChronoTime(s string) (chrono.Time, error)
- func StringToDate(s string) (time.Time, error)
- func StringToDateTime(s string) (time.Time, error)
- func StringToDecimal(s string) (decimal.Decimal, error)
- func StringToDuration(s string) (time.Duration, error)
- func StringToFloat[T constraints.Float](s string) (T, error)
- func StringToInt[T constraints.Signed](s string) (T, error)
- func StringToString[A, B ~string](s A) (B, error)
- func StringToTime(s string) (time.Time, error)
- func StringToUUID(s string) (uuid.UUID, error)
- func StringToUint[T constraints.Unsigned](s string) (T, error)
- func ValidateEnum[S ~string](s S, whitelist []string) error
- func ValidateFormatDecimal(s string) error
- func ValidateFormatUUIDv4(s string) error
- func ValidateMaxItems[T any](a []T, max int) error
- func ValidateMaxLength[S ~string](s S, max int) error
- func ValidateMaxNumber[N constraints.Integer | constraints.Float](val, max N, exclusive bool) error
- func ValidateMaxProperties[V any, M ~map[string]V](m M, max int) error
- func ValidateMaxShopspringDecimal(val, max decimal.Decimal, exclusive bool) error
- func ValidateMinItems[T any](a []T, min int) error
- func ValidateMinLength[S ~string](s S, min int) error
- func ValidateMinNumber[N constraints.Integer | constraints.Float](val, min N, exclusive bool) error
- func ValidateMinProperties[V any, M ~map[string]V](m M, min int) error
- func ValidateMinShopspringDecimal(val, min decimal.Decimal, exclusive bool) error
- func ValidateMultipleOfFloat[N constraints.Float](val, factor N) error
- func ValidateMultipleOfInt[N constraints.Integer](val, factor N) error
- func ValidatePattern[S ~string](s S, pattern string) error
- func ValidateUniqueItems(a any) error
- func WriteJSON(w http.ResponseWriter, object any) error
- type ErrorHandler
- type Errors
- type MW
- type UnmarshalBodyError
- type ValidationConverter
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
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
ReturnJSONBuffer is called to return a buffer to the pool.
func StringNoOp ¶ added in v0.0.37
StringNoOp is used to prevent extra special cases where other types will require conversion.
func StringToChronoDate ¶ added in v0.0.20
StringToChronoDate checks for an RFC3339 "date" production using chrono library
func StringToChronoDateTime ¶ added in v0.0.20
StringToChronoDateTime parses an RFC3339 "date-time" production using chrono library
func StringToChronoTime ¶ added in v0.0.20
StringToChronoTime parses an RFC3339 "time" production using chrono library
func StringToDate ¶ added in v0.0.20
StringToDate checks for an RFC3339 "date" production as time.Time
func StringToDateTime ¶ added in v0.0.20
StringToDateTime parses an RFC3339 "date-time" production as time.Time
func StringToDecimal ¶ added in v0.0.36
StringToDecimal converts a string to a decimal type
func StringToDuration ¶ added in v0.0.20
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
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
StringToTime parses an RFC3339 "time" production as time.Time
func StringToUUID ¶ added in v0.0.36
StringToUUID converts a string to a uuid type
func StringToUint ¶
func StringToUint[T constraints.Unsigned](s string) (T, error)
StringToUint conversion
func ValidateEnum ¶
ValidateEnum validates a string against a whitelisted set of values
func ValidateFormatDecimal ¶ added in v0.0.36
ValidateFormatDecimal checks it look like it's in a decimal shape
func ValidateFormatUUIDv4 ¶
ValidateFormatUUIDv4 checks that it look like it's in a UUID v4 shape
func ValidateMaxItems ¶
ValidateMaxItems ensures a array's length is <= max
func ValidateMaxLength ¶
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 ¶
ValidateMaxProperties ensures a map[string]X's length is <= max
func ValidateMaxShopspringDecimal ¶ added in v0.0.57
ValidateMaxDecimal checks that val <= max or if exclusive then val < max
func ValidateMinItems ¶
ValidateMinItems ensures an array's length is >= min
func ValidateMinLength ¶
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 ¶
ValidateMinProperties ensures a map[string]X's length is >= min
func ValidateMinShopspringDecimal ¶ added in v0.0.57
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 ¶
ValidatePattern validates a string against a pattern
func ValidateUniqueItems ¶
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 ¶
Errors is how validation errors are given to the ValidationConverter
func AddErrs ¶
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
AddErrsFlatten flattens toAdd by adding key on to the errors inside toAdd
eg. {"a": ["1"]}, "key", {"b": ["2"]} = {"a": ["1"], "key.b": ["2"]}
type MW ¶
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 ¶
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.