// Performs validation and combines errors from validation // with errors from deserialization, then maps both the // resulting struct and the errors to the context. func validateAndMap(obj reflect.Value, ctx *macaron.Context, errors *Errors, ifacePtr ...interface{}) { ctx.Invoke(Validate(obj.Interface())) errors.Combine(getErrors(ctx)) ctx.Map(*errors) ctx.Map(obj.Elem().Interface()) if len(ifacePtr) > 0 { ctx.MapTo(obj.Elem().Interface(), ifacePtr[0]) } }
func bind(ctx *macaron.Context, obj interface{}, ifacePtr ...interface{}) { contentType := ctx.Req.Header.Get("Content-Type") if ctx.Req.Method == "POST" || ctx.Req.Method == "PUT" || len(contentType) > 0 { switch { case strings.Contains(contentType, "form-urlencoded"): ctx.Invoke(Form(obj, ifacePtr...)) case strings.Contains(contentType, "multipart/form-data"): ctx.Invoke(MultipartForm(obj, ifacePtr...)) case strings.Contains(contentType, "json"): ctx.Invoke(Json(obj, ifacePtr...)) default: var errors Errors if contentType == "" { errors.Add([]string{}, ERR_CONTENT_TYPE, "Empty Content-Type") } else { errors.Add([]string{}, ERR_CONTENT_TYPE, "Unsupported Content-Type") } ctx.Map(errors) } } else { ctx.Invoke(Form(obj, ifacePtr...)) } }