2023-08-10 15:19:06 +08:00
|
|
|
|
// Package wrapper ...
|
|
|
|
|
//
|
|
|
|
|
// Description : wrapper ...
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2023-08-10 15:01
|
|
|
|
|
package wrapper
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-10 16:58:13 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
2023-08-10 15:19:06 +08:00
|
|
|
|
"errors"
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/easymap"
|
2023-10-12 18:50:56 +08:00
|
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
2023-08-10 15:19:06 +08:00
|
|
|
|
"reflect"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EasyMap ...
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 15:02 2023/8/10
|
|
|
|
|
func EasyMap(mapData interface{}) Map {
|
|
|
|
|
m, _ := EasyMapWithError(mapData)
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EasyMapWithError 转换map,并带上转换的异常
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 15:06 2023/8/10
|
|
|
|
|
func EasyMapWithError(mapData interface{}) (Map, error) {
|
2023-08-27 14:40:13 +08:00
|
|
|
|
if nil == mapData {
|
2023-12-24 22:53:27 +08:00
|
|
|
|
return easymap.NewNormal(), nil
|
2023-08-27 14:40:13 +08:00
|
|
|
|
}
|
2023-12-24 22:53:27 +08:00
|
|
|
|
m := easymap.NewNormal()
|
2023-08-10 15:19:06 +08:00
|
|
|
|
reflectType := reflect.TypeOf(mapData)
|
|
|
|
|
if reflectType.Kind() != reflect.Map {
|
2023-10-12 18:50:56 +08:00
|
|
|
|
mapFormatData := make(map[string]interface{})
|
|
|
|
|
if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByte(mapData), &mapFormatData); nil != err {
|
|
|
|
|
return m, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
|
|
|
|
|
}
|
|
|
|
|
mapData = mapFormatData
|
2023-08-10 15:19:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reflectValue := reflect.ValueOf(mapData).MapRange()
|
|
|
|
|
for reflectValue.Next() {
|
|
|
|
|
// 循环提取相关值
|
|
|
|
|
m.Set(reflectValue.Key().Interface(), reflectValue.Value().Interface())
|
|
|
|
|
}
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 16:58:13 +08:00
|
|
|
|
// EasyMapFromStruct 从struct转map
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 16:11 2023/8/10
|
|
|
|
|
func EasyMapFromStruct(data interface{}) Map {
|
|
|
|
|
byteData, _ := json.Marshal(data)
|
|
|
|
|
return EasyMapFromByte(byteData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EasyMapFromString 从string转为Map
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 16:12 2023/8/10
|
|
|
|
|
func EasyMapFromString(data string) Map {
|
|
|
|
|
return EasyMapFromByte([]byte(data))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EasyMapFromByte 从字节数组转为Map
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 16:12 2023/8/10
|
|
|
|
|
func EasyMapFromByte(data []byte) Map {
|
|
|
|
|
var tmpMap map[interface{}]interface{}
|
|
|
|
|
decoder := json.NewDecoder(bytes.NewReader(data))
|
|
|
|
|
decoder.UseNumber()
|
|
|
|
|
_ = decoder.Decode(&tmpMap)
|
|
|
|
|
return EasyMap(tmpMap)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 15:19:06 +08:00
|
|
|
|
// Map ...
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 15:14 2023/8/10
|
|
|
|
|
type Map easymap.EasyMap
|