-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.go
executable file
·64 lines (55 loc) · 1.68 KB
/
binding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package binding
import (
"github.com/labstack/echo/v4"
"strings"
)
type Binding interface {
Name() string
Bind(i interface{}, c echo.Context) error
}
type StructValidator interface {
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
// If the received type is not a struct, any validation should be skipped and nil must be returned.
// If the received type is a struct or pointer to a struct, the validation should be performed.
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
// Otherwise nil must be returned.
ValidateStruct(interface{}) error
}
var Validator StructValidator = &defaultValidator{}
var (
XML = xmlBinding{}
JSON = jsonBinding{}
Form = formBinding{}
FormPost = formPostBinding{}
FormMultipart = formMultipartBinding{}
)
func Default(method, contentType string) Binding {
if method == "GET" {
return Form
} else {
// POST
switch {
case strings.HasPrefix(contentType, echo.MIMEApplicationJSON):
return JSON
case strings.HasPrefix(contentType, echo.MIMEApplicationXML), strings.HasPrefix(contentType, echo.MIMETextXML):
return XML
case strings.HasPrefix(contentType, echo.MIMEApplicationForm), strings.HasPrefix(contentType, echo.MIMEMultipartForm):
return Form
default:
return Form
}
}
}
func validate(obj interface{}) error {
if Validator == nil {
return nil
}
return Validator.ValidateStruct(obj)
}
type Binder struct {
}
func (Binder) Bind(i interface{}, c echo.Context) (err error) {
b := Default(c.Request().Method, c.Request().Header.Get("Content-Type"))
err = b.Bind(i, c)
return
}