Documentation ¶
Overview ¶
Package apiware provides a tools which can bind the http/fasthttp request params to the structure and validate.
Copyright 2016 HenryLee. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Param tag value description:
tag | key | required | value | desc ------|----------|----------|---------------|---------------------------------- param | in | only one | path | (position of param) if `required` is unsetted, auto set it. e.g. url: "http://www.abc.com/a/{path}" param | in | only one | query | (position of param) e.g. url: "http://www.abc.com/a?b={query}" param | in | only one | formData | (position of param) e.g. "request body: a=123&b={formData}" param | in | only one | body | (position of param) request body can be any content param | in | only one | header | (position of param) request header info param | in | only one | cookie | (position of param) request cookie info, support: `http.Cookie`,`fasthttp.Cookie`,`string`,`[]byte` param | name | no | (e.g. "id") | specify request param`s name param | required | no | required | request param is required param | desc | no | (e.g. "id") | request param description param | len | no | (e.g. 3:6, 3) | length range of param's value param | range | no | (e.g. 0:10) | numerical range of param's value param | nonzero | no | nonzero | param`s value can not be zero param | maxmb | no | (e.g. 32) | when request Content-Type is multipart/form-data, the max memory for body.(multi-param, whichever is greater) regexp| | no |(e.g. "^\\w+$")| param value can not be null err | | no |(e.g. "incorrect password format")| the custom error for binding or validating NOTES: 1. the binding object must be a struct pointer 2. the binding struct's field can not be a pointer 3. `regexp` or `param` tag is only usable when `param:"type(xxx)"` is exist 4. if the `param` tag is not exist, anonymous field will be parsed 5. when the param's position(`in`) is `formData` and the field's type is `multipart.FileHeader`, the param receives file uploaded 6. if param's position(`in`) is `cookie`, field's type must be `http.Cookie` 7. param tags `in(formData)` and `in(body)` can not exist at the same time 8. there should not be more than one `in(body)` param tag
List of supported param value types:
base | slice | special --------|------------|------------------------------------------------------- string | []string | [][]byte byte | []byte | [][]uint8 uint8 | []uint8 | multipart.FileHeader (only for `formData` param) bool | []bool | http.Cookie (only for `net/http`'s `cookie` param) int | []int | fasthttp.Cookie (only for `fasthttp`'s `cookie` param) int8 | []int8 | struct (struct type only for `body` param or as an anonymous field to extend params) int16 | []int16 | int32 | []int32 | int64 | []int64 | uint8 | []uint8 | uint16 | []uint16 | uint32 | []uint32 | uint64 | []uint64 | float32 | []float32 | float64 | []float64 |
Index ¶
- Constants
- Variables
- func Bind(structPointer interface{}, req *http.Request, pathParams KV) error
- func BindByName(paramsAPIName string, req *http.Request, pathParams KV) (interface{}, error)
- func ConvertAssign(dest reflect.Value, src ...string) (err error)
- func FasthttpBind(structPointer interface{}, req *fasthttp.RequestCtx, pathParams KV) error
- func FasthttpBindByName(paramsAPIName string, req *fasthttp.RequestCtx, pathParams KV) (interface{}, error)
- func NewValidationError(id int, field string) error
- func ParseTags(s string) map[string]string
- func Register(structPointer interface{}, paramNameFunc ParamNameFunc, ...) error
- func SetParamsAPI(m *ParamsAPI)
- type Apiware
- type BodyDecodeFunc
- type Error
- type KV
- type Map
- type Param
- type ParamNameFunc
- type ParamsAPI
- func (paramsAPI *ParamsAPI) BindAt(structPointer interface{}, req *http.Request, pathParams KV) error
- func (paramsAPI *ParamsAPI) BindFields(fields []reflect.Value, req *http.Request, pathParams KV) (err error)
- func (paramsAPI *ParamsAPI) BindNew(req *http.Request, pathParams KV) (interface{}, error)
- func (paramsAPI *ParamsAPI) FasthttpBindAt(structPointer interface{}, req *fasthttp.RequestCtx, pathParams KV) error
- func (paramsAPI *ParamsAPI) FasthttpBindFields(fields []reflect.Value, req *fasthttp.RequestCtx, pathParams KV) (err error)
- func (paramsAPI *ParamsAPI) FasthttpBindNew(req *fasthttp.RequestCtx, pathParams KV) (interface{}, error)
- func (paramsAPI *ParamsAPI) FasthttpRawBind(req *fasthttp.RequestCtx, pathParams KV) (interface{}, error)
- func (paramsAPI *ParamsAPI) MaxMemory() int64
- func (paramsAPI *ParamsAPI) Name() string
- func (paramsAPI *ParamsAPI) NewReceiver() (interface{}, []reflect.Value)
- func (paramsAPI *ParamsAPI) Number() int
- func (paramsAPI *ParamsAPI) Params() []*Param
- func (paramsAPI *ParamsAPI) Raw() interface{}
- func (paramsAPI *ParamsAPI) RawBind(req *http.Request, pathParams KV) (interface{}, error)
- func (paramsAPI *ParamsAPI) SetMaxMemory(maxMemory int64)
- type PathDecodeFunc
- type Schema
- type ValidationError
Constants ¶
const ( ValidationErrorValueNotSet = (1<<16 + iota) ValidationErrorValueTooSmall ValidationErrorValueTooBig ValidationErrorValueTooShort ValidationErrorValueTooLong ValidationErrorValueNotMatch )
const ( TAG_PARAM = "param" //request param tag name TAG_REGEXP = "regexp" //regexp validate tag name(optio) TAG_ERR = "err" //the custom error for binding or validating TAG_IGNORE_PARAM = "-" //ignore request param tag value MB = 1 << 20 // 1MB )
Variables ¶
Functions ¶
func Bind ¶
Bind binds the net/http request params to the `structPointer` param and validate it. note: structPointer must be struct pointer.
func BindByName ¶
BindByName binds the net/http request params to a new struct and validate it.
func ConvertAssign ¶
Type conversions for request params.
ConvertAssign copies to dest the value in src, converting it if possible. An error is returned if the copy would result in loss of information. dest should be a pointer type.
func FasthttpBind ¶
func FasthttpBind( structPointer interface{}, req *fasthttp.RequestCtx, pathParams KV, ) error
FasthttpBind binds the net/http request params to the `structPointer` param and validate it. note: structPointer must be struct pointer.
func FasthttpBindByName ¶
func FasthttpBindByName( paramsAPIName string, req *fasthttp.RequestCtx, pathParams KV, ) ( interface{}, error, )
FasthttpBindByName binds the net/http request params to a new struct and validate it.
func NewValidationError ¶
NewValidationError returns a new validation error with the specified id and text. The id's purpose is to distinguish different validation error types. Built-in validation error ids start at 65536, so you should keep your custom ids under that value.
func Register ¶
func Register( structPointer interface{}, paramNameFunc ParamNameFunc, bodyDecodeFunc BodyDecodeFunc, ) error
Register is similar to a `NewParamsAPI`, but only return error. Parse and store the struct object, requires a struct pointer, if `paramNameFunc` is nil, `paramNameFunc=toSnake`, if `bodyDecodeFunc` is nil, `bodyDecodeFunc=bodyJONS`,
Types ¶
type Apiware ¶
type Apiware struct { ParamNameFunc PathDecodeFunc BodyDecodeFunc }
func New ¶
func New(pathDecodeFunc PathDecodeFunc, bodyDecodeFunc BodyDecodeFunc, paramNameFunc ParamNameFunc) *Apiware
Create a new apiware engine. Parse and store the struct object, requires a struct pointer, if `paramNameFunc` is nil, `paramNameFunc=toSnake`, if `bodyDecodeFunc` is nil, `bodyDecodeFunc=bodyJONS`,
func (*Apiware) Bind ¶
Bind the net/http request params to the structure and validate. note: structPointer must be structure pointer.
func (*Apiware) FasthttpBind ¶
func (a *Apiware) FasthttpBind(structPointer interface{}, reqCtx *fasthttp.RequestCtx, pattern string) (err error)
FasthttpBind the fasthttp request params to the structure and validate. note: structPointer must be structure pointer.
type BodyDecodeFunc ¶
Decode params from request body
type Error ¶
type Param ¶
type Param struct {
// contains filtered or unexported fields
}
use the struct field to define a request parameter model
func (*Param) Description ¶
Description gets the description value for the param
func (*Param) IsRequired ¶
IsRequired tests if the param is declared
type ParamNameFunc ¶
Create param name from struct param name
type ParamsAPI ¶
type ParamsAPI struct {
// contains filtered or unexported fields
}
ParamsAPI defines a parameter model for an web api.
func GetParamsAPI ¶
GetParamsAPI gets the `*ParamsAPI` object according to the type name
func NewParamsAPI ¶
func NewParamsAPI( structPointer interface{}, paramNameFunc ParamNameFunc, bodyDecodeFunc BodyDecodeFunc, ) ( *ParamsAPI, error, )
NewParamsAPI parses and store the struct object, requires a struct pointer, if `paramNameFunc` is nil, `paramNameFunc=toSnake`, if `bodyDecodeFunc` is nil, `bodyDecodeFunc=bodyJONS`,
func (*ParamsAPI) BindAt ¶
func (paramsAPI *ParamsAPI) BindAt( structPointer interface{}, req *http.Request, pathParams KV, ) error
BindAt binds the net/http request params to a struct pointer and validate it. note: structPointer must be struct pointer.
func (*ParamsAPI) BindFields ¶
func (paramsAPI *ParamsAPI) BindFields( fields []reflect.Value, req *http.Request, pathParams KV, ) ( err error, )
BindFields binds the net/http request params to a struct and validate it. Must ensure that the param `fields` matches `paramsAPI.params`.
func (*ParamsAPI) BindNew ¶
BindNew binds the net/http request params to a struct pointer and validate it.
func (*ParamsAPI) FasthttpBindAt ¶
func (paramsAPI *ParamsAPI) FasthttpBindAt( structPointer interface{}, req *fasthttp.RequestCtx, pathParams KV, ) error
FasthttpBindAt binds the net/http request params to a struct pointer and validate it. note: structPointer must be struct pointer.
func (*ParamsAPI) FasthttpBindFields ¶
func (paramsAPI *ParamsAPI) FasthttpBindFields( fields []reflect.Value, req *fasthttp.RequestCtx, pathParams KV, ) ( err error, )
FasthttpBindFields binds the net/http request params to a struct and validate it. Must ensure that the param `fields` matches `paramsAPI.params`.
func (*ParamsAPI) FasthttpBindNew ¶
func (paramsAPI *ParamsAPI) FasthttpBindNew( req *fasthttp.RequestCtx, pathParams KV, ) ( interface{}, error, )
FasthttpBindNew binds the net/http request params to a struct pointer and validate it.
func (*ParamsAPI) FasthttpRawBind ¶
func (paramsAPI *ParamsAPI) FasthttpRawBind( req *fasthttp.RequestCtx, pathParams KV, ) ( interface{}, error, )
RawBind binds the net/http request params to the original struct pointer and validate it.
func (*ParamsAPI) MaxMemory ¶
MaxMemory gets maxMemory when request Content-Type is multipart/form-data, the max memory for body.
func (*ParamsAPI) NewReceiver ¶
NewReceiver creates a new struct pointer and the field's values for its receive parameterste it.
func (*ParamsAPI) Raw ¶
func (paramsAPI *ParamsAPI) Raw() interface{}
Raw returns the ParamsAPI's original value
func (*ParamsAPI) RawBind ¶
RawBind binds the net/http request params to the original struct pointer and validate it.
func (*ParamsAPI) SetMaxMemory ¶
SetMaxMemory sets maxMemory for the request which Content-Type is multipart/form-data.
type PathDecodeFunc ¶
Parse path params function, return pathParams of KV type
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
Validation error type
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Field ¶
func (e *ValidationError) Field() string
func (*ValidationError) Kind ¶
func (e *ValidationError) Kind() int