From 0092365481a018b663dba7e064c8d447887f40d9 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 3 Jan 2018 10:23:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E5=8C=85?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/frame/gmvc/controller.go | 2 +- g/net/ghttp/http_request.go | 4 ++-- g/util/gconv/gconv.go | 39 ++++++++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/g/frame/gmvc/controller.go b/g/frame/gmvc/controller.go index 8ed1fa056..912637cbc 100644 --- a/g/frame/gmvc/controller.go +++ b/g/frame/gmvc/controller.go @@ -24,9 +24,9 @@ type Controller struct { // 控制器初始化接口方法 func (c *Controller) Init(r *ghttp.Request) { - c.Server = r.Server c.Request = r c.Response = r.Response + c.Server = r.Server c.View = NewView(r.Response) c.Cookie = r.Cookie c.Session = r.Session diff --git a/g/net/ghttp/http_request.go b/g/net/ghttp/http_request.go index 9cfd0b14a..1369058f2 100644 --- a/g/net/ghttp/http_request.go +++ b/g/net/ghttp/http_request.go @@ -22,13 +22,13 @@ type Request struct { Server *Server // 请求关联的服务器对象 Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全) Session *Session // 与当前请求绑定的Session对象(并发安全) - Response *Response // 对应请求的返回数据操作对象 + Response *Response // 对应请求的返回数据操作对象 } // 获得指定名称的get参数列表 func (r *Request) GetQuery(k string) []string { if r.getvals == nil { - values := r.URL.Query() + values := r.URL.Query() r.getvals = &values } if v, ok := (*r.getvals)[k]; ok { diff --git a/g/util/gconv/gconv.go b/g/util/gconv/gconv.go index 3270745fe..390092237 100644 --- a/g/util/gconv/gconv.go +++ b/g/util/gconv/gconv.go @@ -4,7 +4,9 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -// 数据基本类型强制转换 +// 数据基本类型转换, +// 如果给定的interface{}参数不是指定转换的输出类型,那么会进行强制转换,效率会比较低, +// 建议已知类型的转换自行调用相关方法来单独处理。 package gconv import ( @@ -12,6 +14,17 @@ import ( "strconv" ) +func Bytes(i interface{}) []byte { + if i == nil { + return nil + } + if r, ok := i.([]byte); ok { + return r + } else { + return []byte(String(i)) + } +} + func String(i interface{}) string { if i == nil { return "" @@ -23,6 +36,22 @@ func String(i interface{}) string { } } +func Strings(i interface{}) []string { + if i == nil { + return nil + } + if r, ok := i.([]string); ok { + return r + } else if r, ok := i.([]interface{}); ok { + strs := make([]string, len(r)) + for k, v := range r { + strs[k] = String(v) + } + return strs + } + return []string{fmt.Sprintf("%v", i)} +} + //false: "", 0, false, off func Bool(i interface{}) bool { if i == nil { @@ -44,7 +73,7 @@ func Int(i interface{}) int { if v, ok := i.(int); ok { return v } - v, _ := strconv.Atoi(fmt.Sprintf("%v", i)) + v, _ := strconv.Atoi(String(i)) return v } @@ -55,7 +84,7 @@ func Uint (i interface{}) uint { if v, ok := i.(uint); ok { return v } - v, _ := strconv.ParseUint(fmt.Sprintf("%v", i), 10, 8) + v, _ := strconv.ParseUint(String(i), 10, 8) return uint(v) } @@ -66,7 +95,7 @@ func Float32 (i interface{}) float32 { if v, ok := i.(float32); ok { return v } - v, _ := strconv.ParseFloat(fmt.Sprintf("%v", i), 8) + v, _ := strconv.ParseFloat(String(i), 8) return float32(v) } @@ -77,6 +106,6 @@ func Float64 (i interface{}) float64 { if v, ok := i.(float64); ok { return v } - v, _ := strconv.ParseFloat(fmt.Sprintf("%v", i), 8) + v, _ := strconv.ParseFloat(String(i), 8) return v }