diff --git a/g/container/gmap/int_interface_map.go b/g/container/gmap/int_interface_map.go index 5e6ae20b9..a5d81e551 100644 --- a/g/container/gmap/int_interface_map.go +++ b/g/container/gmap/int_interface_map.go @@ -8,6 +8,7 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" ) type IntInterfaceMap struct { @@ -56,39 +57,28 @@ func (this *IntInterfaceMap) Get(key int) (interface{}) { return val } +func (this *IntInterfaceMap) GetBool(key int) bool { + return gconv.Bool(this.Get(key)) +} + func (this *IntInterfaceMap) GetInt(key int) int { - if r := this.Get(key); r != nil { - return r.(int) - } - return 0 + return gconv.Int(this.Get(key)) } func (this *IntInterfaceMap) GetUint (key int) uint { - if r := this.Get(key); r != nil { - return r.(uint) - } - return 0 + return gconv.Uint(this.Get(key)) } func (this *IntInterfaceMap) GetFloat32 (key int) float32 { - if r := this.Get(key); r != nil { - return r.(float32) - } - return 0 + return gconv.Float32(this.Get(key)) } func (this *IntInterfaceMap) GetFloat64 (key int) float64 { - if r := this.Get(key); r != nil { - return r.(float64) - } - return 0 + return gconv.Float64(this.Get(key)) } func (this *IntInterfaceMap) GetString (key int) string { - if r := this.Get(key); r != nil { - return r.(string) - } - return "" + return gconv.String(this.Get(key)) } // 删除键值对 diff --git a/g/container/gmap/interface_interface_map.go b/g/container/gmap/interface_interface_map.go index c28904fdc..e2e0b3cc3 100644 --- a/g/container/gmap/interface_interface_map.go +++ b/g/container/gmap/interface_interface_map.go @@ -8,6 +8,7 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" ) type InterfaceInterfaceMap struct { @@ -56,39 +57,28 @@ func (this *InterfaceInterfaceMap) Get(key interface{}) (interface{}) { return val } +func (this *InterfaceInterfaceMap) GetBool(key interface{}) bool { + return gconv.Bool(this.Get(key)) +} + func (this *InterfaceInterfaceMap) GetInt(key interface{}) int { - if r := this.Get(key); r != nil { - return r.(int) - } - return 0 + return gconv.Int(this.Get(key)) } func (this *InterfaceInterfaceMap) GetUint (key interface{}) uint { - if r := this.Get(key); r != nil { - return r.(uint) - } - return 0 + return gconv.Uint(this.Get(key)) } func (this *InterfaceInterfaceMap) GetFloat32 (key interface{}) float32 { - if r := this.Get(key); r != nil { - return r.(float32) - } - return 0 + return gconv.Float32(this.Get(key)) } func (this *InterfaceInterfaceMap) GetFloat64 (key interface{}) float64 { - if r := this.Get(key); r != nil { - return r.(float64) - } - return 0 + return gconv.Float64(this.Get(key)) } func (this *InterfaceInterfaceMap) GetString (key interface{}) string { - if r := this.Get(key); r != nil { - return r.(string) - } - return "" + return gconv.String(this.Get(key)) } // 删除键值对 diff --git a/g/container/gmap/string_interface_map.go b/g/container/gmap/string_interface_map.go index 8e5d76d41..5fb60bf1d 100644 --- a/g/container/gmap/string_interface_map.go +++ b/g/container/gmap/string_interface_map.go @@ -8,6 +8,7 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" ) type StringInterfaceMap struct { @@ -56,39 +57,28 @@ func (this *StringInterfaceMap) Get(key string) interface{} { return val } +func (this *StringInterfaceMap) GetBool(key string) bool { + return gconv.Bool(this.Get(key)) +} + func (this *StringInterfaceMap) GetInt(key string) int { - if r := this.Get(key); r != nil { - return r.(int) - } - return 0 + return gconv.Int(this.Get(key)) } func (this *StringInterfaceMap) GetUint (key string) uint { - if r := this.Get(key); r != nil { - return r.(uint) - } - return 0 + return gconv.Uint(this.Get(key)) } func (this *StringInterfaceMap) GetFloat32 (key string) float32 { - if r := this.Get(key); r != nil { - return r.(float32) - } - return 0 + return gconv.Float32(this.Get(key)) } func (this *StringInterfaceMap) GetFloat64 (key string) float64 { - if r := this.Get(key); r != nil { - return r.(float64) - } - return 0 + return gconv.Float64(this.Get(key)) } func (this *StringInterfaceMap) GetString (key string) string { - if r := this.Get(key); r != nil { - return r.(string) - } - return "" + return gconv.String(this.Get(key)) } // 删除键值对 diff --git a/g/container/gmap/uint_interface_map.go b/g/container/gmap/uint_interface_map.go index d9319f20f..09a09748e 100644 --- a/g/container/gmap/uint_interface_map.go +++ b/g/container/gmap/uint_interface_map.go @@ -8,6 +8,7 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" ) type UintInterfaceMap struct { @@ -56,39 +57,28 @@ func (this *UintInterfaceMap) Get(key uint) (interface{}) { return val } +func (this *UintInterfaceMap) GetBool(key uint) bool { + return gconv.Bool(this.Get(key)) +} + func (this *UintInterfaceMap) GetInt(key uint) int { - if r := this.Get(key); r != nil { - return r.(int) - } - return 0 + return gconv.Int(this.Get(key)) } func (this *UintInterfaceMap) GetUint (key uint) uint { - if r := this.Get(key); r != nil { - return r.(uint) - } - return 0 + return gconv.Uint(this.Get(key)) } func (this *UintInterfaceMap) GetFloat32 (key uint) float32 { - if r := this.Get(key); r != nil { - return r.(float32) - } - return 0 + return gconv.Float32(this.Get(key)) } func (this *UintInterfaceMap) GetFloat64 (key uint) float64 { - if r := this.Get(key); r != nil { - return r.(float64) - } - return 0 + return gconv.Float64(this.Get(key)) } func (this *UintInterfaceMap) GetString (key uint) string { - if r := this.Get(key); r != nil { - return r.(string) - } - return "" + return gconv.String(this.Get(key)) } // 删除键值对 diff --git a/g/encoding/gjson/gjson.go b/g/encoding/gjson/gjson.go index bcbc96406..4e5e1d053 100644 --- a/g/encoding/gjson/gjson.go +++ b/g/encoding/gjson/gjson.go @@ -7,11 +7,11 @@ package gjson import ( - "fmt" "strings" "strconv" "io/ioutil" "encoding/json" + "gitee.com/johng/gf/g/util/gconv" ) // json解析结果存放数组 @@ -121,46 +121,28 @@ func (p *Json) GetArray(pattern string) []interface{} { // 返回指定json中的string func (p *Json) GetString(pattern string) string { - result := p.Get(pattern) - if result != nil { - if r, ok := result.(string); ok { - return r - } - } - return "" + return gconv.String(p.Get(pattern)) } -// 返回指定json中的bool +// 返回指定json中的bool(false:"", 0, false, off) func (p *Json) GetBool(pattern string) bool { - result := p.Get(pattern) - if result != nil { - str := fmt.Sprintf("%v", result) - if str != "" && str != "0" && str != "false" { - return true - } - } - return false + return gconv.Bool(p.Get(pattern)) } -// 返回指定json中的float64 -func (p *Json) GetFloat64(pattern string) float64 { - result := p.Get(pattern) - if result != nil { - if r, ok := result.(float64); ok { - return r - } - } - return 0 -} - -// 返回指定json中的float64->int func (p *Json) GetInt(pattern string) int { - return int(p.GetFloat64(pattern)) + return gconv.Int(p.Get(pattern)) } -// 返回指定json中的float64->int64 -func (p *Json) GetInt64(pattern string) int64 { - return int64(p.GetFloat64(pattern)) +func (p *Json) GetUint(pattern string) uint { + return gconv.Uint(p.Get(pattern)) +} + +func (p *Json) GetFloat32(pattern string) float32 { + return gconv.Float32(p.Get(pattern)) +} + +func (p *Json) GetFloat64(pattern string) float64 { + return gconv.Float64(p.Get(pattern)) } // 根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0" diff --git a/g/net/ghttp/http_client_request.go b/g/net/ghttp/http_client_request.go index 1c68630ef..132b6cf33 100644 --- a/g/net/ghttp/http_client_request.go +++ b/g/net/ghttp/http_client_request.go @@ -8,7 +8,7 @@ package ghttp import ( "io/ioutil" "gitee.com/johng/gf/g/encoding/gjson" - "strconv" + "gitee.com/johng/gf/g/util/gconv" ) // 获取当前请求的id @@ -28,18 +28,24 @@ func (r *ClientRequest) GetQuery(k string) []string { return nil } -// 获取指定名称的参数int类型 +func (r *ClientRequest) GetQueryBool(k string) bool { + return gconv.Bool(r.GetQueryString(k)) +} + func (r *ClientRequest) GetQueryInt(k string) int { - v := r.GetQuery(k) - if v == nil { - return -1 - } else { - if i, err := strconv.Atoi(v[0]); err != nil { - return -1 - } else { - return i - } - } + return gconv.Int(r.GetQueryString(k)) +} + +func (r *ClientRequest) GetQueryUint(k string) uint { + return gconv.Uint(r.GetQueryString(k)) +} + +func (r *ClientRequest) GetQueryFloat32(k string) float32 { + return gconv.Float32(r.GetQueryString(k)) +} + +func (r *ClientRequest) GetQueryFloat64(k string) float64 { + return gconv.Float64(r.GetQueryString(k)) } func (r *ClientRequest) GetQueryString(k string) string { @@ -52,12 +58,7 @@ func (r *ClientRequest) GetQueryString(k string) string { } func (r *ClientRequest) GetQueryArray(k string) []string { - v := r.GetQuery(k) - if v == nil { - return nil - } else { - return v - } + return r.GetQuery(k) } // 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值 @@ -82,17 +83,24 @@ func (r *ClientRequest) GetPost(k string) []string { return nil } +func (r *ClientRequest) GetPostBool(k string) bool { + return gconv.Bool(r.GetPostString(k)) +} + func (r *ClientRequest) GetPostInt(k string) int { - v := r.GetPost(k) - if v == nil { - return -1 - } else { - if i, err := strconv.Atoi(v[0]); err != nil { - return -1 - } else { - return i - } - } + return gconv.Int(r.GetPostString(k)) +} + +func (r *ClientRequest) GetPostUint(k string) uint { + return gconv.Uint(r.GetPostString(k)) +} + +func (r *ClientRequest) GetPostFloat32(k string) float32 { + return gconv.Float32(r.GetPostString(k)) +} + +func (r *ClientRequest) GetPostFloat64(k string) float64 { + return gconv.Float64(r.GetPostString(k)) } func (r *ClientRequest) GetPostString(k string) string { @@ -105,13 +113,7 @@ func (r *ClientRequest) GetPostString(k string) string { } func (r *ClientRequest) GetPostArray(k string) []string { - v := r.GetPost(k) - if v == nil { - return nil - } else { - return v - } - return nil + return r.GetPost(k) } // 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值 @@ -146,14 +148,28 @@ func (r *ClientRequest) GetRequestString(k string) string { } } +func (r *ClientRequest) GetRequestBool(k string) bool { + return gconv.Bool(r.GetRequestString(k)) +} + +func (r *ClientRequest) GetRequestInt(k string) int { + return gconv.Int(r.GetRequestString(k)) +} + +func (r *ClientRequest) GetRequestUint(k string) uint { + return gconv.Uint(r.GetRequestString(k)) +} + +func (r *ClientRequest) GetRequestFloat32(k string) float32 { + return gconv.Float32(r.GetRequestString(k)) +} + +func (r *ClientRequest) GetRequestFloat64(k string) float64 { + return gconv.Float64(r.GetRequestString(k)) +} + func (r *ClientRequest) GetRequestArray(k string) []string { - v := r.GetRequest(k) - if v == nil { - return nil - } else { - return v - } - return nil + return r.GetRequest(k) } // 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值 @@ -171,14 +187,13 @@ func (r *ClientRequest) GetRequestMap(defaultMap map[string]string) map[string]s return m } - // 获取原始请求输入字符串 func (r *ClientRequest) GetRaw() []byte { result, _ := ioutil.ReadAll(r.Body) return result } -// 获取原始请求输入字符串 +// 获取原始json请求输入字符串,并解析为json对象 func (r *ClientRequest) GetJson() *gjson.Json { data := r.GetRaw() if data != nil { diff --git a/g/util/gconv/gconv.go b/g/util/gconv/gconv.go new file mode 100644 index 000000000..cd66e852b --- /dev/null +++ b/g/util/gconv/gconv.go @@ -0,0 +1,81 @@ +// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. +// 数据基本类型强制转换 +package gconv + +import ( + "fmt" + "strconv" +) + +func String(i interface{}) string { + if i == nil { + return "" + } + if r, ok := i.(string); ok { + return r + } else { + return fmt.Sprintf("%v", i) + } +} + +//false: "", 0, false, off +func Bool(i interface{}) bool { + if i == nil { + return false + } + if v, ok := i.(bool); ok { + return v + } + if s := String(i); s != "" && s != "0" && s != "false" && s != "off" { + return true + } + return false +} + +func Int(i interface{}) int { + if i == nil { + return 0 + } + if v, ok := i.(int); ok { + return v + } + v, _ := strconv.Atoi(fmt.Sprintf("%v", i)) + return v +} + +func Uint (i interface{}) uint { + if i == nil { + return 0 + } + if v, ok := i.(uint); ok { + return v + } + v, _ := strconv.ParseUint(fmt.Sprintf("%v", i), 10, 8) + return uint(v) +} + +func Float32 (i interface{}) float32 { + if i == nil { + return 0 + } + if v, ok := i.(float32); ok { + return v + } + v, _ := strconv.ParseFloat(fmt.Sprintf("%v", i), 8) + return float32(v) +} + +func Float64 (i interface{}) float64 { + if i == nil { + return 0 + } + if v, ok := i.(float64); ok { + return v + } + v, _ := strconv.ParseFloat(fmt.Sprintf("%v", i), 8) + return v +} diff --git a/g/util/gutil/util.go b/g/util/gutil/gutil.go similarity index 99% rename from g/util/gutil/util.go rename to g/util/gutil/gutil.go index b8778430d..3f14ac771 100644 --- a/g/util/gutil/util.go +++ b/g/util/gutil/gutil.go @@ -36,4 +36,3 @@ func IsLetterUpper(b byte) bool { } return false } -