1 Star 0 Fork 0

micro-tools / wf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gconv.go 14.84 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-09-27 22:16 . 升级go-ole
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
// Package gconv implements powerful and convenient converting functionality for any types of variables.
//
// This package should keep much less dependencies with other packages.
package gconv
import (
"fmt"
"gitee.com/micro-tools/wf/internal/json"
"gitee.com/micro-tools/wf/os/gtime"
"math"
"reflect"
"strconv"
"strings"
"time"
"gitee.com/micro-tools/wf/encoding/gbinary"
)
type (
// errorStack is the interface for Stack feature.
errorStack interface {
Error() string
Stack() string
}
)
var (
// Empty strings.
emptyStringMap = map[string]struct{}{
"": {},
"0": {},
"no": {},
"off": {},
"false": {},
}
// StructTagPriority defines the default priority tags for Map*/Struct* functions.
// Note, the "gconv", "param", "params" tags are used by old version of package.
// It is strongly recommended using short tag "c" or "p" instead in the future.
StructTagPriority = []string{"gconv", "param", "params", "c", "p", "json"}
)
// Convert converts the variable `any` to the type `t`, the type `t` is specified by string.
// The optional parameter `params` is used for additional necessary parameter for this conversion.
// It supports common types conversion as its conversion based on type name string.
func Convert(any interface{}, t string, params ...interface{}) interface{} {
switch t {
case "int":
return Int(any)
case "*int":
if _, ok := any.(*int); ok {
return any
}
v := Int(any)
return &v
case "int8":
return Int8(any)
case "*int8":
if _, ok := any.(*int8); ok {
return any
}
v := Int8(any)
return &v
case "int16":
return Int16(any)
case "*int16":
if _, ok := any.(*int16); ok {
return any
}
v := Int16(any)
return &v
case "int32":
return Int32(any)
case "*int32":
if _, ok := any.(*int32); ok {
return any
}
v := Int32(any)
return &v
case "int64":
return Int64(any)
case "*int64":
if _, ok := any.(*int64); ok {
return any
}
v := Int64(any)
return &v
case "uint":
return Uint(any)
case "*uint":
if _, ok := any.(*uint); ok {
return any
}
v := Uint(any)
return &v
case "uint8":
return Uint8(any)
case "*uint8":
if _, ok := any.(*uint8); ok {
return any
}
v := Uint8(any)
return &v
case "uint16":
return Uint16(any)
case "*uint16":
if _, ok := any.(*uint16); ok {
return any
}
v := Uint16(any)
return &v
case "uint32":
return Uint32(any)
case "*uint32":
if _, ok := any.(*uint32); ok {
return any
}
v := Uint32(any)
return &v
case "uint64":
return Uint64(any)
case "*uint64":
if _, ok := any.(*uint64); ok {
return any
}
v := Uint64(any)
return &v
case "float32":
return Float32(any)
case "*float32":
if _, ok := any.(*float32); ok {
return any
}
v := Float32(any)
return &v
case "float64":
return Float64(any)
case "*float64":
if _, ok := any.(*float64); ok {
return any
}
v := Float64(any)
return &v
case "bool":
return Bool(any)
case "*bool":
if _, ok := any.(*bool); ok {
return any
}
v := Bool(any)
return &v
case "string":
return String(any)
case "*string":
if _, ok := any.(*string); ok {
return any
}
v := String(any)
return &v
case "[]byte":
return Bytes(any)
case "[]int":
return Ints(any)
case "[]int32":
return Int32s(any)
case "[]int64":
return Int64s(any)
case "[]uint":
return Uints(any)
case "[]uint32":
return Uint32s(any)
case "[]uint64":
return Uint64s(any)
case "[]float32":
return Float32s(any)
case "[]float64":
return Float64s(any)
case "[]string":
return Strings(any)
case "Time", "time.Time":
if len(params) > 0 {
return Time(any, String(params[0]))
}
return Time(any)
case "*time.Time":
var v interface{}
if len(params) > 0 {
v = Time(any, String(params[0]))
} else {
if _, ok := any.(*time.Time); ok {
return any
}
v = Time(any)
}
return &v
case "GTime", "gtime.Time":
if len(params) > 0 {
if v := GTime(any, String(params[0])); v != nil {
return *v
} else {
return *gtime.New()
}
}
if v := GTime(any); v != nil {
return *v
} else {
return *gtime.New()
}
case "*gtime.Time":
if len(params) > 0 {
if v := GTime(any, String(params[0])); v != nil {
return v
} else {
return gtime.New()
}
}
if v := GTime(any); v != nil {
return v
} else {
return gtime.New()
}
case "Duration", "time.Duration":
return Duration(any)
case "*time.Duration":
if _, ok := any.(*time.Duration); ok {
return any
}
v := Duration(any)
return &v
case "map[string]string":
return MapStrStr(any)
case "map[string]interface{}":
return Map(any)
case "[]map[string]interface{}":
return Maps(any)
//case "gvar.Var":
// // TODO remove reflect usage to create gvar.Var, considering using unsafe pointer
// rv := reflect.New(intstore.ReflectTypeVarImp)
// ri := rv.Interface()
// if v, ok := ri.(apiSet); ok {
// v.Set(any)
// } else if v, ok := ri.(apiUnmarshalValue); ok {
// v.UnmarshalValue(any)
// } else {
// rv.Set(reflect.ValueOf(any))
// }
// return ri
default:
return any
}
}
// Byte converts `any` to byte.
func Byte(any interface{}) byte {
if v, ok := any.(byte); ok {
return v
}
return Uint8(any)
}
// Bytes converts `any` to []byte.
func Bytes(any interface{}) []byte {
if any == nil {
return nil
}
switch value := any.(type) {
case string:
return []byte(value)
case []byte:
return value
default:
if f, ok := value.(apiBytes); ok {
return f.Bytes()
}
var (
reflectValue = reflect.ValueOf(any)
reflectKind = reflectValue.Kind()
)
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch reflectKind {
case reflect.Array, reflect.Slice:
var (
ok = true
bytes = make([]byte, reflectValue.Len())
)
for i, _ := range bytes {
int32Value := Int32(reflectValue.Index(i).Interface())
if int32Value < 0 || int32Value > math.MaxUint8 {
ok = false
break
}
bytes[i] = byte(int32Value)
}
if ok {
return bytes
}
}
return gbinary.Encode(any)
}
}
// Rune converts `any` to rune.
func Rune(any interface{}) rune {
if v, ok := any.(rune); ok {
return v
}
return Int32(any)
}
// Runes converts `any` to []rune.
func Runes(any interface{}) []rune {
if v, ok := any.([]rune); ok {
return v
}
return []rune(String(any))
}
// String converts `any` to string.
// It's most common used converting function.
func String(any interface{}) string {
if any == nil {
return ""
}
switch value := any.(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.FormatInt(value, 10)
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(value, 10)
case float32:
return strconv.FormatFloat(float64(value), 'f', -1, 32)
case float64:
return strconv.FormatFloat(value, 'f', -1, 64)
case bool:
return strconv.FormatBool(value)
case string:
return value
case []byte:
return string(value)
case time.Time:
if value.IsZero() {
return ""
}
return value.String()
case *time.Time:
if value == nil {
return ""
}
return value.String()
case gtime.Time:
if value.IsZero() {
return ""
}
return value.String()
case *gtime.Time:
if value == nil {
return ""
}
return value.String()
default:
// Empty checks.
if value == nil {
return ""
}
if f, ok := value.(apiString); ok {
// If the variable implements the String() interface,
// then use that interface to perform the conversion
return f.String()
}
if f, ok := value.(apiError); ok {
// If the variable implements the Error() interface,
// then use that interface to perform the conversion
return f.Error()
}
// Reflect checks.
var (
rv = reflect.ValueOf(value)
kind = rv.Kind()
)
switch kind {
case reflect.Chan,
reflect.Map,
reflect.Slice,
reflect.Func,
reflect.Ptr,
reflect.Interface,
reflect.UnsafePointer:
if rv.IsNil() {
return ""
}
case reflect.String:
return rv.String()
}
if kind == reflect.Ptr {
return String(rv.Elem().Interface())
}
// Finally we use json.Marshal to convert.
if jsonContent, err := json.Marshal(value); err != nil {
return fmt.Sprint(value)
} else {
return string(jsonContent)
}
}
}
// Bool converts `any` to bool.
// It returns false if `any` is: false, "", 0, "false", "off", "no", empty slice/map.
func Bool(any interface{}) bool {
if any == nil {
return false
}
switch value := any.(type) {
case bool:
return value
case []byte:
if _, ok := emptyStringMap[strings.ToLower(string(value))]; ok {
return false
}
return true
case string:
if _, ok := emptyStringMap[strings.ToLower(value)]; ok {
return false
}
return true
default:
if f, ok := value.(apiBool); ok {
return f.Bool()
}
rv := reflect.ValueOf(any)
switch rv.Kind() {
case reflect.Ptr:
return !rv.IsNil()
case reflect.Map:
fallthrough
case reflect.Array:
fallthrough
case reflect.Slice:
return rv.Len() != 0
case reflect.Struct:
return true
default:
s := strings.ToLower(String(any))
if _, ok := emptyStringMap[s]; ok {
return false
}
return true
}
}
}
// Int converts `any` to int.
func Int(any interface{}) int {
if any == nil {
return 0
}
if v, ok := any.(int); ok {
return v
}
return int(Int64(any))
}
// Int8 converts `any` to int8.
func Int8(any interface{}) int8 {
if any == nil {
return 0
}
if v, ok := any.(int8); ok {
return v
}
return int8(Int64(any))
}
// Int16 converts `any` to int16.
func Int16(any interface{}) int16 {
if any == nil {
return 0
}
if v, ok := any.(int16); ok {
return v
}
return int16(Int64(any))
}
// Int32 converts `any` to int32.
func Int32(any interface{}) int32 {
if any == nil {
return 0
}
if v, ok := any.(int32); ok {
return v
}
return int32(Int64(any))
}
// Int64 converts `any` to int64.
func Int64(any interface{}) int64 {
if any == nil {
return 0
}
switch value := any.(type) {
case int:
return int64(value)
case int8:
return int64(value)
case int16:
return int64(value)
case int32:
return int64(value)
case int64:
return value
case uint:
return int64(value)
case uint8:
return int64(value)
case uint16:
return int64(value)
case uint32:
return int64(value)
case uint64:
return int64(value)
case float32:
return int64(value)
case float64:
return int64(value)
case bool:
if value {
return 1
}
return 0
case []byte:
return gbinary.DecodeToInt64(value)
default:
if f, ok := value.(apiInt64); ok {
return f.Int64()
}
s := String(value)
isMinus := false
if len(s) > 0 {
if s[0] == '-' {
isMinus = true
s = s[1:]
} else if s[0] == '+' {
s = s[1:]
}
}
// Hexadecimal
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
if v, e := strconv.ParseInt(s[2:], 16, 64); e == nil {
if isMinus {
return -v
}
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseInt(s[1:], 8, 64); e == nil {
if isMinus {
return -v
}
return v
}
}
// Decimal
if v, e := strconv.ParseInt(s, 10, 64); e == nil {
if isMinus {
return -v
}
return v
}
// Float64
return int64(Float64(value))
}
}
// Uint converts `any` to uint.
func Uint(any interface{}) uint {
if any == nil {
return 0
}
if v, ok := any.(uint); ok {
return v
}
return uint(Uint64(any))
}
// Uint8 converts `any` to uint8.
func Uint8(any interface{}) uint8 {
if any == nil {
return 0
}
if v, ok := any.(uint8); ok {
return v
}
return uint8(Uint64(any))
}
// Uint16 converts `any` to uint16.
func Uint16(any interface{}) uint16 {
if any == nil {
return 0
}
if v, ok := any.(uint16); ok {
return v
}
return uint16(Uint64(any))
}
// Uint32 converts `any` to uint32.
func Uint32(any interface{}) uint32 {
if any == nil {
return 0
}
if v, ok := any.(uint32); ok {
return v
}
return uint32(Uint64(any))
}
// Uint64 converts `any` to uint64.
func Uint64(any interface{}) uint64 {
if any == nil {
return 0
}
switch value := any.(type) {
case int:
return uint64(value)
case int8:
return uint64(value)
case int16:
return uint64(value)
case int32:
return uint64(value)
case int64:
return uint64(value)
case uint:
return uint64(value)
case uint8:
return uint64(value)
case uint16:
return uint64(value)
case uint32:
return uint64(value)
case uint64:
return value
case float32:
return uint64(value)
case float64:
return uint64(value)
case bool:
if value {
return 1
}
return 0
case []byte:
return gbinary.DecodeToUint64(value)
default:
if f, ok := value.(apiUint64); ok {
return f.Uint64()
}
s := String(value)
// Hexadecimal
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
if v, e := strconv.ParseUint(s[2:], 16, 64); e == nil {
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseUint(s[1:], 8, 64); e == nil {
return v
}
}
// Decimal
if v, e := strconv.ParseUint(s, 10, 64); e == nil {
return v
}
// Float64
return uint64(Float64(value))
}
}
// Float32 converts `any` to float32.
func Float32(any interface{}) float32 {
if any == nil {
return 0
}
switch value := any.(type) {
case float32:
return value
case float64:
return float32(value)
case []byte:
return gbinary.DecodeToFloat32(value)
default:
if f, ok := value.(apiFloat32); ok {
return f.Float32()
}
v, _ := strconv.ParseFloat(String(any), 64)
return float32(v)
}
}
// Float64 converts `any` to float64.
func Float64(any interface{}) float64 {
if any == nil {
return 0
}
switch value := any.(type) {
case float32:
return float64(value)
case float64:
return value
case []byte:
return gbinary.DecodeToFloat64(value)
default:
if f, ok := value.(apiFloat64); ok {
return f.Float64()
}
v, _ := strconv.ParseFloat(String(any), 64)
return v
}
}
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

搜索帮助

53164aa7 5694891 3bd8fe86 5694891