From 118ef5d59a0daf430e3f693724b50293cbd8a56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 6 Nov 2024 18:26:49 +0800 Subject: [PATCH 1/9] rename file : map.go -> easymap.go --- map.go => easymap.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename map.go => easymap.go (100%) diff --git a/map.go b/easymap.go similarity index 100% rename from map.go rename to easymap.go From dac3a32e6f2700d74b09a0c01ff80c731ab5c442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 6 Nov 2024 18:29:18 +0800 Subject: [PATCH 2/9] easy map --- easymap.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/easymap.go b/easymap.go index 6013835..cbd2a56 100644 --- a/easymap.go +++ b/easymap.go @@ -21,7 +21,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 15:02 2023/8/10 -func EasyMap(mapData any) Map { +func EasyMap(mapData any) easymap.EasyMap { m, _ := EasyMapWithError(mapData) return m } @@ -31,7 +31,7 @@ func EasyMap(mapData any) Map { // Author : go_developer@163.com<白茶清欢> // // Date : 15:06 2023/8/10 -func EasyMapWithError(mapData any) (Map, error) { +func EasyMapWithError(mapData any) (easymap.EasyMap, error) { if nil == mapData { return easymap.NewNormal(), nil } @@ -58,7 +58,7 @@ func EasyMapWithError(mapData any) (Map, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 16:11 2023/8/10 -func EasyMapFromStruct(data any) Map { +func EasyMapFromStruct(data any) easymap.EasyMap { byteData, _ := json.Marshal(data) return EasyMapFromByte(byteData) } @@ -68,7 +68,7 @@ func EasyMapFromStruct(data any) Map { // Author : go_developer@163.com<白茶清欢> // // Date : 16:12 2023/8/10 -func EasyMapFromString(data string) Map { +func EasyMapFromString(data string) easymap.EasyMap { return EasyMapFromByte([]byte(data)) } @@ -77,7 +77,7 @@ func EasyMapFromString(data string) Map { // Author : go_developer@163.com<白茶清欢> // // Date : 16:12 2023/8/10 -func EasyMapFromByte(data []byte) Map { +func EasyMapFromByte(data []byte) easymap.EasyMap { res := easymap.NewNormal() jsonRes := gjson.Parse(string(data)) jsonRes.ForEach(func(key, value gjson.Result) bool { @@ -86,10 +86,3 @@ func EasyMapFromByte(data []byte) Map { }) return res } - -// Map ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:14 2023/8/10 -type Map easymap.EasyMap From 822aabaac68ab398e46d988eacd585c289675661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 6 Nov 2024 18:39:32 +0800 Subject: [PATCH 3/9] update MapResult --- define.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/define.go b/define.go index 1df5f37..f8c377e 100644 --- a/define.go +++ b/define.go @@ -7,7 +7,10 @@ // Date : 2023-05-05 14:44 package wrapper -import "time" +import ( + "git.zhangdeman.cn/zhangdeman/easymap" + "time" +) // Int8Result ... // @@ -465,7 +468,7 @@ type StringSliceResult struct { // // Date : 16:05 2023/8/10 type MapResult struct { - Value Map + Value easymap.EasyMap Err error } From 5ea67a5b6c594e8eef3a5be390e6ed599ba2b955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 6 Nov 2024 18:40:49 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E5=AE=8C=E6=88=90map=E5=8C=85=E8=A3=85?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=20-=20key=E6=98=AF=E5=90=A6=E5=AD=98?= =?UTF-8?q?=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ map_test.go | 21 +++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 map.go create mode 100644 map_test.go diff --git a/map.go b/map.go new file mode 100644 index 0000000..6d23965 --- /dev/null +++ b/map.go @@ -0,0 +1,46 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-11-06 18:27 +package wrapper + +import "sync" + +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 nil == m { + return false + } + m.rlock() + defer m.unlock() + v := *m + _, exist := v[key] + return exist +} diff --git a/map_test.go b/map_test.go new file mode 100644 index 0000000..b5e022e --- /dev/null +++ b/map_test.go @@ -0,0 +1,21 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-11-06 18:37 +package wrapper + +import ( + "fmt" + "testing" +) + +func TestMap_Exist(t *testing.T) { + testData := Map(map[string]any{ + "name": "zhang", + }) + fmt.Println(testData.Exist("name")) + fmt.Println(testData.Exist("age")) +} From fcaa7ee1c243bad4cf4c9113b750baf19cc24bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 6 Nov 2024 19:50:09 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0map=20clone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/map.go b/map.go index 6d23965..9a16202 100644 --- a/map.go +++ b/map.go @@ -44,3 +44,34 @@ func (m *Map) Exist(key string) bool { _, exist := v[key] return exist } + +// Value 获取数据值 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:39 2024/11/6 +func (m *Map) Value() Map { + if nil == m { + return map[string]any{} + } + return *m +} + +// Clone 克隆数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:40 2024/11/6 +func (m *Map) Clone() Map { + newData := map[string]any{} + if nil == m { + return newData + } + m.rlock() + defer m.unlock() + mapValue := m.Value() + for k, v := range mapValue { + newData[k] = v + } + return newData +} From 51d0a05ec359500e55ef01984291bff10f6459da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 19 Nov 2024 15:12:48 +0800 Subject: [PATCH 6/9] =?UTF-8?q?map=20=E5=A2=9E=E5=8A=A0Get=E4=B8=8EFilter?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/map.go b/map.go index 9a16202..ab8912c 100644 --- a/map.go +++ b/map.go @@ -7,7 +7,10 @@ // Date : 2024-11-06 18:27 package wrapper -import "sync" +import ( + "errors" + "sync" +) var mapLock = &sync.RWMutex{} @@ -21,7 +24,7 @@ func (m *Map) unlock() { mapLock.Unlock() } -func (m *Map) rlock() { +func (m *Map) rLock() { mapLock.RLock() } @@ -38,13 +41,62 @@ func (m *Map) Exist(key string) bool { if nil == m { return false } - m.rlock() - defer m.unlock() + m.rLock() + defer m.rUnlock() v := *m _, exist := v[key] return exist } +// Get ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:41 2024/11/19 +func (m *Map) Get(field string) (any, error) { + if nil == m { + return nil, errors.New("map is nil") + } + m.rLock() + defer m.rUnlock() + v := *m + val, exist := v[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 nil == m { + return defaultValue + } + m.rLock() + defer m.rUnlock() + v := *m + val, exist := v[field] + if !exist { + return defaultValue + } + if nil == val { + if allowNil { + return val + } + return defaultValue + } + return val +} + // Value 获取数据值 // // Author : go_developer@163.com<白茶清欢> @@ -67,11 +119,51 @@ func (m *Map) Clone() Map { if nil == m { return newData } - m.rlock() - defer m.unlock() + 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 +} From e95ed61cfc4b09482e6ef8b5069aec7cb878a45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 19 Nov 2024 15:20:25 +0800 Subject: [PATCH 7/9] =?UTF-8?q?Map=E6=8F=90=E4=BE=9BSet=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/map.go b/map.go index ab8912c..6de1fd3 100644 --- a/map.go +++ b/map.go @@ -48,6 +48,25 @@ func (m *Map) Exist(key string) bool { 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 nil == m { + return errors.New("Map is nil") + } + m.lock() + defer m.unlock() + (*m)[field] = value + return nil +} + // Get ... // // Author : go_developer@163.com<白茶清欢> From db7bf0817e8495dc3974af2a972f3d3f357a4a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 19 Nov 2024 15:28:47 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Map=E4=B8=BANil=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 37 ++++++++++++++++++++++++++++++++----- map_test.go | 9 +++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/map.go b/map.go index 6de1fd3..46145c4 100644 --- a/map.go +++ b/map.go @@ -9,6 +9,7 @@ package wrapper import ( "errors" + "reflect" "sync" ) @@ -38,7 +39,7 @@ func (m *Map) rUnlock() { // // Date : 18:34 2024/11/6 func (m *Map) Exist(key string) bool { - if nil == m { + if m.IsNil() { return false } m.rLock() @@ -58,7 +59,7 @@ func (m *Map) Exist(key string) bool { // // Date : 15:16 2024/11/19 func (m *Map) Set(field string, value any) error { - if nil == m { + if m.IsNil() { return errors.New("Map is nil") } m.lock() @@ -67,13 +68,39 @@ func (m *Map) Set(field string, value any) error { 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 nil == m { + if m.IsNil() { return nil, errors.New("map is nil") } m.rLock() @@ -97,7 +124,7 @@ func (m *Map) Get(field string) (any, error) { // // Date : 14:59 2024/11/19 func (m *Map) GetDefault(field string, defaultValue any, allowNil bool) any { - if nil == m { + if m.IsNil() { return defaultValue } m.rLock() @@ -135,7 +162,7 @@ func (m *Map) Value() Map { // Date : 19:40 2024/11/6 func (m *Map) Clone() Map { newData := map[string]any{} - if nil == m { + if m.IsNil() { return newData } m.rLock() diff --git a/map_test.go b/map_test.go index b5e022e..e57cc26 100644 --- a/map_test.go +++ b/map_test.go @@ -19,3 +19,12 @@ func TestMap_Exist(t *testing.T) { fmt.Println(testData.Exist("name")) fmt.Println(testData.Exist("age")) } + +func TestMap_IsNil(t *testing.T) { + var ( + m Map + m1 *Map + ) + fmt.Println(m.Set("a", 1)) + fmt.Println(m.IsNil(), m1.IsNil()) +} From 0d6d72b07ee89e279a6944d6af079df26632a562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 19 Nov 2024 15:39:37 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Map=20MarshalJSON=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 20 +++++++++++++++++--- map_test.go | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/map.go b/map.go index 46145c4..16ae07c 100644 --- a/map.go +++ b/map.go @@ -9,6 +9,7 @@ package wrapper import ( "errors" + "git.zhangdeman.cn/zhangdeman/serialize" "reflect" "sync" ) @@ -148,9 +149,9 @@ 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 { - if nil == m { - return map[string]any{} +func (m *Map) Value() map[string]any { + if m.IsNil() { + return nil } return *m } @@ -213,3 +214,16 @@ func (m *Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]a } 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) +} diff --git a/map_test.go b/map_test.go index e57cc26..58652ee 100644 --- a/map_test.go +++ b/map_test.go @@ -28,3 +28,20 @@ func TestMap_IsNil(t *testing.T) { fmt.Println(m.Set("a", 1)) fmt.Println(m.IsNil(), m1.IsNil()) } + +func TestMap_IsMasher(t *testing.T) { + var ( + m Map + m1 = Map(map[string]any{ + "a": 1, + "b": m, + "c": Map(map[string]any{ + "name": "de", + }), + }) + ) + d, err := m.MarshalJSON() + fmt.Println(string(d), err) + d, err = m1.MarshalJSON() + fmt.Println(string(d), err) +}