binding_nomsgpack.go (3618B)
1 // Copyright 2020 Gin Core Team. All rights reserved. 2 // Use of this source code is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 //go:build nomsgpack 6 7 package binding 8 9 import "net/http" 10 11 // Content-Type MIME of the most common data formats. 12 const ( 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 MIMETOML = "application/toml" 23 ) 24 25 // Binding describes the interface which needs to be implemented for binding the 26 // data present in the request such as JSON request body, query parameters or 27 // the form POST. 28 type Binding interface { 29 Name() string 30 Bind(*http.Request, any) error 31 } 32 33 // BindingBody adds BindBody method to Binding. BindBody is similar with Bind, 34 // but it reads the body from supplied bytes instead of req.Body. 35 type BindingBody interface { 36 Binding 37 BindBody([]byte, any) error 38 } 39 40 // BindingUri adds BindUri method to Binding. BindUri is similar with Bind, 41 // but it reads the Params. 42 type BindingUri interface { 43 Name() string 44 BindUri(map[string][]string, any) error 45 } 46 47 // StructValidator is the minimal interface which needs to be implemented in 48 // order for it to be used as the validator engine for ensuring the correctness 49 // of the request. Gin provides a default implementation for this using 50 // https://github.com/go-playground/validator/tree/v10.6.1. 51 type StructValidator interface { 52 // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right. 53 // If the received type is not a struct, any validation should be skipped and nil must be returned. 54 // If the received type is a struct or pointer to a struct, the validation should be performed. 55 // If the struct is not valid or the validation itself fails, a descriptive error should be returned. 56 // Otherwise nil must be returned. 57 ValidateStruct(any) error 58 59 // Engine returns the underlying validator engine which powers the 60 // StructValidator implementation. 61 Engine() any 62 } 63 64 // Validator is the default validator which implements the StructValidator 65 // interface. It uses https://github.com/go-playground/validator/tree/v10.6.1 66 // under the hood. 67 var Validator StructValidator = &defaultValidator{} 68 69 // These implement the Binding interface and can be used to bind the data 70 // present in the request to struct instances. 71 var ( 72 JSON = jsonBinding{} 73 XML = xmlBinding{} 74 Form = formBinding{} 75 Query = queryBinding{} 76 FormPost = formPostBinding{} 77 FormMultipart = formMultipartBinding{} 78 ProtoBuf = protobufBinding{} 79 YAML = yamlBinding{} 80 Uri = uriBinding{} 81 Header = headerBinding{} 82 TOML = tomlBinding{} 83 ) 84 85 // Default returns the appropriate Binding instance based on the HTTP method 86 // and the content type. 87 func Default(method, contentType string) Binding { 88 if method == "GET" { 89 return Form 90 } 91 92 switch contentType { 93 case MIMEJSON: 94 return JSON 95 case MIMEXML, MIMEXML2: 96 return XML 97 case MIMEPROTOBUF: 98 return ProtoBuf 99 case MIMEYAML: 100 return YAML 101 case MIMEMultipartPOSTForm: 102 return FormMultipart 103 case MIMETOML: 104 return TOML 105 default: // case MIMEPOSTForm: 106 return Form 107 } 108 } 109 110 func validate(obj any) error { 111 if Validator == nil { 112 return nil 113 } 114 return Validator.ValidateStruct(obj) 115 }