gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

form.go (1346B)


      1 // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
      2 // Use of this source code is governed by a MIT style
      3 // license that can be found in the LICENSE file.
      4 
      5 package binding
      6 
      7 import (
      8 	"errors"
      9 	"net/http"
     10 )
     11 
     12 const defaultMemory = 32 << 20
     13 
     14 type formBinding struct{}
     15 type formPostBinding struct{}
     16 type formMultipartBinding struct{}
     17 
     18 func (formBinding) Name() string {
     19 	return "form"
     20 }
     21 
     22 func (formBinding) Bind(req *http.Request, obj any) error {
     23 	if err := req.ParseForm(); err != nil {
     24 		return err
     25 	}
     26 	if err := req.ParseMultipartForm(defaultMemory); err != nil && !errors.Is(err, http.ErrNotMultipart) {
     27 		return err
     28 	}
     29 	if err := mapForm(obj, req.Form); err != nil {
     30 		return err
     31 	}
     32 	return validate(obj)
     33 }
     34 
     35 func (formPostBinding) Name() string {
     36 	return "form-urlencoded"
     37 }
     38 
     39 func (formPostBinding) Bind(req *http.Request, obj any) error {
     40 	if err := req.ParseForm(); err != nil {
     41 		return err
     42 	}
     43 	if err := mapForm(obj, req.PostForm); err != nil {
     44 		return err
     45 	}
     46 	return validate(obj)
     47 }
     48 
     49 func (formMultipartBinding) Name() string {
     50 	return "multipart/form-data"
     51 }
     52 
     53 func (formMultipartBinding) Bind(req *http.Request, obj any) error {
     54 	if err := req.ParseMultipartForm(defaultMemory); err != nil {
     55 		return err
     56 	}
     57 	if err := mappingByPtr(obj, (*multipartRequest)(req), "form"); err != nil {
     58 		return err
     59 	}
     60 
     61 	return validate(obj)
     62 }