Skip to content
Myr

binding.go

This file

Path

binding/binding.go

127 lines · 4.2 KiB · Go

Last changed by

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

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.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	MIMEMSGPACK           = "application/x-msgpack"22	MIMEMSGPACK2          = "application/msgpack"23	MIMEYAML              = "application/x-yaml"24	MIMEYAML2             = "application/yaml"25	MIMETOML              = "application/toml"26	MIMEBSON              = "application/bson"27)2829// Binding describes the interface which needs to be implemented for binding the30// data present in the request such as JSON request body, query parameters or31// the form POST.32type Binding interface {33	Name() string34	Bind(*http.Request, any) error35}3637// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,38// but it reads the body from supplied bytes instead of req.Body.39type BindingBody interface {40	Binding41	BindBody([]byte, any) error42}4344// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,45// but it reads the Params.46type BindingUri interface {47	Name() string48	BindUri(map[string][]string, any) error49}5051// StructValidator is the minimal interface which needs to be implemented in52// order for it to be used as the validator engine for ensuring the correctness53// of the request. Gin provides a default implementation for this using54// https://github.com/go-playground/validator/tree/v10.6.1.55type StructValidator interface {56	// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.57	// If the received type is a slice|array, the validation should be performed travel on every element.58	// If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned.59	// If the received type is a struct or pointer to a struct, the validation should be performed.60	// If the struct is not valid or the validation itself fails, a descriptive error should be returned.61	// Otherwise nil must be returned.62	ValidateStruct(any) error6364	// Engine returns the underlying validator engine which powers the65	// StructValidator implementation.66	Engine() any67}6869// Validator is the default validator which implements the StructValidator70// interface. It uses https://github.com/go-playground/validator/tree/v10.6.171// under the hood.72var Validator StructValidator = &defaultValidator{}7374// These implement the Binding interface and can be used to bind the data75// present in the request to struct instances.76var (77	JSON          BindingBody = jsonBinding{}78	XML           BindingBody = xmlBinding{}79	Form          Binding     = formBinding{}80	Query         Binding     = queryBinding{}81	FormPost      Binding     = formPostBinding{}82	FormMultipart Binding     = formMultipartBinding{}83	ProtoBuf      BindingBody = protobufBinding{}84	MsgPack       BindingBody = msgpackBinding{}85	YAML          BindingBody = yamlBinding{}86	Uri           BindingUri  = uriBinding{}87	Header        Binding     = headerBinding{}88	Plain         BindingBody = plainBinding{}89	TOML          BindingBody = tomlBinding{}90	BSON          BindingBody = bsonBinding{}91)9293// Default returns the appropriate Binding instance based on the HTTP method94// and the content type.95func Default(method, contentType string) Binding {96	if method == http.MethodGet {97		return Form98	}99100	switch contentType {101	case MIMEJSON:102		return JSON103	case MIMEXML, MIMEXML2:104		return XML105	case MIMEPROTOBUF:106		return ProtoBuf107	case MIMEMSGPACK, MIMEMSGPACK2:108		return MsgPack109	case MIMEYAML, MIMEYAML2:110		return YAML111	case MIMETOML:112		return TOML113	case MIMEMultipartPOSTForm:114		return FormMultipart115	case MIMEBSON:116		return BSON117	default: // case MIMEPOSTForm:118		return Form119	}120}121122func validate(obj any) error {123	if Validator == nil {124		return nil125	}126	return Validator.ValidateStruct(obj)127}