2014-08-29 21:49:50 +04:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-03 21:19:06 +04:00
|
|
|
package binding
|
|
|
|
|
2019-04-17 17:10:21 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-31 18:51:10 +03:00
|
|
|
|
2019-04-17 17:10:21 +03:00
|
|
|
"github.com/gin-gonic/gin/binding/common"
|
2014-07-03 21:19:06 +04:00
|
|
|
)
|
|
|
|
|
2018-03-29 09:33:07 +03:00
|
|
|
// These implement the Binding interface and can be used to bind the data
|
|
|
|
// present in the request to struct instances.
|
2014-07-03 21:19:06 +04:00
|
|
|
var (
|
2015-07-03 05:20:00 +03:00
|
|
|
Form = formBinding{}
|
2017-07-19 10:50:05 +03:00
|
|
|
Query = queryBinding{}
|
2015-07-03 05:20:00 +03:00
|
|
|
FormPost = formPostBinding{}
|
2018-11-22 04:29:48 +03:00
|
|
|
Uri = uriBinding{}
|
2019-04-17 17:10:21 +03:00
|
|
|
FormMultipart = formMultipartBinding{}
|
2014-07-03 21:19:06 +04:00
|
|
|
)
|
|
|
|
|
2018-03-29 09:33:07 +03:00
|
|
|
// Default returns the appropriate Binding instance based on the HTTP method
|
|
|
|
// and the content type.
|
2019-04-17 17:10:21 +03:00
|
|
|
func Default(method, contentType string) common.Binding {
|
2015-03-31 18:51:10 +03:00
|
|
|
if method == "GET" {
|
2015-05-05 16:06:38 +03:00
|
|
|
return Form
|
2017-06-13 05:36:37 +03:00
|
|
|
}
|
|
|
|
switch contentType {
|
2019-04-17 17:10:21 +03:00
|
|
|
case common.MIMEMultipartPOSTForm:
|
2019-03-18 05:16:34 +03:00
|
|
|
return FormMultipart
|
2019-04-17 17:10:21 +03:00
|
|
|
default:
|
|
|
|
b, ok := common.List[contentType]
|
|
|
|
if !ok {
|
|
|
|
return Form //Default to Form
|
|
|
|
}
|
|
|
|
return b
|
2014-07-05 01:28:50 +04:00
|
|
|
}
|
2014-07-03 21:19:06 +04:00
|
|
|
}
|
2015-04-09 13:15:02 +03:00
|
|
|
|
2019-04-17 17:10:21 +03:00
|
|
|
//YAML return the binding for yaml if loaded
|
|
|
|
func YAML() common.BindingBody {
|
|
|
|
return retBinding(common.MIMEYAML)
|
|
|
|
}
|
|
|
|
|
|
|
|
//JSON return the binding for json if loaded
|
|
|
|
func JSON() common.BindingBody {
|
|
|
|
return retBinding(common.MIMEJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
//XML return the binding for xml if loaded
|
|
|
|
func XML() common.BindingBody {
|
|
|
|
return retBinding(common.MIMEXML)
|
|
|
|
}
|
|
|
|
|
|
|
|
//ProtoBuf return the binding for ProtoBuf if loaded
|
|
|
|
func ProtoBuf() common.BindingBody {
|
|
|
|
return retBinding(common.MIMEPROTOBUF)
|
|
|
|
}
|
|
|
|
|
|
|
|
//MsgPack return the binding for MsgPack if loaded
|
|
|
|
func MsgPack() common.BindingBody {
|
|
|
|
return retBinding(common.MIMEMSGPACK)
|
|
|
|
}
|
|
|
|
|
|
|
|
//retBinding Search for a render and panic on not found
|
|
|
|
func retBinding(contentType string) common.BindingBody {
|
|
|
|
b, ok := common.List[contentType]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("Undefined binding %s", contentType))
|
2015-05-29 21:34:41 +03:00
|
|
|
}
|
2019-04-17 17:10:21 +03:00
|
|
|
return b
|
2015-05-29 21:34:41 +03:00
|
|
|
}
|