github.com/abemedia/go-don@v0.2.2-0.20240329015135-be88e32bb73b/encoding/form/form.go (about)

     1  // Package form provides decoding of form data.
     2  package form
     3  
     4  import (
     5  	"github.com/abemedia/go-don/decoder"
     6  	"github.com/abemedia/go-don/encoding"
     7  	"github.com/valyala/fasthttp"
     8  )
     9  
    10  var dec = decoder.New("form")
    11  
    12  func decodeForm(ctx *fasthttp.RequestCtx, v any) error {
    13  	return dec.Decode((*decoder.Args)(ctx.PostArgs()), v)
    14  }
    15  
    16  func decodeMultipartForm(ctx *fasthttp.RequestCtx, v any) error {
    17  	f, err := ctx.MultipartForm()
    18  	if err != nil {
    19  		return err
    20  	}
    21  	return dec.Decode(decoder.Map(f.Value), v)
    22  }
    23  
    24  func init() {
    25  	encoding.RegisterDecoder(decodeForm, "application/x-www-form-urlencoded")
    26  	encoding.RegisterDecoder(decodeMultipartForm, "multipart/form-data")
    27  }