Documentation ¶
Overview ¶
Package support is full of helper functions and types for the code generator
Index ¶
- Variables
- func ReadJSON(r *http.Request, object interface{}) 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 StringToDuration(s string) (time.Duration, error)
- func StringToFloat[T constraints.Float](s string, bits int) (T, error)
- func StringToInt[T constraints.Signed](s string, bits int) (T, error)
- func StringToTime(s string) (time.Time, error)
- func StringToUint[T constraints.Unsigned](s string, bits int) (T, error)
- func ValidateEnum[S ~string](s S, whitelist []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 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 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 interface{}) error
- func WriteJSON(w http.ResponseWriter, object interface{}) error
- type ErrorHandler
- type Errors
- type MW
- 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 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 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 StringToDuration ¶ added in v0.0.20
StringToDuration parses an RFC3339 "duration" production
func StringToFloat ¶
func StringToFloat[T constraints.Float](s string, bits int) (T, error)
StringToFloat conversion
func StringToInt ¶
func StringToInt[T constraints.Signed](s string, bits int) (T, error)
StringToInt conversion
func StringToTime ¶ added in v0.0.20
StringToTime parses an RFC3339 "time" production as time.Time
func StringToUint ¶
func StringToUint[T constraints.Unsigned](s string, bits int) (T, error)
StringToUint conversion
func ValidateEnum ¶
ValidateEnum validates a string against a whitelisted set of values
func ValidateFormatUUIDv4 ¶
ValidateFormatUUIDv4 makes 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 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 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 ¶
func ValidateUniqueItems(a interface{}) 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 interface{}) 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 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.