2017-12-31 11:09:16 +08:00
|
|
|
|
// 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.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2018-01-03 11:04:47 +08:00
|
|
|
|
// 类型转换.
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 内部使用了bytes作为底层转换类型,效率很高。
|
2017-12-31 11:09:16 +08:00
|
|
|
|
package gconv
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2018-04-15 10:30:59 +08:00
|
|
|
|
"strconv"
|
2018-07-02 14:08:15 +08:00
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"gitee.com/johng/gf/g/encoding/gbinary"
|
2018-09-18 18:57:34 +08:00
|
|
|
|
"strings"
|
2017-12-31 11:09:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2018-09-30 13:31:03 +08:00
|
|
|
|
// 将变量i转换为字符串指定的类型t,非必须参数extraParams泳衣额外的参数传递
|
|
|
|
|
|
func Convert(i interface{}, t string, extraParams...interface{}) interface{} {
|
2018-04-29 21:33:47 +08:00
|
|
|
|
switch t {
|
2018-05-01 10:09:57 +08:00
|
|
|
|
case "int": return Int(i)
|
|
|
|
|
|
case "int8": return Int8(i)
|
|
|
|
|
|
case "int16": return Int16(i)
|
|
|
|
|
|
case "int32": return Int32(i)
|
|
|
|
|
|
case "int64": return Int64(i)
|
|
|
|
|
|
case "uint": return Uint(i)
|
|
|
|
|
|
case "uint8": return Uint8(i)
|
|
|
|
|
|
case "uint16": return Uint16(i)
|
|
|
|
|
|
case "uint32": return Uint32(i)
|
|
|
|
|
|
case "uint64": return Uint64(i)
|
|
|
|
|
|
case "float32": return Float32(i)
|
|
|
|
|
|
case "float64": return Float64(i)
|
|
|
|
|
|
case "bool": return Bool(i)
|
|
|
|
|
|
case "string": return String(i)
|
|
|
|
|
|
case "[]byte": return Bytes(i)
|
2018-09-28 13:33:41 +08:00
|
|
|
|
case "[]int": return Ints(i)
|
|
|
|
|
|
case "[]string": return Strings(i)
|
2018-06-30 22:50:21 +08:00
|
|
|
|
case "time.Time":
|
2018-09-30 13:31:03 +08:00
|
|
|
|
if len(extraParams) > 0 {
|
|
|
|
|
|
return Time(i, String(extraParams[0]))
|
2018-06-30 22:50:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Time(i)
|
|
|
|
|
|
|
2018-05-01 10:09:57 +08:00
|
|
|
|
case "time.Duration": return TimeDuration(i)
|
|
|
|
|
|
default:
|
|
|
|
|
|
return i
|
2018-04-29 21:33:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-03 10:23:37 +08:00
|
|
|
|
func Bytes(i interface{}) []byte {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if r, ok := i.([]byte); ok {
|
|
|
|
|
|
return r
|
|
|
|
|
|
} else {
|
2018-04-14 01:05:46 +08:00
|
|
|
|
return gbinary.Encode(i)
|
2018-01-03 10:23:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 基础的字符串类型转换
|
2017-12-31 11:09:16 +08:00
|
|
|
|
func String(i interface{}) string {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
switch value := i.(type) {
|
|
|
|
|
|
case int: return strconv.Itoa(value)
|
|
|
|
|
|
case int8: return strconv.Itoa(int(value))
|
|
|
|
|
|
case int16: return strconv.Itoa(int(value))
|
|
|
|
|
|
case int32: return strconv.Itoa(int(value))
|
|
|
|
|
|
case int64: return strconv.Itoa(int(value))
|
|
|
|
|
|
case uint: return strconv.FormatUint(uint64(value), 10)
|
|
|
|
|
|
case uint8: return strconv.FormatUint(uint64(value), 10)
|
|
|
|
|
|
case uint16: return strconv.FormatUint(uint64(value), 10)
|
|
|
|
|
|
case uint32: return strconv.FormatUint(uint64(value), 10)
|
|
|
|
|
|
case uint64: return strconv.FormatUint(uint64(value), 10)
|
2018-06-27 20:58:28 +08:00
|
|
|
|
case float32: return strconv.FormatFloat(float64(value), 'f', -1, 32)
|
2018-05-03 18:30:16 +08:00
|
|
|
|
case float64: return strconv.FormatFloat(value, 'f', -1, 64)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
case bool: return strconv.FormatBool(value)
|
|
|
|
|
|
case string: return value
|
|
|
|
|
|
case []byte: return string(value)
|
|
|
|
|
|
default:
|
2018-07-01 19:13:25 +08:00
|
|
|
|
// 默认使用json进行字符串转换
|
|
|
|
|
|
jsonContent, _ := json.Marshal(value)
|
|
|
|
|
|
return string(jsonContent)
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//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
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
switch value := i.(type) {
|
|
|
|
|
|
case int: return value
|
|
|
|
|
|
case int8: return int(value)
|
|
|
|
|
|
case int16: return int(value)
|
|
|
|
|
|
case int32: return int(value)
|
|
|
|
|
|
case int64: return int(value)
|
|
|
|
|
|
case uint: return int(value)
|
|
|
|
|
|
case uint8: return int(value)
|
|
|
|
|
|
case uint16: return int(value)
|
|
|
|
|
|
case uint32: return int(value)
|
|
|
|
|
|
case uint64: return int(value)
|
2018-05-03 18:30:16 +08:00
|
|
|
|
case float32: return int(value)
|
|
|
|
|
|
case float64: return int(value)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
case bool:
|
|
|
|
|
|
if value {
|
|
|
|
|
|
return 1
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0
|
|
|
|
|
|
default:
|
2018-09-18 18:57:34 +08:00
|
|
|
|
v, _ := strconv.Atoi(strings.TrimSpace(String(value)))
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return v
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
func Int8(i interface{}) int8 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(int8); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return int8(Int(i))
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Int16(i interface{}) int16 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(int16); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return int16(Int(i))
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Int32(i interface{}) int32 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(int32); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return int32(Int(i))
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Int64(i interface{}) int64 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(int64); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return int64(Int(i))
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Uint(i interface{}) uint {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
switch value := i.(type) {
|
2018-10-13 20:29:27 +08:00
|
|
|
|
case int:
|
|
|
|
|
|
if value < 0 {
|
|
|
|
|
|
value = -value
|
|
|
|
|
|
}
|
|
|
|
|
|
return uint(value)
|
|
|
|
|
|
case int8:
|
|
|
|
|
|
if value < 0 {
|
|
|
|
|
|
value = -value
|
|
|
|
|
|
}
|
|
|
|
|
|
return uint(value)
|
|
|
|
|
|
case int16:
|
|
|
|
|
|
if value < 0 {
|
|
|
|
|
|
value = -value
|
|
|
|
|
|
}
|
|
|
|
|
|
return uint(value)
|
|
|
|
|
|
case int32:
|
|
|
|
|
|
if value < 0 {
|
|
|
|
|
|
value = -value
|
|
|
|
|
|
}
|
|
|
|
|
|
return uint(value)
|
|
|
|
|
|
case int64:
|
|
|
|
|
|
if value < 0 {
|
|
|
|
|
|
value = -value
|
|
|
|
|
|
}
|
|
|
|
|
|
return uint(value)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
case uint: return value
|
|
|
|
|
|
case uint8: return uint(value)
|
|
|
|
|
|
case uint16: return uint(value)
|
|
|
|
|
|
case uint32: return uint(value)
|
|
|
|
|
|
case uint64: return uint(value)
|
2018-05-03 18:30:16 +08:00
|
|
|
|
case float32: return uint(value)
|
|
|
|
|
|
case float64: return uint(value)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
case bool:
|
|
|
|
|
|
if value {
|
|
|
|
|
|
return 1
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0
|
|
|
|
|
|
default:
|
2018-09-18 18:57:34 +08:00
|
|
|
|
v, _ := strconv.ParseUint(strings.TrimSpace(String(value)), 10, 64)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return uint(v)
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Uint8(i interface{}) uint8 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(uint8); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return uint8(Uint(i))
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Uint16(i interface{}) uint16 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(uint16); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return uint16(Uint(i))
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Uint32(i interface{}) uint32 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(uint32); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return uint32(Uint(i))
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Uint64(i interface{}) uint64 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(uint64); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return uint64(Uint(i))
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Float32 (i interface{}) float32 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(float32); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-09-18 18:57:34 +08:00
|
|
|
|
v, _ := strconv.ParseFloat(strings.TrimSpace(String(i)), 64)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return float32(v)
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Float64 (i interface{}) float64 {
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := i.(float64); ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
2018-09-18 18:57:34 +08:00
|
|
|
|
v, _ := strconv.ParseFloat(strings.TrimSpace(String(i)), 64)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return v
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
|