|
| 1 | +// Copyright 2014 Manu Martinez-Almeida. 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 | +// +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 | +) |
| 23 | + |
| 24 | +// Binding describes the interface which needs to be implemented for binding the |
| 25 | +// data present in the request such as JSON request body, query parameters or |
| 26 | +// the form POST. |
| 27 | +type Binding interface { |
| 28 | + Name() string |
| 29 | + Bind(*http.Request, interface{}) error |
| 30 | +} |
| 31 | + |
| 32 | +// BindingBody adds BindBody method to Binding. BindBody is similar with Bind, |
| 33 | +// but it reads the body from supplied bytes instead of req.Body. |
| 34 | +type BindingBody interface { |
| 35 | + Binding |
| 36 | + BindBody([]byte, interface{}) error |
| 37 | +} |
| 38 | + |
| 39 | +// BindingUri adds BindUri method to Binding. BindUri is similar with Bind, |
| 40 | +// but it read the Params. |
| 41 | +type BindingUri interface { |
| 42 | + Name() string |
| 43 | + BindUri(map[string][]string, interface{}) error |
| 44 | +} |
| 45 | + |
| 46 | +// StructValidator is the minimal interface which needs to be implemented in |
| 47 | +// order for it to be used as the validator engine for ensuring the correctness |
| 48 | +// of the request. Gin provides a default implementation for this using |
| 49 | +// https://github.com/go-playground/validator/tree/v8.18.2. |
| 50 | +type StructValidator interface { |
| 51 | + // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right. |
| 52 | + // If the received type is not a struct, 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 | + YAML = yamlBinding{} |
| 79 | + Uri = uriBinding{} |
| 80 | + Header = headerBinding{} |
| 81 | +) |
| 82 | + |
| 83 | +// Default returns the appropriate Binding instance based on the HTTP method |
| 84 | +// and the content type. |
| 85 | +func Default(method, contentType string) Binding { |
| 86 | + if method == "GET" { |
| 87 | + return Form |
| 88 | + } |
| 89 | + |
| 90 | + switch contentType { |
| 91 | + case MIMEJSON: |
| 92 | + return JSON |
| 93 | + case MIMEXML, MIMEXML2: |
| 94 | + return XML |
| 95 | + case MIMEPROTOBUF: |
| 96 | + return ProtoBuf |
| 97 | + case MIMEYAML: |
| 98 | + return YAML |
| 99 | + case MIMEMultipartPOSTForm: |
| 100 | + return FormMultipart |
| 101 | + default: // case MIMEPOSTForm: |
| 102 | + return Form |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func validate(obj interface{}) error { |
| 107 | + if Validator == nil { |
| 108 | + return nil |
| 109 | + } |
| 110 | + return Validator.ValidateStruct(obj) |
| 111 | +} |
0 commit comments