Skip to content
Myr

binding_nomsgpack.go

This file

Path

binding/binding_nomsgpack.go

121 lines · 3.7 KiB · Go

Last changed by

feat(render): add bson protocol (#4145)Laurent Caumontbefore adoptiond7776de

History of this file

Take it

1// Copyright 2020 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build nomsgpack67package binding89import "net/http"1011// Content-Type MIME of the most common data formats.12const (13	MIMEJSON              = "application/json"14	MIMEHTML              = "text/html"15	MIMEXML               = "application/xml"16	MIMEXML2              = "text/xml"17	MIMEPlain             = "text/plain"18	MIMEPOSTForm          = "application/x-www-form-urlencoded"19	MIMEMultipartPOSTForm = "multipart/form-data"20	MIMEPROTOBUF          = "application/x-protobuf"21	MIMEYAML              = "application/x-yaml"22	MIMEYAML2             = "application/yaml"23	MIMETOML              = "application/toml"24	MIMEBSON              = "application/bson"25)2627// Binding describes the interface which needs to be implemented for binding the28// data present in the request such as JSON request body, query parameters or29// the form POST.30type Binding interface {31	Name() string32	Bind(*http.Request, any) error33}3435// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,36// but it reads the body from supplied bytes instead of req.Body.37type BindingBody interface {38	Binding39	BindBody([]byte, any) error40}4142// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,43// but it reads the Params.44type BindingUri interface {45	Name() string46	BindUri(map[string][]string, any) error47}4849// StructValidator is the minimal interface which needs to be implemented in50// order for it to be used as the validator engine for ensuring the correctness51// of the request. Gin provides a default implementation for this using52// https://github.com/go-playground/validator/tree/v10.6.1.53type StructValidator interface {54	// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.55	// If the received type is not a struct, any validation should be skipped and nil must be returned.56	// If the received type is a struct or pointer to a struct, the validation should be performed.57	// If the struct is not valid or the validation itself fails, a descriptive error should be returned.58	// Otherwise nil must be returned.59	ValidateStruct(any) error6061	// Engine returns the underlying validator engine which powers the62	// StructValidator implementation.63	Engine() any64}6566// Validator is the default validator which implements the StructValidator67// interface. It uses https://github.com/go-playground/validator/tree/v10.6.168// under the hood.69var Validator StructValidator = &defaultValidator{}7071// These implement the Binding interface and can be used to bind the data72// present in the request to struct instances.73var (74	JSON          = jsonBinding{}75	XML           = xmlBinding{}76	Form          = formBinding{}77	Query         = queryBinding{}78	FormPost      = formPostBinding{}79	FormMultipart = formMultipartBinding{}80	ProtoBuf      = protobufBinding{}81	YAML          = yamlBinding{}82	Uri           = uriBinding{}83	Header        = headerBinding{}84	TOML          = tomlBinding{}85	Plain         = plainBinding{}86	BSON          BindingBody = bsonBinding{}87)8889// Default returns the appropriate Binding instance based on the HTTP method90// and the content type.91func Default(method, contentType string) Binding {92	if method == "GET" {93		return Form94	}9596	switch contentType {97	case MIMEJSON:98		return JSON99	case MIMEXML, MIMEXML2:100		return XML101	case MIMEPROTOBUF:102		return ProtoBuf103	case MIMEYAML, MIMEYAML2:104		return YAML105	case MIMEMultipartPOSTForm:106		return FormMultipart107	case MIMETOML:108		return TOML109	case MIMEBSON:110		return BSON111	default: // case MIMEPOSTForm:112		return Form113	}114}115116func validate(obj any) error {117	if Validator == nil {118		return nil119	}120	return Validator.ValidateStruct(obj)121}