Skip to content

Commit d4e05e6

Browse files
authored
Merge pull request #96 from devfeel/develop
Develop - 强化Bind能力,新增PostValues接口,更正Readme部分文字描述
2 parents 650e68d + a598a24 commit d4e05e6

6 files changed

Lines changed: 64 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ import (
114114

115115
func main() {
116116
dotapp := dotweb.New()
117-
dotapp.HttpServer.GET("/hello", func(ctx *dotweb.HttpContext) {
117+
dotapp.HttpServer.GET("/hello", func(ctx dotweb.Context) error{
118118
ctx.WriteString("hello world!")
119+
return nil
119120
})
120121
dotapp.StartServer(80)
121122
}
@@ -169,13 +170,14 @@ type UserInfo struct {
169170
Sex int `form:"sex"`
170171
}
171172

172-
func(ctx *dotweb.HttpContext) TestBind{
173+
func TestBind(ctx dotweb.HttpContext) error{
173174
user := new(UserInfo)
174175
if err := ctx.Bind(user); err != nil {
175176
ctx.WriteString("err => " + err.Error())
176177
}else{
177178
ctx.WriteString("TestBind " + fmt.Sprint(user))
178179
}
180+
return nil
179181
}
180182

181183
```
@@ -281,6 +283,9 @@ redis - github.com/garyburd/redigo/redis
281283
#### <a href="https://github.com/devfeel/tokenserver" target="_blank">TokenServer</a>
282284
项目简介:token服务,提供token一致性服务以及相关的全局ID生成服务等
283285

286+
#### <a href="https://github.com/devfeel/longweb" target="_blank">LongWeb</a>
287+
项目简介:http长连接网关服务,提供Websocket及长轮询服务
288+
284289
## 贡献名单
285290
目前已经有几位朋友在为框架一起做努力,我们将在合适的时间向大家展现,谢谢他们的支持!
286291

bind.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ type (
1717
// Binder is the interface that wraps the Bind method.
1818
Binder interface {
1919
Bind(interface{}, Context) error
20+
BindJsonBody(interface{}, Context) error
2021
}
2122

2223
binder struct{}
2324
)
2425

26+
//Bind decode req.Body or form-value to struct
2527
func (b *binder) Bind(i interface{}, ctx Context) (err error) {
2628
req := ctx.Request()
2729
ctype := req.Header.Get(HeaderContentType)
@@ -50,6 +52,17 @@ func (b *binder) Bind(i interface{}, ctx Context) (err error) {
5052
return err
5153
}
5254

55+
//BindJsonBody default use json decode req.Body to struct
56+
func (b *binder) BindJsonBody(i interface{}, ctx Context) (err error) {
57+
req := ctx.Request()
58+
if req.Body == nil {
59+
err = errors.New("request body can't be empty")
60+
return err
61+
}
62+
err = json.NewDecoder(req.Body).Decode(i)
63+
return err
64+
}
65+
5366
func newBinder() *binder {
5467
return &binder{}
5568
}

context.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type (
5858
Attachment(file string, name string) error
5959
Inline(file string, name string) error
6060
Bind(i interface{}) error
61+
BindJsonBody(i interface{}) error
6162
GetRouterName(key string) string
6263
RemoteIP() string
6364
SetCookieValue(name, value string, maxAge int)
@@ -379,13 +380,17 @@ func (ctx *HttpContext) contentDisposition(file, name, dispositionType string) (
379380
return
380381
}
381382

382-
/*
383-
* 支持Json、Xml、Form提交的属性绑定
384-
*/
383+
// Bind decode req.Body or form-value to struct
385384
func (ctx *HttpContext) Bind(i interface{}) error {
386385
return ctx.httpServer.Binder().Bind(i, ctx)
387386
}
388387

388+
// BindJsonBody default use json decode req.Body to struct
389+
func (ctx *HttpContext) BindJsonBody(i interface{}) error {
390+
return ctx.httpServer.Binder().BindJsonBody(i, ctx)
391+
}
392+
393+
// GetRouterName get router name
389394
func (ctx *HttpContext) GetRouterName(key string) string {
390395
return ctx.routerParams.ByName(key)
391396
}

example/bind/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,25 @@ func GetBind(ctx dotweb.Context) error {
7373
return err
7474
}
7575

76+
func PostJsonBind(ctx dotweb.Context) error{
77+
type UserInfo struct {
78+
UserName string `json:"user"`
79+
Sex int `json:"sex"`
80+
}
81+
user := new(UserInfo)
82+
errstr := "no error"
83+
if err := ctx.BindJsonBody(user); err != nil {
84+
errstr = err.Error()
85+
} else {
86+
87+
}
88+
89+
_, err := ctx.WriteString("PostBind [" + errstr + "] " + fmt.Sprint(user))
90+
return err
91+
}
92+
7693
func InitRoute(server *dotweb.HttpServer) {
7794
server.Router().POST("/", TestBind)
7895
server.Router().GET("/getbind", GetBind)
96+
server.Router().POST("/jsonbind", PostJsonBind)
7997
}

request.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,18 @@ func (req *Request) FormFiles()(map[string]*UploadFile, error){
8585
return files, nil
8686
}
8787

88-
// FormValues 获取包括post、put和get内的值
88+
// FormValues including both the URL field's query parameters and the POST or PUT form data
8989
func (req *Request) FormValues() map[string][]string {
9090
req.parseForm()
9191
return map[string][]string(req.Form)
9292
}
9393

94+
// PostValues contains the parsed form data from POST, PATCH, or PUT body parameters
95+
func (req *Request) PostValues() map[string][]string {
96+
req.parseForm()
97+
return map[string][]string(req.PostForm)
98+
}
99+
94100
func (req *Request) parseForm() error {
95101
if strings.HasPrefix(req.QueryHeader(HeaderContentType), MIMEMultipartForm) {
96102
if err := req.ParseMultipartForm(defaultMemory); err != nil {

version.MD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
## dotweb版本记录:
22

3+
#### Version 1.4.0
4+
* 强化Bind能力,添加BindJsonBody接口,不对Content-Type进行判定,直接获取req.Body进行json序列化
5+
* 基础Bind机制:
6+
1)如果识别到Content-Type = application/json 会尝试使用json序列化到struct
7+
2)如果识别到Content-Type = application/xml 会尝试使用xml序列化到struct
8+
3)非以上类型,会默认识别form的key/value模式匹配
9+
* Context.Request()新增PostValues接口,包含 POST, PATCH, or PUT请求集合,以map[string][]string返回
10+
* 更正Readme部分文字描述
11+
* 完善example/bind
12+
* 2017-12-29 16:00:00
13+
314
#### Version 1.3.9
415
* Request新增FormFiles接口,用于支持多文件上传
516
* fixed #92 支持多文件同时上传

0 commit comments

Comments
 (0)