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

View File

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