2017-08-04 08:20:59 +00:00
|
|
|
package hotime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
2017-09-05 02:40:37 +00:00
|
|
|
//"time"
|
2017-08-04 08:20:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//hotime的常用map
|
|
|
|
type Map map[string]interface{}
|
|
|
|
|
|
|
|
//获取string
|
|
|
|
func (this Map) GetString(key string, err ...*Error) string {
|
2017-09-05 02:40:37 +00:00
|
|
|
return SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
if len(err) != 0 {
|
|
|
|
err[0].SetError(nil)
|
|
|
|
}
|
|
|
|
return ObjToStr((this)[key])
|
2017-09-05 02:40:37 +00:00
|
|
|
}).(string)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
func (this *Map) Pointer() *Map {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
//增加接口
|
|
|
|
func (this Map) Put(key string, value interface{}) {
|
2017-09-05 02:40:37 +00:00
|
|
|
SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
this[key] = value
|
2017-09-05 02:40:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//删除接口
|
|
|
|
func (this Map) Delete(key string) {
|
2017-09-05 02:40:37 +00:00
|
|
|
SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
delete(this, key)
|
2017-09-05 02:40:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
2017-08-04 08:20:59 +00:00
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
//唯一标志
|
|
|
|
func (this Map) GetTag() string {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
//return SafeMutex(MUTEX_MAP, func() interface{} {
|
|
|
|
// if this[MUTEX_MAP] == nil {
|
|
|
|
// this[MUTEX_MAP] = ObjToStr(time.Now().UnixNano())
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// return ObjToStr(this[MUTEX_MAP])
|
|
|
|
//}).(string)
|
|
|
|
return MUTEX_MAP
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
//获取Int
|
|
|
|
func (this Map) GetInt(key string, err ...*Error) int {
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
return SafeMutex(this.GetTag(), func() interface{} {
|
|
|
|
v := ObjToInt((this)[key], err...)
|
2017-08-04 08:20:59 +00:00
|
|
|
return v
|
2017-09-05 02:40:37 +00:00
|
|
|
}).(int)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取Int
|
|
|
|
func (this Map) GetInt64(key string, err ...*Error) int64 {
|
2017-09-05 02:40:37 +00:00
|
|
|
|
|
|
|
return SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
v := ObjToInt64((this)[key], err...)
|
|
|
|
return v
|
2017-09-05 02:40:37 +00:00
|
|
|
}).(int64)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取Float64
|
|
|
|
func (this Map) GetFloat64(key string, err ...*Error) float64 {
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
return SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
v := ObjToFloat64((this)[key], err...)
|
|
|
|
return v
|
2017-09-05 02:40:37 +00:00
|
|
|
}).(float64)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this Map) GetSlice(key string, err ...*Error) Slice {
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
res := SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
v := ObjToSlice((this)[key], err...)
|
|
|
|
return v
|
2017-09-05 02:40:37 +00:00
|
|
|
})
|
|
|
|
//var v Slice
|
|
|
|
if res != nil {
|
|
|
|
return res.(Slice)
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
return nil
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this Map) GetMap(key string, err ...*Error) Map {
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
res := SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
v := ObjToMap((this)[key], err...)
|
|
|
|
return v
|
2017-09-05 02:40:37 +00:00
|
|
|
})
|
|
|
|
//var v Slice
|
|
|
|
if res != nil {
|
|
|
|
return res.(Map)
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
return nil
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this Map) Get(key string, err ...*Error) interface{} {
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
res := SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
if v, ok := (this)[key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
e := errors.New("没有存储key及对应的数据")
|
|
|
|
|
|
|
|
if len(err) != 0 {
|
|
|
|
err[0].SetError(e)
|
|
|
|
|
|
|
|
}
|
2017-09-05 02:40:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//请传递指针过来
|
|
|
|
func (this Map) ToStruct(stct interface{}) {
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
data := reflect.ValueOf(stct).Elem()
|
|
|
|
for k, v := range this {
|
|
|
|
ks := StrFirstToUpper(k)
|
|
|
|
dkey := data.FieldByName(ks)
|
|
|
|
if !dkey.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch dkey.Type().String() {
|
|
|
|
case "int":
|
|
|
|
dkey.SetInt(this.GetInt64(k))
|
|
|
|
case "int64":
|
|
|
|
dkey.Set(reflect.ValueOf(this.GetInt64(k)))
|
|
|
|
case "float64":
|
|
|
|
dkey.Set(reflect.ValueOf(this.GetFloat64(k)))
|
|
|
|
case "string":
|
|
|
|
dkey.Set(reflect.ValueOf(this.GetString(k)))
|
|
|
|
case "interface{}":
|
|
|
|
dkey.Set(reflect.ValueOf(v))
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 02:40:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this Map) ToJsonString() string {
|
2017-09-05 02:40:37 +00:00
|
|
|
res := SafeMutex(this.GetTag(), func() interface{} {
|
2017-08-04 08:20:59 +00:00
|
|
|
return ObjToStr(this)
|
2017-09-05 02:40:37 +00:00
|
|
|
})
|
|
|
|
return res.(string)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-05 02:40:37 +00:00
|
|
|
func (this Map) JsonToMap(jsonStr string, err ...*Error) {
|
|
|
|
|
|
|
|
SafeMutex(this.GetTag(), func() interface{} {
|
|
|
|
e := json.Unmarshal([]byte(jsonStr), &this)
|
|
|
|
if e != nil && len(err) != 0 {
|
2017-08-04 08:20:59 +00:00
|
|
|
err[0].SetError(e)
|
|
|
|
}
|
2017-09-05 02:40:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|