-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathextend.go
77 lines (62 loc) · 1.77 KB
/
extend.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
65
66
67
68
69
70
71
72
73
74
75
76
77
package rux
import (
"bytes"
"errors"
"io"
)
const (
// ContentType header key
ContentType = "Content-Type"
// ContentBinary represents content type application/octet-stream
ContentBinary = "application/octet-stream"
// ContentDisposition describes contentDisposition
ContentDisposition = "Content-Disposition"
// describes content disposition type
dispositionInline = "inline"
// describes content disposition type
dispositionAttachment = "attachment"
)
/*************************************************************
* Extends interfaces definition
*************************************************************/
// Binder interface
type Binder interface {
Bind(i interface{}, c *Context) error
}
// Renderer interface
type Renderer interface {
Render(io.Writer, string, interface{}, *Context) error
}
// Validator interface
type Validator interface {
Validate(i interface{}) error
}
/*************************************************************
* Context function extends
*************************************************************/
// Bind context bind struct
func (c *Context) Bind(i interface{}) error {
if c.router.Binder == nil {
return errors.New("binder not registered")
}
return c.router.Binder.Bind(i, c)
}
// Render context template
func (c *Context) Render(status int, name string, data interface{}) (err error) {
if c.router.Renderer == nil {
return errors.New("renderer not registered")
}
var buf = new(bytes.Buffer)
if err = c.router.Renderer.Render(buf, name, data, c); err != nil {
return err
}
c.HTML(status, buf.Bytes())
return
}
// Validate context validator
func (c *Context) Validate(i interface{}) error {
if c.Router().Validator == nil {
return errors.New("validator not registered")
}
return c.Router().Validator.Validate(i)
}