#2037 binding: add BindPlain function

Open guonaihong guonaihong
Showing 3 of 5 files from the diff.

@@ -81,6 +81,7 @@
Loading
81 81
	YAML          = yamlBinding{}
82 82
	Uri           = uriBinding{}
83 83
	Header        = headerBinding{}
84 +
	Plain         = plainBinding{}
84 85
)
85 86
86 87
// Default returns the appropriate Binding instance based on the HTTP method
@@ -103,6 +104,8 @@
Loading
103 104
		return YAML
104 105
	case MIMEMultipartPOSTForm:
105 106
		return FormMultipart
107 +
	case MIMEPlain:
108 +
		return Plain
106 109
	default: // case MIMEPOSTForm:
107 110
		return Form
108 111
	}

@@ -0,0 +1,47 @@
Loading
1 +
package binding
2 +
3 +
import (
4 +
	"fmt"
5 +
	"io/ioutil"
6 +
	"net/http"
7 +
	"reflect"
8 +
	"unsafe"
9 +
)
10 +
11 +
type plainBinding struct{}
12 +
13 +
func (plainBinding) Name() string {
14 +
	return "plain"
15 +
}
16 +
17 +
func (plainBinding) Bind(req *http.Request, obj interface{}) error {
18 +
	if obj == nil {
19 +
		return nil
20 +
	}
21 +
22 +
	v := reflect.ValueOf(obj)
23 +
24 +
	for v.Kind() == reflect.Ptr {
25 +
		if v.IsNil() {
26 +
			return nil
27 +
		}
28 +
		v = v.Elem()
29 +
	}
30 +
31 +
	all, err := ioutil.ReadAll(req.Body)
32 +
	if err != nil {
33 +
		return err
34 +
	}
35 +
36 +
	if v.Kind() == reflect.String {
37 +
		v.SetString(*(*string)(unsafe.Pointer(&all)))
38 +
		return nil
39 +
	}
40 +
41 +
	if _, ok := v.Interface().([]byte); ok {
42 +
		v.SetBytes(all)
43 +
		return nil
44 +
	}
45 +
46 +
	return fmt.Errorf("type (%T) unknown type", v)
47 +
}

@@ -624,6 +624,11 @@
Loading
624 624
	return c.MustBindWith(obj, binding.YAML)
625 625
}
626 626
627 +
// BindHeader is a shortcut for c.MustBindWith(obj, binding.Plain).
628 +
func (c *Context) BindPlain(obj interface{}) error {
629 +
	return c.MustBindWith(obj, binding.Plain)
630 +
}
631 +
627 632
// BindHeader is a shortcut for c.MustBindWith(obj, binding.Header).
628 633
func (c *Context) BindHeader(obj interface{}) error {
629 634
	return c.MustBindWith(obj, binding.Header)
@@ -683,6 +688,11 @@
Loading
683 688
	return c.ShouldBindWith(obj, binding.YAML)
684 689
}
685 690
691 +
// ShouldBindPlain is a shortcut for c.ShouldBindWith(obj, binding.Header).
692 +
func (c *Context) ShouldBindPlain(obj interface{}) error {
693 +
	return c.ShouldBindWith(obj, binding.Plain)
694 +
}
695 +
686 696
// ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, binding.Header).
687 697
func (c *Context) ShouldBindHeader(obj interface{}) error {
688 698
	return c.ShouldBindWith(obj, binding.Header)

Learn more Showing 2 files with coverage changes found.

binding/binding_nomsgpack.go
Loading file...
New file binding/plain.go
New
Loading file...
Files Coverage
binding 100.00%
render 92.96%
auth.go 100.00%
context.go 0.01% 97.52%
debug.go 92.50%
deprecated.go 100.00%
errors.go 100.00%
fs.go 100.00%
gin.go 99.01%
logger.go 100.00%
mode.go 100.00%
path.go 100.00%
recovery.go 97.18%
response_writer.go 93.33%
routergroup.go 100.00%
test_helpers.go 100.00%
tree.go 100.00%
utils.go 96.83%
Project Totals (41 files) 98.48%
Loading