github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/binding.go (about)

     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  //go:build !nomsgpack
     6  // +build !nomsgpack
     7  
     8  package binding
     9  
    10  import "github.com/hellobchain/newcryptosm/http"
    11  
    12  // Content-Type MIME of the most common data formats.
    13  const (
    14  	MIMEJSON              = "application/json"
    15  	MIMEHTML              = "text/html"
    16  	MIMEXML               = "application/xml"
    17  	MIMEXML2              = "text/xml"
    18  	MIMEPlain             = "text/plain"
    19  	MIMEPOSTForm          = "application/x-www-form-urlencoded"
    20  	MIMEMultipartPOSTForm = "multipart/form-data"
    21  	MIMEPROTOBUF          = "application/x-protobuf"
    22  	MIMEMSGPACK           = "application/x-msgpack"
    23  	MIMEMSGPACK2          = "application/msgpack"
    24  	MIMEYAML              = "application/x-yaml"
    25  )
    26  
    27  // Binding describes the interface which needs to be implemented for binding the
    28  // data present in the request such as JSON request body, query parameters or
    29  // the form POST.
    30  type Binding interface {
    31  	Name() string
    32  	Bind(*http.Request, interface{}) error
    33  }
    34  
    35  // BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
    36  // but it reads the body from supplied bytes instead of req.Body.
    37  type BindingBody interface {
    38  	Binding
    39  	BindBody([]byte, interface{}) error
    40  }
    41  
    42  // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
    43  // but it read the Params.
    44  type BindingUri interface {
    45  	Name() string
    46  	BindUri(map[string][]string, interface{}) error
    47  }
    48  
    49  // StructValidator is the minimal interface which needs to be implemented in
    50  // order for it to be used as the validator engine for ensuring the correctness
    51  // of the request. Gin provides a default implementation for this using
    52  // https://github.com/go-playground/validator/tree/v8.18.2.
    53  type 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(interface{}) error
    61  
    62  	// Engine returns the underlying validator engine which powers the
    63  	// StructValidator implementation.
    64  	Engine() interface{}
    65  }
    66  
    67  // Validator is the default validator which implements the StructValidator
    68  // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
    69  // under the hood.
    70  var Validator StructValidator = &defaultValidator{}
    71  
    72  // These implement the Binding interface and can be used to bind the data
    73  // present in the request to struct instances.
    74  var (
    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  )
    87  
    88  // Default returns the appropriate Binding instance based on the HTTP method
    89  // and the content type.
    90  func Default(method, contentType string) Binding {
    91  	if method == http.MethodGet {
    92  		return Form
    93  	}
    94  
    95  	switch contentType {
    96  	case MIMEJSON:
    97  		return JSON
    98  	case MIMEXML, MIMEXML2:
    99  		return XML
   100  	case MIMEPROTOBUF:
   101  		return ProtoBuf
   102  	case MIMEMSGPACK, MIMEMSGPACK2:
   103  		return MsgPack
   104  	case MIMEYAML:
   105  		return YAML
   106  	case MIMEMultipartPOSTForm:
   107  		return FormMultipart
   108  	default: // case MIMEPOSTForm:
   109  		return Form
   110  	}
   111  }
   112  
   113  func validate(obj interface{}) error {
   114  	if Validator == nil {
   115  		return nil
   116  	}
   117  	return Validator.ValidateStruct(obj)
   118  }