This commit is contained in:
白茶清欢 2024-11-21 15:11:50 +08:00
parent 3bb36680ad
commit 4196d342f7
2 changed files with 28 additions and 31 deletions

49
map.go
View File

@ -18,19 +18,19 @@ var mapLock = &sync.RWMutex{}
type Map map[string]any type Map map[string]any
func (m *Map) lock() { func (m Map) lock() {
mapLock.Lock() mapLock.Lock()
} }
func (m *Map) unlock() { func (m Map) unlock() {
mapLock.Unlock() mapLock.Unlock()
} }
func (m *Map) rLock() { func (m Map) rLock() {
mapLock.RLock() mapLock.RLock()
} }
func (m *Map) rUnlock() { func (m Map) rUnlock() {
mapLock.RUnlock() mapLock.RUnlock()
} }
@ -39,14 +39,13 @@ func (m *Map) rUnlock() {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:34 2024/11/6 // Date : 18:34 2024/11/6
func (m *Map) Exist(key string) bool { func (m Map) Exist(key string) bool {
if m.IsNil() { if m.IsNil() {
return false return false
} }
m.rLock() m.rLock()
defer m.rUnlock() defer m.rUnlock()
v := *m _, exist := m[key]
_, exist := v[key]
return exist return exist
} }
@ -59,13 +58,13 @@ func (m *Map) Exist(key string) bool {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:16 2024/11/19 // Date : 15:16 2024/11/19
func (m *Map) Set(field string, value any) error { func (m Map) Set(field string, value any) error {
if m.IsNil() { if m.IsNil() {
return errors.New("Map is nil") return errors.New("Map is nil")
} }
m.lock() m.lock()
defer m.unlock() defer m.unlock()
(*m)[field] = value m[field] = value
return nil return nil
} }
@ -74,13 +73,13 @@ func (m *Map) Set(field string, value any) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:21 2024/11/19 // Date : 15:21 2024/11/19
func (m *Map) Del(field string) { func (m Map) Del(field string) {
if m.IsNil() { if m.IsNil() {
return return
} }
m.lock() m.lock()
defer m.unlock() defer m.unlock()
delete(*m, field) delete(m, field)
} }
// IsNil 判断map是否为nil // IsNil 判断map是否为nil
@ -88,11 +87,11 @@ func (m *Map) Del(field string) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:22 2024/11/19 // Date : 15:22 2024/11/19
func (m *Map) IsNil() bool { func (m Map) IsNil() bool {
if nil == m { if nil == m {
return true return true
} }
return reflect.ValueOf(*m).IsNil() return reflect.ValueOf(m).IsNil()
} }
// Get ... // Get ...
@ -100,14 +99,13 @@ func (m *Map) IsNil() bool {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:41 2024/11/19 // Date : 14:41 2024/11/19
func (m *Map) Get(field string) (any, error) { func (m Map) Get(field string) (any, error) {
if m.IsNil() { if m.IsNil() {
return nil, errors.New("map is nil") return nil, errors.New("map is nil")
} }
m.rLock() m.rLock()
defer m.rUnlock() defer m.rUnlock()
v := *m val, exist := m[field]
val, exist := v[field]
if !exist { if !exist {
return nil, errors.New(field + " : field not found") return nil, errors.New(field + " : field not found")
} }
@ -124,14 +122,13 @@ func (m *Map) Get(field string) (any, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:59 2024/11/19 // Date : 14:59 2024/11/19
func (m *Map) GetDefault(field string, defaultValue any, allowNil bool) any { func (m Map) GetDefault(field string, defaultValue any, allowNil bool) any {
if m.IsNil() { if m.IsNil() {
return defaultValue return defaultValue
} }
m.rLock() m.rLock()
defer m.rUnlock() defer m.rUnlock()
v := *m val, exist := m[field]
val, exist := v[field]
if !exist { if !exist {
return defaultValue return defaultValue
} }
@ -149,11 +146,11 @@ func (m *Map) GetDefault(field string, defaultValue any, allowNil bool) any {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:39 2024/11/6 // Date : 19:39 2024/11/6
func (m *Map) Value() map[string]any { func (m Map) Value() map[string]any {
if m.IsNil() { if m.IsNil() {
return nil return nil
} }
return *m return m
} }
// Clone 克隆数据 // Clone 克隆数据
@ -161,7 +158,7 @@ func (m *Map) Value() map[string]any {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:40 2024/11/6 // Date : 19:40 2024/11/6
func (m *Map) Clone() Map { func (m Map) Clone() Map {
newData := map[string]any{} newData := map[string]any{}
if m.IsNil() { if m.IsNil() {
return newData return newData
@ -184,7 +181,7 @@ func (m *Map) Clone() Map {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:40 2024/11/19 // Date : 14:40 2024/11/19
func (m *Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, error) { func (m Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, error) {
res := make(map[string]any) res := make(map[string]any)
for _, itemField := range fieldList { for _, itemField := range fieldList {
if val, err := m.Get(itemField); err == nil { if val, err := m.Get(itemField); err == nil {
@ -207,7 +204,7 @@ func (m *Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, e
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:07 2024/11/19 // Date : 15:07 2024/11/19
func (m *Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]any { func (m Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]any {
res := make(map[string]any) res := make(map[string]any)
for itemField, fieldDefaultValue := range fieldMap { for itemField, fieldDefaultValue := range fieldMap {
res[itemField] = m.GetDefault(itemField, fieldDefaultValue, allowNil) res[itemField] = m.GetDefault(itemField, fieldDefaultValue, allowNil)
@ -220,7 +217,7 @@ func (m *Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]a
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:35 2024/11/19 // Date : 15:35 2024/11/19
func (m *Map) MarshalJSON() ([]byte, error) { func (m Map) MarshalJSON() ([]byte, error) {
mapData := m.Value() mapData := m.Value()
if nil == mapData { if nil == mapData {
return nil, nil return nil, nil
@ -233,7 +230,7 @@ func (m *Map) MarshalJSON() ([]byte, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:24 2024/11/21 // Date : 14:24 2024/11/21
func (m *Map) ToString() string { func (m Map) ToString() string {
byteData, _ := m.MarshalJSON() byteData, _ := m.MarshalJSON()
return string(byteData) return string(byteData)
} }

View File

@ -136,21 +136,21 @@ func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map,
} }
var ( var (
inputFieldVal any inputFieldVal any
inputFieldValExist bool inputFieldValExist error
storageFieldVal any storageFieldVal any
storageFieldValExist bool storageFieldValExist error
) )
inputFieldVal, inputFieldValExist = inputVal.Get(field) inputFieldVal, inputFieldValExist = inputVal.Get(field)
storageFieldVal, storageFieldValExist = storageVal.Get(field) storageFieldVal, storageFieldValExist = storageVal.Get(field)
// 字段在输入数据和存储数据中均不存在 // 字段在输入数据和存储数据中均不存在
if !inputFieldValExist && !storageFieldValExist { if nil != inputFieldValExist && nil != storageFieldValExist {
// 输入和存储都没这个字段 // 输入和存储都没这个字段
return result return result
} }
// 判断输入字段是否存在 // 判断输入字段是否存在
if !inputFieldValExist { if nil != inputFieldValExist {
if option.IgnoreNotFoundField { if option.IgnoreNotFoundField {
// 忽略不存在的字段 // 忽略不存在的字段
return result return result
@ -162,7 +162,7 @@ func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map,
return result return result
} }
// 判断存储字段是否存在 // 判断存储字段是否存在
if !storageFieldValExist { if nil != storageFieldValExist {
result.IsSame = false result.IsSame = false
result.DiffReason = DiffReasonStorageFieldNotFound result.DiffReason = DiffReasonStorageFieldNotFound
result.NewVal = inputFieldVal result.NewVal = inputFieldVal