forms

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package forms assists working with web forms and Request.Form

Index

Constants

View Source
const DefaultFormMaxMemory = 1 << 20 // 1MiB

DefaultFormMaxMemory indicates the memory limit when parsing a form used when ParseForm is called without a positive number.

Variables

View Source
var ErrMissing = errors.New("not specified")

ErrMissing indicates the field was missing from the request.

View Source
var ErrRange = strconv.ErrRange

ErrRange indicates the value of the field is out of range

View Source
var ErrSyntax = strconv.ErrSyntax

ErrSyntax indicates the field isn't of the correct form.

Functions

func FormValue added in v0.6.5

func FormValue[T core.String](req *http.Request, field string) (value T, found bool, err error)

FormValue reads a field from req.Form, after populating it if needed, and returning the trimmed string, an indicator saying if it was actually present, or an error if ParseForm failed.

func FormValueBool added in v0.6.5

func FormValueBool[T core.Bool](req *http.Request, field string) (value T, found bool, err error)

FormValueBool reads a field from [http.Request#Form], after populating it if needed, and returns a core.Bool value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormValueFloat added in v0.6.5

func FormValueFloat[T core.Float](req *http.Request, field string) (value T, found bool, err error)

FormValueFloat reads a field from [http.Request#Form], after populating it if needed, and returns a core.Float value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormValueFloatInRange added in v0.6.5

func FormValueFloatInRange[T core.Float](req *http.Request, field string,
	minV, maxV T) (value T, found bool, err error)

FormValueFloatInRange reads a field from [http.Request#Form], after populating it if needed, and returns a core.Float value, an indicator saying if it was actually, and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type or if it's outside the specified boundaries.

func FormValueFn added in v0.7.0

func FormValueFn[T any, S core.String](req *http.Request, field string,
	fn func(S) (T, error)) (T, bool, error)

FormValueFn reads a field from req.Form, and processes it through a helper function to get the value. FormValueFn also indicates if the field is missing, or if ParseForm or the conversion failed.

func FormValueSigned added in v0.6.5

func FormValueSigned[T core.Signed](req *http.Request, field string,
	base int) (value T, found bool, err error)

FormValueSigned reads a field from [http.Request#Form], after populating it if needed, and returns a core.Signed value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormValueSignedInRange added in v0.6.5

func FormValueSignedInRange[T core.Signed](req *http.Request, field string, base int,
	minV, maxV T) (value T, found bool, err error)

FormValueSignedInRange reads a field from [http.Request#Form], after populating it if needed, and returns a core.Signed value, an indicator saying if it was actually, and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type or if it's outside the specified boundaries.

func FormValueUnsigned added in v0.6.5

func FormValueUnsigned[T core.Unsigned](req *http.Request, field string,
	base int) (value T, found bool, err error)

FormValueUnsigned reads a field from [http.Request#Form], after populating it if needed, and returns a core.Unsigned value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormValueUnsignedInRange added in v0.6.5

func FormValueUnsignedInRange[T core.Unsigned](req *http.Request, field string, base int,
	minV, maxV T) (value T, found bool, err error)

FormValueUnsignedInRange reads a field from [http.Request#Form], after populating it if needed, and returns a core.Unsigned value, an indicator saying if it was actually, and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type or if it's outside the specified boundaries.

func FormValues added in v0.6.5

func FormValues[T core.String](req *http.Request, field string) (values []T, found bool, err error)

FormValues reads a field from req.Form, after populating it if needed, and returning the trimmed strings, an indicator saying if it was actually present, or an error if ParseForm failed. Empty values will be omitted.

func FormValuesBool added in v0.6.5

func FormValuesBool[T core.Bool](req *http.Request, field string) (values []T, found bool, err error)

FormValuesBool reads a field from [http.Request#Form], after populating it if needed, and returns its core.Bool values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormValuesFloat added in v0.6.5

func FormValuesFloat[T core.Float](req *http.Request, field string) (values []T, found bool, err error)

FormValuesFloat reads a field from [http.Request#Form], after populating it if needed, and returns its core.Float values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormValuesFn added in v0.7.0

func FormValuesFn[T any, S core.String](req *http.Request, field string,
	fn func(S) (T, error)) ([]T, bool, error)

FormValuesFn reads a field from req.Form, and processes all values through a helper function to get the values. FormValuesFn also indicates if the field is missing, or if ParseForm or the conversion failed.

func FormValuesSigned added in v0.6.5

func FormValuesSigned[T core.Signed](req *http.Request, field string,
	base int) (values []T, found bool, err error)

FormValuesSigned reads a field from [http.Request#Form], after populating it if needed, and returns its core.Signed values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormValuesUnsigned added in v0.6.5

func FormValuesUnsigned[T core.Unsigned](req *http.Request, field string,
	base int) (values []T, found bool, err error)

FormValuesUnsigned reads a field from [http.Request#Form], after populating it if needed, and returns its core.Unsigned values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func FormatFloat added in v0.6.5

func FormatFloat[T core.Float](v T, fmt byte, prec int) string

FormatFloat extends strconv.FormatFloat working with any [Float] type.

func FormatSigned added in v0.6.5

func FormatSigned[T core.Signed](v T, base int) string

FormatSigned extends strconv.FormatInt working with any [Signed] type.

func FormatUnsigned added in v0.6.5

func FormatUnsigned[T core.Unsigned](v T, base int) string

FormatUnsigned extends strconv.FormatUint working with any [Unsigned] type.

func IsEmptyString added in v0.7.1

func IsEmptyString(err error) bool

IsEmptyString recursively checks if the given error was caused by the argument being an empty string.

func IsNilOrEmptyString added in v0.7.1

func IsNilOrEmptyString(err error) bool

IsNilOrEmptyString recursively checks if the given error was caused by the argument being an empty string, but it also succeeds when no error is given. To be used when testing an error condition.

func ParseBool added in v0.6.5

func ParseBool[T core.Bool](s string) (T, error)

ParseBool extends the standard strconv.ParseBool making the comparison case-insensitive and allowing y/yes/n/no options.

func ParseFloat added in v0.6.5

func ParseFloat[T core.Float](s string) (v T, err error)

ParseFloat converts a string to a floating point value of the correct size for the type using strconv.ParseFloat.

func ParseFloatInRange added in v0.6.5

func ParseFloatInRange[T core.Float](s string, minV, maxV T) (value T, err error)

ParseFloatInRange parses a string and and returns a core.Float value or a strconv.NumError if invalid or it's outside the specified boundaries.

func ParseForm

func ParseForm(req *http.Request, maxMemory int64) (err error)

ParseForm is similar to the standard request.ParseForm() but it handles urlencoded, multipart and JSON. For nested JSON objects ParseForm uses dots to join keys.

func ParseSigned added in v0.6.5

func ParseSigned[T core.Signed](s string, base int) (v T, err error)

ParseSigned converts a string to a signed integer value of the correct size for the type using strconv.ParseInt.

func ParseSignedInRange added in v0.6.5

func ParseSignedInRange[T core.Signed](s string, base int, minV, maxV T) (value T, err error)

ParseSignedInRange parses a string and and returns a core.Signed value or a strconv.NumError if invalid or it's outside the specified boundaries.

func ParseUnsigned added in v0.6.5

func ParseUnsigned[T core.Unsigned](s string, base int) (v T, err error)

ParseUnsigned converts a string to an unsigned integer value of the correct size for the type using strconv.ParseUint.

func ParseUnsignedInRange added in v0.6.5

func ParseUnsignedInRange[T core.Unsigned](s string, base int, minV, maxV T) (value T, err error)

ParseUnsignedInRange parses a string and and returns a core.Unsigned value or a strconv.NumError if invalid or it's outside the specified boundaries.

func PostFormValue added in v0.6.5

func PostFormValue[T core.String](req *http.Request, field string) (value T, found bool, err error)

PostFormValue reads a field from req.PostForm, after populating it if needed, and returning the trimmed string, an indicator saying if it was actually present, or an error if ParseForm failed.

func PostFormValueBool added in v0.6.5

func PostFormValueBool[T core.Bool](req *http.Request, field string) (value T, found bool, err error)

PostFormValueBool reads a field from [http.Request#PostForm], after populating it if needed, and returns a core.Bool value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func PostFormValueFloat added in v0.6.5

func PostFormValueFloat[T core.Float](req *http.Request, field string) (value T, found bool, err error)

PostFormValueFloat reads a field from [http.Request#PostForm], after populating it if needed, and returns a core.Float value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func PostFormValueFloatInRange added in v0.6.5

func PostFormValueFloatInRange[T core.Float](req *http.Request, field string,
	minV, maxV T) (value T, found bool, err error)

PostFormValueFloatInRange reads a field from [http.Request#PostForm], after populating it if needed, and returns a core.Float value, an indicator saying if it was actually, and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type or if it's outside the specified boundaries.

func PostFormValueFn added in v0.7.0

func PostFormValueFn[T any, S core.String](req *http.Request, field string,
	fn func(S) (T, error)) (T, bool, error)

PostFormValueFn reads a field from req.PostForm, and processes it through a helper function to get the value. PostFormValueFn also indicates if the field is missing, or if ParseForm or the conversion failed.

func PostFormValueSigned added in v0.6.5

func PostFormValueSigned[T core.Signed](req *http.Request, field string,
	base int) (value T, found bool, err error)

PostFormValueSigned reads a field from [http.Request#PostForm], after populating it if needed, and returns a core.Signed value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func PostFormValueSignedInRange added in v0.6.5

func PostFormValueSignedInRange[T core.Signed](req *http.Request, field string, base int,
	minV, maxV T) (value T, found bool, err error)

PostFormValueSignedInRange reads a field from [http.Request#PostForm], after populating it if needed, and returns a core.Signed value, an indicator saying if it was actually, and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type or if it's outside the specified boundaries.

func PostFormValueUnsigned added in v0.6.5

func PostFormValueUnsigned[T core.Unsigned](req *http.Request, field string,
	base int) (value T, found bool, err error)

PostFormValueUnsigned reads a field from [http.Request#PostForm], after populating it if needed, and returns a core.Unsigned value, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func PostFormValueUnsignedInRange added in v0.6.5

func PostFormValueUnsignedInRange[T core.Unsigned](req *http.Request, field string, base int,
	minV, maxV T) (value T, found bool, err error)

PostFormValueUnsignedInRange reads a field from [http.Request#PostForm], after populating it if needed, and returns a core.Unsigned value, an indicator saying if it was actually, and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type or if it's outside the specified boundaries.

func PostFormValues added in v0.6.5

func PostFormValues[T core.String](req *http.Request, field string) (values []T, found bool, err error)

PostFormValues reads a field from req.PostForm, after populating it if needed, and returning the trimmed strings, an indicator saying if it was actually present, or an error if ParseForm failed. Empty values will be omitted.

func PostFormValuesBool added in v0.6.5

func PostFormValuesBool[T core.Bool](req *http.Request, field string) (values []T, found bool, err error)

PostFormValuesBool reads a field from [http.Request#PostForm], after populating it if needed, and returns its core.Bool values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func PostFormValuesFloat added in v0.6.5

func PostFormValuesFloat[T core.Float](req *http.Request, field string) (values []T, found bool, err error)

PostFormValuesFloat reads a field from [http.Request#PostForm], after populating it if needed, and returns its core.Float values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func PostFormValuesFn added in v0.7.0

func PostFormValuesFn[T any, S core.String](req *http.Request, field string,
	fn func(S) (T, error)) ([]T, bool, error)

PostFormValuesFn reads a field from req.PostForm, and processes all values through a helper function to get the values. PostFormValuesFn also indicates if the field is missing, or if ParseForm or the conversion failed.

func PostFormValuesSigned added in v0.6.5

func PostFormValuesSigned[T core.Signed](req *http.Request, field string,
	base int) (values []T, found bool, err error)

PostFormValuesSigned reads a field from [http.Request#PostForm], after populating it if needed, and returns its core.Signed values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func PostFormValuesUnsigned added in v0.6.5

func PostFormValuesUnsigned[T core.Unsigned](req *http.Request, field string,
	base int) (values []T, found bool, err error)

PostFormValuesUnsigned reads a field from [http.Request#PostForm], after populating it if needed, and returns its core.Unsigned values, an indicator saying if it was actually present and possibly an error. Errors could indicate ParseForm failed, or a strconv.NumError if it couldn't be converted to the intended type.

func ReadAll

func ReadAll(body io.Reader, maxBytes int64) ([]byte, error)

ReadAll read the whole body of a request but fails if it exceeds the given limit. If no limit is provided, DefaultFormMaxMemory will be used.

func UnmarshalFormJSON

func UnmarshalFormJSON(b []byte) (url.Values, error)

UnmarshalFormJSON attempts to convert a JSON document into a url.Values map.

Types

type NumError added in v0.6.7

type NumError = strconv.NumError

NumError is an alias of the standard string to value conversion error

Jump to

Keyboard shortcuts

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