Skip to content
Myr

form.go

This file

Path

binding/form.go

64 lines · 1.3 KiB · Go

Last changed by

ci(golangci-lint): update configuration and fix lint issues (#4247)Flc゛before adoptionc4287b1

History of this file

Take it

1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package binding67import (8	"errors"9	"net/http"10)1112const defaultMemory = 32 << 201314type (15	formBinding          struct{}16	formPostBinding      struct{}17	formMultipartBinding struct{}18)1920func (formBinding) Name() string {21	return "form"22}2324func (formBinding) Bind(req *http.Request, obj any) error {25	if err := req.ParseForm(); err != nil {26		return err27	}28	if err := req.ParseMultipartForm(defaultMemory); err != nil && !errors.Is(err, http.ErrNotMultipart) {29		return err30	}31	if err := mapForm(obj, req.Form); err != nil {32		return err33	}34	return validate(obj)35}3637func (formPostBinding) Name() string {38	return "form-urlencoded"39}4041func (formPostBinding) Bind(req *http.Request, obj any) error {42	if err := req.ParseForm(); err != nil {43		return err44	}45	if err := mapForm(obj, req.PostForm); err != nil {46		return err47	}48	return validate(obj)49}5051func (formMultipartBinding) Name() string {52	return "multipart/form-data"53}5455func (formMultipartBinding) Bind(req *http.Request, obj any) error {56	if err := req.ParseMultipartForm(defaultMemory); err != nil {57		return err58	}59	if err := mappingByPtr(obj, (*multipartRequest)(req), "form"); err != nil {60		return err61	}6263	return validate(obj)64}