ff410b94c3
- 更新应用程序处理程序中的 URL 赋值逻辑,确保静态文件使用原始路径 - 修改缓存数据库的 JSON 解析方法,使用 JsonToObj 函数替代 json.Unmarshal,提升代码可读性和性能 - 在 Map 和 Slice 类型中新增获取四舍五入浮点数的方法,增强数据处理能力 - 在 Obj 类型中添加四舍五入功能,支持精度控制 - 改进数据库查询结果的处理逻辑,确保数据类型的准确性和一致性 - 优化日志格式设置,增强日志信息的可读性
203 lines
3.5 KiB
Go
203 lines
3.5 KiB
Go
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
// hotime的常用map
|
|
type Map map[string]interface{}
|
|
|
|
// 获取string
|
|
func (that Map) GetString(key string, err ...*Error) string {
|
|
|
|
if len(err) != 0 {
|
|
err[0].SetError(nil)
|
|
}
|
|
return ObjToStr((that)[key])
|
|
|
|
}
|
|
|
|
func (that *Map) Pointer() *Map {
|
|
|
|
return that
|
|
}
|
|
|
|
// 增加接口
|
|
func (that Map) Put(key string, value interface{}) {
|
|
//if that==nil{
|
|
// that=Map{}
|
|
//}
|
|
that[key] = value
|
|
}
|
|
|
|
// 删除接口
|
|
func (that Map) Delete(key string) {
|
|
delete(that, key)
|
|
|
|
}
|
|
|
|
// 获取Int
|
|
func (that Map) GetInt(key string, err ...*Error) int {
|
|
v := ObjToInt((that)[key], err...)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
// 获取Int
|
|
func (that Map) GetInt64(key string, err ...*Error) int64 {
|
|
v := ObjToInt64((that)[key], err...)
|
|
return v
|
|
|
|
}
|
|
|
|
// 获取向上取整Int64
|
|
func (that Map) GetCeilInt64(key string, err ...*Error) int64 {
|
|
v := ObjToCeilInt64((that)[key], err...)
|
|
return v
|
|
|
|
}
|
|
|
|
// 获取向上取整Int
|
|
func (that Map) GetCeilInt(key string, err ...*Error) int {
|
|
v := ObjToCeilInt((that)[key], err...)
|
|
return v
|
|
|
|
}
|
|
|
|
// 获取向上取整float64
|
|
func (that Map) GetCeilFloat64(key string, err ...*Error) float64 {
|
|
v := ObjToCeilFloat64((that)[key], err...)
|
|
return v
|
|
|
|
}
|
|
|
|
// 获取Float64
|
|
func (that Map) GetFloat64(key string, err ...*Error) float64 {
|
|
|
|
v := ObjToFloat64((that)[key], err...)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
func (that Map) GetSlice(key string, err ...*Error) Slice {
|
|
|
|
//var v Slice
|
|
v := ObjToSlice((that)[key], err...)
|
|
|
|
return v
|
|
|
|
}
|
|
func (that Map) GetBool(key string, err ...*Error) bool {
|
|
|
|
//var v Slice
|
|
v := ObjToBool((that)[key], err...)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
func (that Map) GetTime(key string, err ...*Error) *time.Time {
|
|
|
|
v := ObjToTime((that)[key], err...)
|
|
return v
|
|
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (that Map) GetMap(key string, err ...*Error) Map {
|
|
//var data Slice
|
|
|
|
v := ObjToMap((that)[key], err...)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
func (that Map) Get(key string, err ...*Error) interface{} {
|
|
|
|
if v, ok := (that)[key]; ok {
|
|
return v
|
|
}
|
|
e := errors.New("没有存储key及对应的数据")
|
|
|
|
if len(err) != 0 {
|
|
err[0].SetError(e)
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 请传递指针过来
|
|
func (that Map) ToStruct(stct interface{}) {
|
|
|
|
data := reflect.ValueOf(stct).Elem()
|
|
for k, v := range that {
|
|
ks := StrFirstToUpper(k)
|
|
dkey := data.FieldByName(ks)
|
|
if !dkey.IsValid() {
|
|
continue
|
|
}
|
|
switch dkey.Type().String() {
|
|
case "int":
|
|
dkey.SetInt(that.GetInt64(k))
|
|
case "int64":
|
|
dkey.Set(reflect.ValueOf(that.GetInt64(k)))
|
|
case "float64":
|
|
dkey.Set(reflect.ValueOf(that.GetFloat64(k)))
|
|
case "string":
|
|
dkey.Set(reflect.ValueOf(that.GetString(k)))
|
|
case "interface{}":
|
|
dkey.Set(reflect.ValueOf(v))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (that Map) ToJsonString() string {
|
|
return ObjToStr(that)
|
|
|
|
}
|
|
|
|
func (that Map) JsonToMap(jsonStr string, err ...*Error) {
|
|
dec := json.NewDecoder(bytes.NewReader([]byte(jsonStr)))
|
|
dec.UseNumber()
|
|
e := dec.Decode(&that)
|
|
if e != nil {
|
|
if len(err) != 0 {
|
|
err[0].SetError(e)
|
|
}
|
|
return
|
|
}
|
|
for k, v := range that {
|
|
that[k] = fixJsonNumbers(v)
|
|
}
|
|
}
|
|
|
|
// GetRoundFloat64 获取四舍五入到 precision 位小数的 float64
|
|
func (that Map) GetRoundFloat64(key string, precision int, err ...*Error) float64 {
|
|
return ObjToRoundFloat64((that)[key], precision, err...)
|
|
}
|