now parse parameter only if needed by controller's method

This commit is contained in:
Adrien Marquès 2018-05-29 16:00:43 +02:00
parent 8f9c86c391
commit fdf036d366
1 changed files with 11 additions and 2 deletions

View File

@ -73,7 +73,7 @@ func (s *Server) route(res http.ResponseWriter, req *http.Request) {
---------------------------------------------------------*/ ---------------------------------------------------------*/
var paramError Err = ErrSuccess var paramError Err = ErrSuccess
for name, param := range method.Parameters { for name, param := range method.Parameters {
fmt.Printf("- %s: %v | '%v'\n", name, *param.Optional, *param.Rename) fmt.Printf("- parameter '%s'\n", name)
/* (1) Extract value */ /* (1) Extract value */
p, isset := request.Data.Set[name] p, isset := request.Data.Set[name]
@ -83,18 +83,21 @@ func (s *Server) route(res http.ResponseWriter, req *http.Request) {
// fail if required // fail if required
if !*param.Optional { if !*param.Optional {
fmt.Printf(" - required and missing\n")
paramError = ErrMissingParam paramError = ErrMissingParam
paramError.BindArgument(name) paramError.BindArgument(name)
break break
// error if default param is nil // error if default param is nil
} else if param.Default == nil { } else if param.Default == nil {
fmt.Printf(" - required and no default\n")
paramError = ErrInvalidDefaultParam paramError = ErrInvalidDefaultParam
paramError.BindArgument(name) paramError.BindArgument(name)
break break
// set default p if optional // set default p if optional
} else { } else {
fmt.Printf(" - default value: '%v'\n", *param.Default)
p = &RequestParameter{ p = &RequestParameter{
Parsed: true, Parsed: true,
Value: *param.Default, Value: *param.Default,
@ -103,8 +106,14 @@ func (s *Server) route(res http.ResponseWriter, req *http.Request) {
} }
/* (3) Check type */ /* (3) Parse parameter variable */
if !p.Parsed && !p.File {
p.Value = parseHttpData(p.Value)
}
/* (4) Check type */
isValid := s.Checker.Run(param.Type, p.Value) isValid := s.Checker.Run(param.Type, p.Value)
fmt.Printf(" - valid: %t\n", isValid == nil)
if isValid != nil { if isValid != nil {
paramError = ErrInvalidParam paramError = ErrInvalidParam
paramError.BindArgument(name) paramError.BindArgument(name)