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 fileTake it
The walk stopped after 200 commits: lines older than 5c62979 point at it.
chore: update go.mod and remove space from copyright (#3158)thinkeroubefore adoption4b68a5f
line 1why, one step back1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.bump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 2–23why, one step back2// 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"[GIN-001] - Add TOML bining for gin (#3081)Valentine Oragbakosibefore adoptioned03102
line 24why, one step back24 MIMETOML = "application/toml"bump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 25–31why, one step back25)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() stringtest support go1.18 (#2990)thinkeroubefore adoption2bde107
line 32why, one step back32 Bind(*http.Request, any) errorbump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 33–38why, one step back33}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 Bindingtest support go1.18 (#2990)thinkeroubefore adoption2bde107
line 39why, one step back39 BindBody([]byte, any) errorbump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 40–42why, one step back40}4142// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,fix: typo (#2993)jincheng9before adoption8a0f95c
line 43why, one step back43// but it reads the Params.bump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 44–45why, one step back44type BindingUri interface {45 Name() stringtest support go1.18 (#2990)thinkeroubefore adoption2bde107
line 46why, one step back46 BindUri(map[string][]string, any) errorbump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 47–59why, one step back47}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.test support go1.18 (#2990)thinkeroubefore adoption2bde107
line 60why, one step back60 ValidateStruct(any) errorbump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 61–63why, one step back6162 // Engine returns the underlying validator engine which powers the63 // StructValidator implementation.test support go1.18 (#2990)thinkeroubefore adoption2bde107
line 64why, one step back64 Engine() anybump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 65–85why, one step back65}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{}[GIN-001] - Add TOML bining for gin (#3081)Valentine Oragbakosibefore adoptioned03102
line 86why, one step back86 TOML = tomlBinding{}bump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 87–106why, one step back87)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 YAML[GIN-001] - Add TOML bining for gin (#3081)Valentine Oragbakosibefore adoptioned03102
lines 107–108why, one step back107 case MIMETOML:108 return TOMLbump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 109–115why, one step back109 case MIMEMultipartPOSTForm:110 return FormMultipart111 default: // case MIMEPOSTForm:112 return Form113 }114}115test support go1.18 (#2990)thinkeroubefore adoption2bde107
line 116why, one step back116func validate(obj any) error {bump golangci-lint version (#2858)Alexander Melentyevbefore adoption5c62979
lines 117–121why, one step back117 if Validator == nil {118 return nil119 }120 return Validator.ValidateStruct(obj)121}