iedc-go/map.go

154 lines
2.6 KiB
Go
Raw Normal View History

2017-08-04 08:20:59 +00:00
package hotime
import (
"encoding/json"
"errors"
"reflect"
)
//hotime的常用map
type Map map[string]interface{}
//获取string
func (this Map) GetString(key string, err ...*Error) string {
2017-09-05 02:55:14 +00:00
if len(err) != 0 {
err[0].SetError(nil)
}
return ObjToStr((this)[key])
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:55:14 +00:00
//if this==nil{
// this=Map{}
//}
this[key] = value
2017-08-04 08:20:59 +00:00
}
//删除接口
func (this Map) Delete(key string) {
2017-09-05 02:55:14 +00:00
delete(this, 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
func (this Map) GetInt(key string, err ...*Error) int {
2017-09-05 02:55:14 +00:00
v := ObjToInt((this)[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
}
2018-01-23 18:08:25 +00:00
2017-08-04 08:20:59 +00:00
//获取Int
func (this Map) GetInt64(key string, err ...*Error) int64 {
2017-09-05 02:55:14 +00:00
v := ObjToInt64((this)[key], err...)
return v
2017-08-04 08:20:59 +00:00
}
2018-01-23 18:08:25 +00:00
//获取向上取整Int64
func (this Map) GetCeilInt64(key string, err ...*Error) int64 {
v := ObjToCeilInt64((this)[key], err...)
return v
}
//获取向上取整Int
func (this Map) GetCeilInt(key string, err ...*Error) int {
v := ObjToCeilInt((this)[key], err...)
return v
}
2018-01-28 17:30:47 +00:00
//获取向上取整float64
func (this Map) GetCeilFloat64(key string, err ...*Error) float64 {
v := ObjToCeilFloat64((this)[key], err...)
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
func (this Map) GetFloat64(key string, err ...*Error) float64 {
2017-09-05 02:55:14 +00:00
v := ObjToFloat64((this)[key], err...)
return v
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
//var v Slice
2017-09-05 02:55:14 +00:00
v := ObjToSlice((this)[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
}
func (this 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
2017-09-05 02:55:14 +00:00
v := ObjToMap((this)[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
}
func (this Map) Get(key string, err ...*Error) interface{} {
2017-09-05 02:55:14 +00:00
if v, ok := (this)[key]; ok {
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
}
//请传递指针过来
func (this Map) ToStruct(stct interface{}) {
2017-09-05 02:55:14 +00:00
data := reflect.ValueOf(stct).Elem()
for k, v := range this {
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":
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-08-04 08:20:59 +00:00
}
func (this Map) ToJsonString() string {
2017-09-05 02:55:14 +00:00
return ObjToStr(this)
2017-08-04 08:20:59 +00:00
}
2017-09-05 02:40:37 +00:00
func (this Map) JsonToMap(jsonStr string, err ...*Error) {
2017-09-05 02:55:14 +00:00
e := json.Unmarshal([]byte(jsonStr), &this)
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
2017-08-04 08:20:59 +00:00
}