2024-11-06 18:40:49 +08:00
|
|
|
// Package wrapper ...
|
|
|
|
//
|
|
|
|
// Description : wrapper ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2024-11-06 18:27
|
|
|
|
package wrapper
|
|
|
|
|
2024-11-19 15:12:48 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
2024-11-19 15:39:37 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
2024-11-19 15:28:47 +08:00
|
|
|
"reflect"
|
2024-11-19 15:12:48 +08:00
|
|
|
"sync"
|
|
|
|
)
|
2024-11-06 18:40:49 +08:00
|
|
|
|
|
|
|
var mapLock = &sync.RWMutex{}
|
|
|
|
|
|
|
|
type Map map[string]any
|
|
|
|
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) lock() {
|
2024-11-06 18:40:49 +08:00
|
|
|
mapLock.Lock()
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) unlock() {
|
2024-11-06 18:40:49 +08:00
|
|
|
mapLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) rLock() {
|
2024-11-06 18:40:49 +08:00
|
|
|
mapLock.RLock()
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) rUnlock() {
|
2024-11-06 18:40:49 +08:00
|
|
|
mapLock.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exist key是否存在
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:34 2024/11/6
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) Exist(key string) bool {
|
2024-11-19 15:28:47 +08:00
|
|
|
if m.IsNil() {
|
2024-11-06 18:40:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2024-11-19 15:12:48 +08:00
|
|
|
m.rLock()
|
|
|
|
defer m.rUnlock()
|
2024-11-21 15:11:50 +08:00
|
|
|
_, exist := m[key]
|
2024-11-06 18:40:49 +08:00
|
|
|
return exist
|
|
|
|
}
|
2024-11-06 19:50:09 +08:00
|
|
|
|
2024-11-19 15:20:25 +08:00
|
|
|
// Set 设置map的值, 字段如果已存在, 会覆盖
|
|
|
|
//
|
|
|
|
// 参数说明:
|
|
|
|
// - field : 摇摆存的字段
|
|
|
|
// - value : 字段对应的值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:16 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) Set(field string, value any) error {
|
2024-11-19 15:28:47 +08:00
|
|
|
if m.IsNil() {
|
2024-11-19 15:20:25 +08:00
|
|
|
return errors.New("Map is nil")
|
|
|
|
}
|
|
|
|
m.lock()
|
|
|
|
defer m.unlock()
|
2024-11-21 15:11:50 +08:00
|
|
|
m[field] = value
|
2024-11-19 15:20:25 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-19 15:28:47 +08:00
|
|
|
// Del 删除指定的字段
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:21 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) Del(field string) {
|
2024-11-19 15:28:47 +08:00
|
|
|
if m.IsNil() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.lock()
|
|
|
|
defer m.unlock()
|
2024-11-21 15:11:50 +08:00
|
|
|
delete(m, field)
|
2024-11-19 15:28:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil 判断map是否为nil
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:22 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) IsNil() bool {
|
2024-11-19 15:28:47 +08:00
|
|
|
if nil == m {
|
|
|
|
return true
|
|
|
|
}
|
2024-11-21 15:11:50 +08:00
|
|
|
return reflect.ValueOf(m).IsNil()
|
2024-11-19 15:28:47 +08:00
|
|
|
}
|
|
|
|
|
2024-11-19 15:12:48 +08:00
|
|
|
// Get ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:41 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) Get(field string) (any, error) {
|
2024-11-19 15:28:47 +08:00
|
|
|
if m.IsNil() {
|
2024-11-19 15:12:48 +08:00
|
|
|
return nil, errors.New("map is nil")
|
|
|
|
}
|
|
|
|
m.rLock()
|
|
|
|
defer m.rUnlock()
|
2024-11-21 15:11:50 +08:00
|
|
|
val, exist := m[field]
|
2024-11-19 15:12:48 +08:00
|
|
|
if !exist {
|
|
|
|
return nil, errors.New(field + " : field not found")
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefault 获取指定字段, 不存在则设置默认值
|
|
|
|
//
|
|
|
|
// 参数说明:
|
|
|
|
// - field : 要读取的字段
|
|
|
|
// - defaultValue : 字段不存在返回的默认值
|
|
|
|
// - allowNil : 字段存在, 但是值为Nil, 是否是一个合法值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:59 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) GetDefault(field string, defaultValue any, allowNil bool) any {
|
2024-11-19 15:28:47 +08:00
|
|
|
if m.IsNil() {
|
2024-11-19 15:12:48 +08:00
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
m.rLock()
|
|
|
|
defer m.rUnlock()
|
2024-11-21 15:11:50 +08:00
|
|
|
val, exist := m[field]
|
2024-11-19 15:12:48 +08:00
|
|
|
if !exist {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
if nil == val {
|
|
|
|
if allowNil {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2024-11-06 19:50:09 +08:00
|
|
|
// Value 获取数据值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 19:39 2024/11/6
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) Value() map[string]any {
|
2024-11-19 15:39:37 +08:00
|
|
|
if m.IsNil() {
|
|
|
|
return nil
|
2024-11-06 19:50:09 +08:00
|
|
|
}
|
2024-11-21 15:11:50 +08:00
|
|
|
return m
|
2024-11-06 19:50:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone 克隆数据
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 19:40 2024/11/6
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) Clone() Map {
|
2024-11-06 19:50:09 +08:00
|
|
|
newData := map[string]any{}
|
2024-11-19 15:28:47 +08:00
|
|
|
if m.IsNil() {
|
2024-11-06 19:50:09 +08:00
|
|
|
return newData
|
|
|
|
}
|
2024-11-19 15:12:48 +08:00
|
|
|
m.rLock()
|
|
|
|
defer m.rUnlock()
|
2024-11-06 19:50:09 +08:00
|
|
|
mapValue := m.Value()
|
|
|
|
for k, v := range mapValue {
|
|
|
|
newData[k] = v
|
|
|
|
}
|
|
|
|
return newData
|
|
|
|
}
|
2024-11-19 15:12:48 +08:00
|
|
|
|
|
|
|
// Filter 过滤指定字段
|
|
|
|
//
|
|
|
|
// 参数说明:
|
|
|
|
// - fieldList : 要保留的字段列表
|
|
|
|
// - ignoreNotFound : 指定字段不存在是否忽略,如不忽略, 字段不存在, 将会报错
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:40 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, error) {
|
2024-11-19 15:12:48 +08:00
|
|
|
res := make(map[string]any)
|
|
|
|
for _, itemField := range fieldList {
|
|
|
|
if val, err := m.Get(itemField); err == nil {
|
|
|
|
res[itemField] = val
|
|
|
|
} else {
|
|
|
|
if !ignoreNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterDefault 过滤指定字段, 字段不存储在则用默认值填充
|
|
|
|
//
|
|
|
|
// 参数说明:
|
|
|
|
// - fieldMap : 查询字段表, key为要查询的字段, value为 字段不存在时返回的默认值
|
|
|
|
// - allowNil : 字段存在, 三只值为你来是否是一个合法值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:07 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]any {
|
2024-11-19 15:12:48 +08:00
|
|
|
res := make(map[string]any)
|
|
|
|
for itemField, fieldDefaultValue := range fieldMap {
|
|
|
|
res[itemField] = m.GetDefault(itemField, fieldDefaultValue, allowNil)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2024-11-19 15:39:37 +08:00
|
|
|
|
|
|
|
// MarshalJSON Map序列化
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:35 2024/11/19
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) MarshalJSON() ([]byte, error) {
|
2024-11-19 15:39:37 +08:00
|
|
|
mapData := m.Value()
|
|
|
|
if nil == mapData {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return serialize.JSON.MarshalForByte(mapData)
|
|
|
|
}
|
2024-11-21 14:28:15 +08:00
|
|
|
|
|
|
|
// ToString 序列化成字符串
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:24 2024/11/21
|
2024-11-21 15:11:50 +08:00
|
|
|
func (m Map) ToString() string {
|
2024-11-21 14:28:15 +08:00
|
|
|
byteData, _ := m.MarshalJSON()
|
|
|
|
return string(byteData)
|
|
|
|
}
|
2024-11-21 15:16:30 +08:00
|
|
|
|
|
|
|
// GetString 获取字符串结果
|
|
|
|
//
|
|
|
|
// 参数说明:
|
|
|
|
// - field : 要查找的字段
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:15 2024/11/21
|
|
|
|
func (m Map) GetString(field string) (string, error) {
|
|
|
|
val, err := m.Get(field)
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return AnyDataType(val).ToString().Value(), nil
|
|
|
|
}
|
2024-11-21 15:20:31 +08:00
|
|
|
|
|
|
|
// GetInt64 获取Int64值
|
|
|
|
//
|
|
|
|
// 参数说明:
|
|
|
|
// - field : 要查找的字段
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:18 2024/11/21
|
|
|
|
func (m Map) GetInt64(field string) (int64, error) {
|
|
|
|
val, err := m.Get(field)
|
|
|
|
if nil != err {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
int64Res := AnyDataType(val).ToString().ToInt64()
|
|
|
|
return int64Res.Value, int64Res.Err
|
|
|
|
}
|