Documentation
¶
Overview ¶
Package paramex is a library that binds http request parameters to a Go struct annotated with param.
Description ¶
To extract http parameters (headers, url query values, form values), multiple code lines need to be written in http Handlers.
But, Using Paramex http headers, url query values or form values can be extracted by calling a single function.
Sample code example code to extract request form values using paramex is shown below.
package main import ( "fmt" "log" "net/http" "net/url" "strings" "github.com/senpathi/paramex" ) type formParams struct { Name string `param:"name"` Age int `param:"age"` Height float64 `param:"height"` Married bool `param:"married"` OtherNames []string `param:"other_names"` } func main() { reqForm := url.Values{} reqForm.Set(`name`, `form_name`) reqForm.Set(`age`, `50`) reqForm.Set(`height`, `1.72`) reqForm.Set(`married`, `true`) reqForm[`other_names`] = []string{`form_test`, `form_example`} req, err := http.NewRequest(`POST`, `https://nipuna.lk`, strings.NewReader(reqForm.Encode())) if err != nil { log.Fatalln(err) } req.Header.Add("Content-Type", "application/x-www-form-urlencoded") forms := formParams{} extractor := paramex.NewParamExtractor() err = extractor.ExtractForms(&forms, req) if err != nil { log.Fatalln(fmt.Errorf(`error extracting forms due to %v`, err)) } fmt.Println(fmt.Sprintf(`request forms := %v`, forms)) //Output : request forms := {form_name 50 1.72 true [form_test form_example]} }
Examples codes to extract http headers, url query values and form values are implemented in https://github.com/senpathi/paramex/tree/master/example directory.
Supported parameter types
- string
- bool
- int32
- int
- int64
- float32
- float64
- []string (only for form values and query values)
- https://github.com/google/uuid
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorNotAssignable ¶
type ErrorNotAssignable struct {
// contains filtered or unexported fields
}
ErrorNotAssignable created when sent interface to extract is not assignable. Not a reference to a Go struct
type ErrorUnSupportedParamType ¶
type ErrorUnSupportedParamType struct {
// contains filtered or unexported fields
}
ErrorUnSupportedParamType created when trying to extract unsupported parameter type
type ErrorUnSupportedType ¶
type ErrorUnSupportedType struct {
// contains filtered or unexported fields
}
ErrorUnSupportedType created when sent reference is not a Go struct type reference
type ErrorUnmarshalType ¶
type ErrorUnmarshalType struct {
// contains filtered or unexported fields
}
ErrorUnmarshalType created when trying to marshal different type value to another type variable
type Extractor ¶
type Extractor interface { // ExtractHeaders extract http headers from sent request and binds to `v` // `v` should be a Go struct reference ExtractHeaders(v interface{}, req *http.Request) error // ExtractQueries extract http url parameters from sent request and binds to v // `v` should be a Go struct reference ExtractQueries(v interface{}, req *http.Request) error // ExtractForms extract http form values from sent request and binds to v // `v` should be a Go struct reference ExtractForms(v interface{}, req *http.Request) error }
The Extractor interface is implemented to extract http request headers, form values and url query values
func NewParamExtractor ¶
func NewParamExtractor() Extractor
NewParamExtractor returns an Extractor which extract req.Header, req.FormValue, req.URL.Query values and binds them to a Go struct