Skip to content
Myr

binding.go

This file

Path

binding/binding.go

121 lines · 3.8 KiB · Go

Last changed by

chore: support min go version 1.18 (#3511)thinkeroubefore adoption1e1f0b1

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	MIMETOML              = "application/toml"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 a slice|array, the validation should be performed travel on every element.56	// If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned.57	// If the received type is a struct or pointer to a struct, the validation should be performed.58	// If the struct is not valid or the validation itself fails, a descriptive error should be returned.59	// Otherwise nil must be returned.60	ValidateStruct(any) error6162	// Engine returns the underlying validator engine which powers the63	// StructValidator implementation.64	Engine() any65}6667// Validator is the default validator which implements the StructValidator68// interface. It uses https://github.com/go-playground/validator/tree/v10.6.169// under the hood.70var Validator StructValidator = &defaultValidator{}7172// These implement the Binding interface and can be used to bind the data73// present in the request to struct instances.74var (75	JSON          = jsonBinding{}76	XML           = xmlBinding{}77	Form          = formBinding{}78	Query         = queryBinding{}79	FormPost      = formPostBinding{}80	FormMultipart = formMultipartBinding{}81	ProtoBuf      = protobufBinding{}82	MsgPack       = msgpackBinding{}83	YAML          = yamlBinding{}84	Uri           = uriBinding{}85	Header        = headerBinding{}86	TOML          = tomlBinding{}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 == http.MethodGet {93		return Form94	}9596	switch contentType {97	case MIMEJSON:98		return JSON99	case MIMEXML, MIMEXML2:100		return XML101	case MIMEPROTOBUF:102		return ProtoBuf103	case MIMEMSGPACK, MIMEMSGPACK2:104		return MsgPack105	case MIMEYAML:106		return YAML107	case MIMETOML:108		return TOML109	case MIMEMultipartPOSTForm:110		return FormMultipart111	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}