github.com/songzhibin97/gkit@v1.2.13/tools/bind/bind.go (about) 1 //go:build !nomsgpack 2 // +build !nomsgpack 3 4 package bind 5 6 import "net/http" 7 8 // Content-Type MIME of the most common data formats. 9 const ( 10 MIMEJSON = "application/json" 11 MIMEHTML = "text/html" 12 MIMEXML = "application/xml" 13 MIMEXML2 = "text/xml" 14 MIMEPlain = "text/plain" 15 MIMEPOSTForm = "application/x-www-form-urlencoded" 16 MIMEMultipartPOSTForm = "multipart/form-data" 17 MIMEPROTOBUF = "application/x-protobuf" 18 MIMEMSGPACK = "application/x-msgpack" 19 MIMEMSGPACK2 = "application/msgpack" 20 MIMEYAML = "application/x-yaml" 21 ) 22 23 // Binding describes the interface which needs to be implemented for binding the 24 // data present in the request such as JSON request body, query parameters or 25 // the form POST. 26 type Binding interface { 27 Name() string 28 Bind(*http.Request, interface{}) error 29 } 30 31 // BindingBody adds BindBody method to Binding. BindBody is similar with Bind, 32 // but it reads the body from supplied bytes instead of req.Body. 33 type BindingBody interface { 34 Binding 35 BindBody([]byte, interface{}) error 36 } 37 38 // BindingUri adds BindUri method to Binding. BindUri is similar with Bind, 39 // but it read the Params. 40 type BindingUri interface { 41 Name() string 42 BindUri(map[string][]string, interface{}) error 43 } 44 45 // StructValidator is the minimal interface which needs to be implemented in 46 // order for it to be used as the validator engine for ensuring the correctness 47 // of the request. Gin provides a default implementation for this using 48 // https://github.com/go-playground/validator/tree/v8.18.2. 49 type StructValidator interface { 50 // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right. 51 // If the received type is a slice|array, the validation should be performed travel on every element. 52 // If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned. 53 // If the received type is a struct or pointer to a struct, the validation should be performed. 54 // If the struct is not valid or the validation itself fails, a descriptive error should be returned. 55 // Otherwise nil must be returned. 56 ValidateStruct(interface{}) error 57 58 // Engine returns the underlying validator engine which powers the 59 // StructValidator implementation. 60 Engine() interface{} 61 } 62 63 // Validator is the default validator which implements the StructValidator 64 // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2 65 // under the hood. 66 var Validator StructValidator = &defaultValidator{} 67 68 // These implement the Binding interface and can be used to bind the data 69 // present in the request to struct instances. 70 var ( 71 JSON = jsonBinding{} 72 XML = xmlBinding{} 73 Form = formBinding{} 74 Query = queryBinding{} 75 FormPost = formPostBinding{} 76 FormMultipart = formMultipartBinding{} 77 ProtoBuf = protobufBinding{} 78 MsgPack = msgpackBinding{} 79 YAML = yamlBinding{} 80 Uri = uriBinding{} 81 Header = headerBinding{} 82 ) 83 84 func contentTypeSelect(contentType string) Binding { 85 switch contentType { 86 case MIMEJSON: 87 return JSON 88 case MIMEXML, MIMEXML2: 89 return XML 90 case MIMEPROTOBUF: 91 return ProtoBuf 92 case MIMEMSGPACK, MIMEMSGPACK2: 93 return MsgPack 94 case MIMEYAML: 95 return YAML 96 case MIMEMultipartPOSTForm: 97 return FormMultipart 98 default: 99 return Form 100 } 101 } 102 103 func validate(obj interface{}) error { 104 if Validator == nil { 105 return nil 106 } 107 return Validator.ValidateStruct(obj) 108 }