feat: 相关操作升级为泛型实现

This commit is contained in:
2025-10-13 17:05:00 +08:00
parent 80b5e4e7cc
commit 50d2d6c7bb
18 changed files with 316 additions and 1354 deletions

67
op_map/easymap.go Normal file
View File

@ -0,0 +1,67 @@
// Package op_map ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-08-10 15:01
package op_map
import (
"encoding/json"
"errors"
"reflect"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/tidwall/gjson"
)
// EasyMap ...
func EasyMap(mapData any) Map {
m, _ := EasyMapWithError(mapData)
return m
}
// EasyMapWithError 转换map并带上转换的异常
func EasyMapWithError(mapData any) (Map, error) {
if nil == mapData {
return map[string]any{}, nil
}
reflectType := reflect.TypeOf(mapData)
if reflectType.Kind() != reflect.Map {
mapFormatData := make(map[string]any)
if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByteIgnoreError(mapData), &mapFormatData); nil != err {
return mapFormatData, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
}
return mapFormatData, nil
}
m := Map(map[string]any{})
reflectValue := reflect.ValueOf(mapData).MapRange()
for reflectValue.Next() {
// 循环提取相关值
_ = m.Set(reflectValue.Key().String(), reflectValue.Value().Interface())
}
return m, nil
}
// EasyMapFromStruct 从struct转map
func EasyMapFromStruct(data any) Map {
byteData, _ := json.Marshal(data)
return EasyMapFromByte(byteData)
}
// EasyMapFromString 从string转为Map
func EasyMapFromString(data string) Map {
return EasyMapFromByte([]byte(data))
}
// EasyMapFromByte 从字节数组转为Map
func EasyMapFromByte(data []byte) Map {
res := Map(map[string]any{})
jsonRes := gjson.Parse(string(data))
jsonRes.ForEach(func(key, value gjson.Result) bool {
_ = res.Set(key.String(), value.Value())
return true
})
return res
}

289
op_map/map.go Normal file
View File

@ -0,0 +1,289 @@
// Package op_map ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-06 18:27
package op_map
import (
"errors"
"reflect"
"sync"
"git.zhangdeman.cn/zhangdeman/serialize"
"git.zhangdeman.cn/zhangdeman/wrapper/op_any"
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
)
var mapLock = &sync.RWMutex{}
type Map map[string]any
func (m Map) lock() {
mapLock.Lock()
}
func (m Map) unlock() {
mapLock.Unlock()
}
func (m Map) rLock() {
mapLock.RLock()
}
func (m Map) rUnlock() {
mapLock.RUnlock()
}
// Exist key是否存在
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:34 2024/11/6
func (m Map) Exist(key string) bool {
if m.IsNil() {
return false
}
m.rLock()
defer m.rUnlock()
_, exist := m[key]
return exist
}
// Set 设置map的值, 字段如果已存在, 会覆盖
//
// 参数说明:
// - field : 摇摆存的字段
// - value : 字段对应的值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:16 2024/11/19
func (m Map) Set(field string, value any) error {
if m.IsNil() {
return errors.New("Map is nil")
}
m.lock()
defer m.unlock()
m[field] = value
return nil
}
// Del 删除指定的字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:21 2024/11/19
func (m Map) Del(field string) {
if m.IsNil() {
return
}
m.lock()
defer m.unlock()
delete(m, field)
}
// IsNil 判断map是否为nil
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:22 2024/11/19
func (m Map) IsNil() bool {
if nil == m {
return true
}
return reflect.ValueOf(m).IsNil()
}
// Get ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:41 2024/11/19
func (m Map) Get(field string) (any, error) {
if m.IsNil() {
return nil, errors.New("map is nil")
}
m.rLock()
defer m.rUnlock()
val, exist := m[field]
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
func (m Map) GetDefault(field string, defaultValue any, allowNil bool) any {
if m.IsNil() {
return defaultValue
}
m.rLock()
defer m.rUnlock()
val, exist := m[field]
if !exist {
return defaultValue
}
if nil == val {
if allowNil {
return val
}
return defaultValue
}
return val
}
// Value 获取数据值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:39 2024/11/6
func (m Map) Value() map[string]any {
if m.IsNil() {
return nil
}
return m
}
// Clone 克隆数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:40 2024/11/6
func (m Map) Clone() Map {
newData := map[string]any{}
if m.IsNil() {
return newData
}
m.rLock()
defer m.rUnlock()
mapValue := m.Value()
for k, v := range mapValue {
newData[k] = v
}
return newData
}
// Filter 过滤指定字段
//
// 参数说明:
// - fieldList : 要保留的字段列表
// - ignoreNotFound : 指定字段不存在是否忽略,如不忽略, 字段不存在, 将会报错
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:40 2024/11/19
func (m Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, error) {
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
func (m Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]any {
res := make(map[string]any)
for itemField, fieldDefaultValue := range fieldMap {
res[itemField] = m.GetDefault(itemField, fieldDefaultValue, allowNil)
}
return res
}
// MarshalJSON Map序列化
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:35 2024/11/19
func (m Map) MarshalJSON() ([]byte, error) {
mapData := m.Value()
if nil == mapData {
return nil, nil
}
return serialize.JSON.MarshalForByte(mapData)
}
// ToString 序列化成字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:24 2024/11/21
func (m Map) ToString() string {
byteData, _ := m.MarshalJSON()
return string(byteData)
}
// 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 op_any.AnyDataType(val).ToString(), nil
}
// 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 := op_string.ToBaseTypeValue[int64](op_any.AnyDataType(val).ToString())
return int64Res.Value, int64Res.Err
}
// GetFloat64 获取float64值
//
// 参数说明:
// - field : 要查找的字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:18 2024/11/21
func (m Map) GetFloat64(field string) (float64, error) {
val, err := m.Get(field)
if nil != err {
return 0, err
}
float64Res := op_string.ToBaseTypeValue[float64](op_any.AnyDataType(val).ToString())
return float64Res.Value, float64Res.Err
}