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

     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  // +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  	MIMEYAML              = "application/x-yaml"
    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, interface{}) 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, interface{}) error
    38  }
    39  
    40  // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
    41  // but it read the Params.
    42  type BindingUri interface {
    43  	Name() string
    44  	BindUri(map[string][]string, interface{}) 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/v8.18.2.
    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(interface{}) error
    58  
    59  	// Engine returns the underlying validator engine which powers the
    60  	// StructValidator implementation.
    61  	Engine() interface{}
    62  }
    63  
    64  // Validator is the default validator which implements the StructValidator
    65  // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
    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  )
    83  
    84  // Default returns the appropriate Binding instance based on the HTTP method
    85  // and the content type.
    86  func Default(method, contentType string) Binding {
    87  	if method == "GET" {
    88  		return Form
    89  	}
    90  
    91  	switch contentType {
    92  	case MIMEJSON:
    93  		return JSON
    94  	case MIMEXML, MIMEXML2:
    95  		return XML
    96  	case MIMEPROTOBUF:
    97  		return ProtoBuf
    98  	case MIMEYAML:
    99  		return YAML
   100  	case MIMEMultipartPOSTForm:
   101  		return FormMultipart
   102  	default: // case MIMEPOSTForm:
   103  		return Form
   104  	}
   105  }
   106  
   107  func validate(obj interface{}) error {
   108  	if Validator == nil {
   109  		return nil
   110  	}
   111  	return Validator.ValidateStruct(obj)
   112  }