iedc-go/common/objtoobj.go

393 lines
7.0 KiB
Go
Raw Permalink Normal View History

2021-05-23 23:27:41 +00:00
package common
2017-08-04 08:20:59 +00:00
import (
2017-10-24 01:31:20 +00:00
"encoding/json"
2017-08-04 08:20:59 +00:00
"errors"
2018-01-23 18:08:25 +00:00
"math"
"strconv"
2022-05-13 07:31:55 +00:00
"time"
2017-08-04 08:20:59 +00:00
)
//仅限于hotime.Slice
2017-10-24 01:31:20 +00:00
func ObjToMap(obj interface{}, e ...*Error) Map {
2017-08-04 08:20:59 +00:00
var err error
var v Map
if obj == nil {
v = nil
err = errors.New("没有合适的转换对象!")
} else {
2017-10-24 01:31:20 +00:00
switch obj.(type) {
case Map:
v = obj.(Map)
case map[string]interface{}:
v = obj.(map[string]interface{})
2017-11-06 16:38:03 +00:00
case string:
v = Map{}
e := json.Unmarshal([]byte(obj.(string)), &v)
if e != nil {
err = errors.New("没有合适的转换对象!" + e.Error())
v = nil
}
2017-08-04 08:20:59 +00:00
default:
2017-10-27 09:14:15 +00:00
data, err := json.Marshal(obj)
if err != nil {
err = errors.New("没有合适的转换对象!" + err.Error())
v = nil
}
v = Map{}
e := json.Unmarshal(data, &v)
if e != nil {
err = errors.New("没有合适的转换对象!" + e.Error())
v = nil
}
2017-08-04 08:20:59 +00:00
}
}
2017-10-24 01:31:20 +00:00
if len(e) != 0 {
2017-08-04 08:20:59 +00:00
e[0].SetError(err)
}
return v
}
2018-04-09 17:16:24 +00:00
func ObjToMapArray(obj interface{}, e ...*Error) []Map {
s := ObjToSlice(obj, e...)
res := []Map{}
for i := 0; i < len(s); i++ {
res = append(res, s.GetMap(i))
2018-04-09 17:16:24 +00:00
}
return res
}
2017-08-04 08:20:59 +00:00
//仅限于hotime.Slice
2017-10-24 01:31:20 +00:00
func ObjToSlice(obj interface{}, e ...*Error) Slice {
2017-08-04 08:20:59 +00:00
var err error
var v Slice
if obj == nil {
v = nil
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case Slice:
v = obj.(Slice)
2017-10-24 01:31:20 +00:00
case []interface{}:
v = obj.([]interface{})
2017-10-27 04:28:47 +00:00
case []string:
v = Slice{}
for i := 0; i < len(obj.([]string)); i++ {
v = append(v, obj.([]string)[i])
}
2017-11-09 10:15:55 +00:00
case string:
v = Slice{}
err = json.Unmarshal([]byte(obj.(string)), &v)
2017-10-27 04:28:47 +00:00
2017-08-04 08:20:59 +00:00
default:
2017-11-13 10:20:35 +00:00
v = Slice{}
var data []byte
data, err = json.Marshal(obj)
err = json.Unmarshal(data, &v)
2017-08-04 08:20:59 +00:00
}
}
2017-10-24 01:31:20 +00:00
if len(e) != 0 {
2017-08-04 08:20:59 +00:00
e[0].SetError(err)
}
return v
}
2022-10-20 01:08:23 +00:00
func ObjToTime(obj interface{}, e ...*Error) time.Time {
2022-05-13 07:31:55 +00:00
tInt := ObjToInt64(obj)
//字符串类型只支持标准mysql datetime格式
if tInt == 0 {
tStr := ObjToStr(obj)
if len(tStr) > 18 {
t, e := time.Parse("2006-01-02 15:04:05", tStr)
if e == nil {
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
}
} else if len(tStr) > 15 {
t, e := time.Parse("2006-01-02 15:04", tStr)
if e == nil {
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
}
} else if len(tStr) > 12 {
t, e := time.Parse("2006-01-02 15", tStr)
if e == nil {
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
}
} else if len(tStr) > 9 {
t, e := time.Parse("2006-01-02", tStr)
if e == nil {
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
}
} else if len(tStr) > 6 {
t, e := time.Parse("2006-01", tStr)
if e == nil {
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
}
}
}
//纳秒级别
if len(ObjToStr(tInt)) > 16 {
2022-06-14 01:52:49 +00:00
t := time.Time{}.Add(time.Nanosecond * time.Duration(tInt))
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
//微秒级别
} else if len(ObjToStr(tInt)) > 13 {
2022-06-14 01:52:49 +00:00
t := time.Time{}.Add(time.Microsecond * time.Duration(tInt))
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
//毫秒级别
} else if len(ObjToStr(tInt)) > 10 {
2022-06-14 01:52:49 +00:00
t := time.Time{}.Add(time.Millisecond * time.Duration(tInt))
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
//秒级别
} else if len(ObjToStr(tInt)) > 9 {
2022-06-14 01:52:49 +00:00
t := time.Time{}.Add(time.Second * time.Duration(tInt))
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
} else if len(ObjToStr(tInt)) > 3 {
t, e := time.Parse("2006", ObjToStr(tInt))
if e == nil {
2022-10-20 01:08:23 +00:00
return t
2022-05-13 07:31:55 +00:00
}
}
2022-10-20 01:08:23 +00:00
return time.Time{}
2022-05-13 07:31:55 +00:00
}
2017-10-24 01:31:20 +00:00
func ObjToFloat64(obj interface{}, e ...*Error) float64 {
2017-08-04 08:20:59 +00:00
var err error
v := float64(0)
if obj == nil {
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case int:
v = float64(obj.(int))
case int64:
v = float64(obj.(int64))
case string:
value, e := strconv.ParseFloat(obj.(string), 64)
if e != nil {
v = float64(0)
err = e
} else {
v = value
}
case float64:
v = obj.(float64)
case float32:
v = float64(obj.(float32))
case uint8:
value, e := strconv.ParseFloat(obj.(string), 64)
if e != nil {
v = float64(0)
err = e
} else {
v = value
}
default:
v = float64(0)
err = errors.New("没有合适的转换对象!")
}
}
2022-01-22 08:12:02 +00:00
if math.IsNaN(v) {
err = errors.New("float64 is NaN")
v = 0
}
2022-02-16 20:10:53 +00:00
if math.IsInf(v, 0) {
err = errors.New("float64 is Inf")
v = 0
}
2017-10-24 01:31:20 +00:00
if len(e) != 0 {
2017-08-04 08:20:59 +00:00
e[0].SetError(err)
}
2017-11-15 10:09:20 +00:00
2017-08-04 08:20:59 +00:00
return v
}
2018-01-23 18:08:25 +00:00
//向上取整
func ObjToCeilInt64(obj interface{}, e ...*Error) int64 {
f := ObjToCeilFloat64(obj, e...)
return ObjToInt64(math.Ceil(f))
2018-01-23 18:08:25 +00:00
}
2018-01-28 17:30:47 +00:00
2018-01-23 18:08:25 +00:00
//向上取整
func ObjToCeilFloat64(obj interface{}, e ...*Error) float64 {
f := ObjToFloat64(obj, e...)
return math.Ceil(f)
2018-01-28 17:30:47 +00:00
}
//向上取整
func ObjToCeilInt(obj interface{}, e ...*Error) int {
f := ObjToCeilFloat64(obj, e...)
return ObjToInt(f)
2018-01-23 18:08:25 +00:00
}
2017-08-04 08:20:59 +00:00
2017-10-24 01:31:20 +00:00
func ObjToInt64(obj interface{}, e ...*Error) int64 {
2017-08-04 08:20:59 +00:00
var err error
v := int64(0)
if obj == nil {
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case int:
v = int64(obj.(int))
case int64:
v = obj.(int64)
case string:
value, e := StrToInt(obj.(string))
if e != nil {
v = int64(0)
err = e
} else {
v = int64(value)
}
case uint8:
value, e := StrToInt(obj.(string))
if e != nil {
v = int64(0)
err = e
} else {
v = int64(value)
}
case float64:
v = int64(obj.(float64))
case float32:
v = int64(obj.(float32))
default:
v = int64(0)
err = errors.New("没有合适的转换对象!")
}
}
2017-10-24 01:31:20 +00:00
if len(e) != 0 {
2017-08-04 08:20:59 +00:00
e[0].SetError(err)
}
return v
}
2017-10-24 01:31:20 +00:00
func ObjToInt(obj interface{}, e ...*Error) int {
v := ObjToInt64(obj, e...)
2018-01-23 18:08:25 +00:00
return int(v)
2017-08-04 08:20:59 +00:00
}
2021-05-23 21:47:56 +00:00
func ObjToBool(obj interface{}, e ...*Error) bool {
var err error
v := false
if obj == nil {
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case bool:
v = obj.(bool)
default:
toInt := ObjToInt(obj)
if toInt != 0 {
v = true
}
err = errors.New("没有合适的转换对象!")
}
}
if len(e) != 0 {
e[0].SetError(err)
}
return v
}
2017-08-04 08:20:59 +00:00
func ObjToStr(obj interface{}) string {
// fmt.Println(reflect.ValueOf(obj).Type().String() )
str := ""
2017-10-24 01:31:20 +00:00
if obj == nil {
2017-08-04 08:20:59 +00:00
return str
}
switch obj.(type) {
case int:
str = strconv.Itoa(obj.(int))
case uint8:
str = obj.(string)
case int64:
str = strconv.FormatInt(obj.(int64), 10)
case []byte:
str = string(obj.([]byte))
case string:
str = obj.(string)
case float64:
2017-11-15 10:09:20 +00:00
str = strconv.FormatFloat(obj.(float64), 'f', -1, 64)
2017-08-04 08:20:59 +00:00
default:
2021-06-05 18:27:22 +00:00
strbte, err := json.MarshalIndent(obj, "", "\t")
2017-10-24 01:31:20 +00:00
if err == nil {
2021-06-05 18:27:22 +00:00
str = string(strbte)
2017-08-04 08:20:59 +00:00
}
}
return str
}
2017-10-30 09:54:54 +00:00
//转换为Map
func StrToMap(string string) Map {
data := Map{}
data.JsonToMap(string)
return data
}
//转换为Slice
func StrToSlice(string string) Slice {
2017-11-09 10:15:55 +00:00
data := ObjToSlice(string)
2017-10-30 09:54:54 +00:00
return data
}
2017-10-24 01:31:20 +00:00
//字符串数组: a1,a2,a3转["a1","a2","a3"]
func StrArrayToJsonStr(a string) string {
2017-10-30 08:23:42 +00:00
2017-11-14 10:27:09 +00:00
if len(a) > 2 {
2017-10-30 08:23:42 +00:00
if a[0] == ',' {
2017-11-09 10:15:55 +00:00
a = Substr(a, 1, len(a)-1)
}
if a[len(a)-1] == ',' {
2017-12-03 18:58:10 +00:00
a = Substr(a, 0, len(a)-1)
2017-10-30 08:23:42 +00:00
}
2017-11-09 10:15:55 +00:00
//a = strings.Replace(a, ",", `,`, -1)
a = `[` + a + `]`
2017-10-24 01:31:20 +00:00
} else {
a = "[]"
}
return a
}
//字符串数组: a1,a2,a3转["a1","a2","a3"]
func JsonStrToStrArray(a string) string {
2017-11-09 10:15:55 +00:00
//a = strings.Replace(a, `"`, "", -1)
2017-10-27 04:28:47 +00:00
if len(a) != 0 {
2017-10-24 01:31:20 +00:00
a = Substr(a, 1, len(a)-2)
}
2017-11-09 10:15:55 +00:00
return "," + a + ","
2017-10-24 01:31:20 +00:00
}
2017-08-04 08:20:59 +00:00
//字符串转int
func StrToInt(s string) (int, error) {
i, err := strconv.Atoi(s)
return i, err
2017-10-24 01:31:20 +00:00
}