hotime/map.go
2017-09-05 02:40:37 +00:00

187 lines
3.3 KiB
Go

package hotime
import (
"encoding/json"
"errors"
"reflect"
//"time"
)
//hotime的常用map
type Map map[string]interface{}
//获取string
func (this Map) GetString(key string, err ...*Error) string {
return SafeMutex(this.GetTag(), func() interface{} {
if len(err) != 0 {
err[0].SetError(nil)
}
return ObjToStr((this)[key])
}).(string)
}
func (this *Map) Pointer() *Map {
return this
}
//增加接口
func (this Map) Put(key string, value interface{}) {
SafeMutex(this.GetTag(), func() interface{} {
this[key] = value
return nil
})
}
//删除接口
func (this Map) Delete(key string) {
SafeMutex(this.GetTag(), func() interface{} {
delete(this, key)
return nil
})
}
//唯一标志
func (this Map) GetTag() string {
//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
}
//获取Int
func (this Map) GetInt(key string, err ...*Error) int {
return SafeMutex(this.GetTag(), func() interface{} {
v := ObjToInt((this)[key], err...)
return v
}).(int)
}
//获取Int
func (this Map) GetInt64(key string, err ...*Error) int64 {
return SafeMutex(this.GetTag(), func() interface{} {
v := ObjToInt64((this)[key], err...)
return v
}).(int64)
}
//获取Float64
func (this Map) GetFloat64(key string, err ...*Error) float64 {
return SafeMutex(this.GetTag(), func() interface{} {
v := ObjToFloat64((this)[key], err...)
return v
}).(float64)
}
func (this Map) GetSlice(key string, err ...*Error) Slice {
res := SafeMutex(this.GetTag(), func() interface{} {
v := ObjToSlice((this)[key], err...)
return v
})
//var v Slice
if res != nil {
return res.(Slice)
}
return nil
}
func (this Map) GetMap(key string, err ...*Error) Map {
res := SafeMutex(this.GetTag(), func() interface{} {
v := ObjToMap((this)[key], err...)
return v
})
//var v Slice
if res != nil {
return res.(Map)
}
return nil
}
func (this Map) Get(key string, err ...*Error) interface{} {
res := SafeMutex(this.GetTag(), func() interface{} {
if v, ok := (this)[key]; ok {
return v
}
e := errors.New("没有存储key及对应的数据")
if len(err) != 0 {
err[0].SetError(e)
}
return nil
})
return res
}
//请传递指针过来
func (this Map) ToStruct(stct interface{}) {
SafeMutex(this.GetTag(), func() interface{} {
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))
}
}
return nil
})
}
func (this Map) ToJsonString() string {
res := SafeMutex(this.GetTag(), func() interface{} {
return ObjToStr(this)
})
return res.(string)
}
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 {
err[0].SetError(e)
}
return nil
})
}