iedc-go/common/map.go

190 lines
3.1 KiB
Go
Raw Normal View History

2021-05-23 23:27:41 +00:00
package common
2017-08-04 08:20:59 +00:00
import (
"encoding/json"
"errors"
"reflect"
2022-03-12 21:06:28 +00:00
"sort"
2022-05-13 07:31:55 +00:00
"time"
2017-08-04 08:20:59 +00:00
)
//hotime的常用map
type Map map[string]interface{}
//获取string
2022-03-12 17:48:54 +00:00
func (that Map) GetString(key string, err ...*Error) string {
2017-09-05 02:55:14 +00:00
if len(err) != 0 {
err[0].SetError(nil)
}
2022-03-12 17:48:54 +00:00
return ObjToStr((that)[key])
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that *Map) Pointer() *Map {
2017-08-04 08:20:59 +00:00
2022-03-12 17:48:54 +00:00
return that
2017-08-04 08:20:59 +00:00
}
//增加接口
2022-03-12 17:48:54 +00:00
func (that Map) Put(key string, value interface{}) {
//if that==nil{
// that=Map{}
2017-09-05 02:55:14 +00:00
//}
2022-03-12 17:48:54 +00:00
that[key] = value
2017-08-04 08:20:59 +00:00
}
//删除接口
2022-03-12 17:48:54 +00:00
func (that Map) Delete(key string) {
delete(that, key)
2017-08-04 08:20:59 +00:00
2017-09-05 02:40:37 +00:00
}
2017-08-04 08:20:59 +00:00
//获取Int
2022-03-12 17:48:54 +00:00
func (that Map) GetInt(key string, err ...*Error) int {
v := ObjToInt((that)[key], err...)
2017-08-04 08:20:59 +00:00
2017-09-05 02:55:14 +00:00
return v
2017-08-04 08:20:59 +00:00
}
//获取Int
2022-03-12 17:48:54 +00:00
func (that Map) GetInt64(key string, err ...*Error) int64 {
v := ObjToInt64((that)[key], err...)
2017-09-05 02:55:14 +00:00
return v
2017-08-04 08:20:59 +00:00
}
2018-01-23 18:08:25 +00:00
//获取向上取整Int64
2022-03-12 17:48:54 +00:00
func (that Map) GetCeilInt64(key string, err ...*Error) int64 {
v := ObjToCeilInt64((that)[key], err...)
2018-01-23 18:08:25 +00:00
return v
}
2018-01-23 18:08:25 +00:00
//获取向上取整Int
2022-03-12 17:48:54 +00:00
func (that Map) GetCeilInt(key string, err ...*Error) int {
v := ObjToCeilInt((that)[key], err...)
2018-01-23 18:08:25 +00:00
return v
}
2018-01-28 17:30:47 +00:00
//获取向上取整float64
2022-03-12 17:48:54 +00:00
func (that Map) GetCeilFloat64(key string, err ...*Error) float64 {
v := ObjToCeilFloat64((that)[key], err...)
2018-01-28 17:30:47 +00:00
return v
2018-01-23 18:08:25 +00:00
2018-01-28 17:30:47 +00:00
}
2017-08-04 08:20:59 +00:00
//获取Float64
2022-03-12 17:48:54 +00:00
func (that Map) GetFloat64(key string, err ...*Error) float64 {
2017-08-04 08:20:59 +00:00
2022-03-12 17:48:54 +00:00
v := ObjToFloat64((that)[key], err...)
2017-09-05 02:55:14 +00:00
return v
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that Map) GetSlice(key string, err ...*Error) Slice {
2017-08-04 08:20:59 +00:00
2017-09-05 02:40:37 +00:00
//var v Slice
2022-03-12 17:48:54 +00:00
v := ObjToSlice((that)[key], err...)
2017-08-04 08:20:59 +00:00
2017-09-05 02:55:14 +00:00
return v
2017-08-04 08:20:59 +00:00
2021-05-23 21:47:56 +00:00
}
2022-03-12 17:48:54 +00:00
func (that Map) GetBool(key string, err ...*Error) bool {
2021-05-23 21:47:56 +00:00
//var v Slice
2022-03-12 17:48:54 +00:00
v := ObjToBool((that)[key], err...)
2021-05-23 21:47:56 +00:00
return v
2022-03-12 21:06:28 +00:00
}
2022-05-13 07:31:55 +00:00
2022-10-20 01:08:23 +00:00
func (that Map) GetTime(key string, err ...*Error) time.Time {
2022-05-13 07:31:55 +00:00
v := ObjToTime((that)[key], err...)
return v
}
2022-03-12 21:06:28 +00:00
func (that Map) RangeSort(callback func(k string, v interface{}) (isEnd bool)) {
testQu := []string{}
//testQuData:= qu[0].(Map)
for key, _ := range that {
//fmt.Println(key, ":", value)
testQu = append(testQu, key)
}
sort.Strings(testQu)
for _, k := range testQu {
re := callback(k, that[k])
if re {
return
}
}
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that Map) GetMap(key string, err ...*Error) Map {
2017-09-05 02:55:14 +00:00
//var data Slice
2017-08-04 08:20:59 +00:00
2022-03-12 17:48:54 +00:00
v := ObjToMap((that)[key], err...)
2017-08-04 08:20:59 +00:00
2017-09-05 02:55:14 +00:00
return v
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that Map) Get(key string, err ...*Error) interface{} {
2017-08-04 08:20:59 +00:00
2022-03-12 17:48:54 +00:00
if v, ok := (that)[key]; ok {
2017-09-05 02:55:14 +00:00
return v
}
e := errors.New("没有存储key及对应的数据")
2017-09-05 02:40:37 +00:00
2017-09-05 02:55:14 +00:00
if len(err) != 0 {
err[0].SetError(e)
2017-09-05 02:40:37 +00:00
2017-09-05 02:55:14 +00:00
}
return nil
2017-08-04 08:20:59 +00:00
}
//请传递指针过来
2022-03-12 17:48:54 +00:00
func (that Map) ToStruct(stct interface{}) {
2017-08-04 08:20:59 +00:00
2017-09-05 02:55:14 +00:00
data := reflect.ValueOf(stct).Elem()
2022-03-12 17:48:54 +00:00
for k, v := range that {
2017-09-05 02:55:14 +00:00
ks := StrFirstToUpper(k)
dkey := data.FieldByName(ks)
if !dkey.IsValid() {
continue
2017-08-04 08:20:59 +00:00
}
2017-09-05 02:55:14 +00:00
switch dkey.Type().String() {
case "int":
2022-03-12 17:48:54 +00:00
dkey.SetInt(that.GetInt64(k))
2017-09-05 02:55:14 +00:00
case "int64":
2022-03-12 17:48:54 +00:00
dkey.Set(reflect.ValueOf(that.GetInt64(k)))
2017-09-05 02:55:14 +00:00
case "float64":
2022-03-12 17:48:54 +00:00
dkey.Set(reflect.ValueOf(that.GetFloat64(k)))
2017-09-05 02:55:14 +00:00
case "string":
2022-03-12 17:48:54 +00:00
dkey.Set(reflect.ValueOf(that.GetString(k)))
2017-09-05 02:55:14 +00:00
case "interface{}":
dkey.Set(reflect.ValueOf(v))
}
}
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that Map) ToJsonString() string {
return ObjToStr(that)
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that Map) JsonToMap(jsonStr string, err ...*Error) {
e := json.Unmarshal([]byte(jsonStr), &that)
2017-09-05 02:55:14 +00:00
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
2017-08-04 08:20:59 +00:00
}