From 570814cf6e9252d8eee3e1d997bd430540fb7667 Mon Sep 17 00:00:00 2001 From: huangqian Date: Mon, 15 Nov 2021 23:34:25 +0800 Subject: [PATCH 01/22] gmap example function 1.NewFrom 2.NewHashMap 3.NewHashMapFrom --- container/gmap/gmap_z_example_any_any_test.go | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index cee0ab69d..97fccd448 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -74,6 +74,44 @@ func ExampleNew() { // 0 } +func ExampleNewFrom() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) + + n := gmap.NewFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} + +func ExampleNewHashMap() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) + + // Output: + // {"key1":"val1"} +} + +func ExampleNewHashMapFrom() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) + + n := gmap.NewFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} + func ExampleAnyAnyMap_Keys() { var m gmap.Map m.Sets(g.MapAnyAny{ From cc9b3a08d01e6875a6b3f7fab1bcafeda4e09135 Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 16 Nov 2021 07:20:18 +0800 Subject: [PATCH 02/22] splite gmap example file. --- container/gmap/gmap_z_example_test.go | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 container/gmap/gmap_z_example_test.go diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_test.go new file mode 100644 index 000000000..1f1148300 --- /dev/null +++ b/container/gmap/gmap_z_example_test.go @@ -0,0 +1,112 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with gm file, +// You can obtain one at https://github.com/gogf/gf. + +package gmap_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/container/gmap" +) + +func ExampleNew() { + m := gmap.New() + + // Add data. + m.Set("key1", "val1") + + // Print size. + fmt.Println(m.Size()) + + addMap := make(map[interface{}]interface{}) + addMap["key2"] = "val2" + addMap["key3"] = "val3" + addMap[1] = 1 + + fmt.Println(m.Values()) + + // Batch add data. + m.Sets(addMap) + + // Gets the value of the corresponding key. + fmt.Println(m.Get("key3")) + + // Get the value by key, or set it with given key-value if not exist. + fmt.Println(m.GetOrSet("key4", "val4")) + + // Set key-value if the key does not exist, then return true; or else return false. + fmt.Println(m.SetIfNotExist("key3", "val3")) + + // Remove key + m.Remove("key2") + fmt.Println(m.Keys()) + + // Batch remove keys. + m.Removes([]interface{}{"key1", 1}) + fmt.Println(m.Keys()) + + // Contains checks whether a key exists. + fmt.Println(m.Contains("key3")) + + // Flip exchanges key-value of the map, it will change key-value to value-key. + m.Flip() + fmt.Println(m.Map()) + + // Clear deletes all data of the map. + m.Clear() + + fmt.Println(m.Size()) + + // May Output: + // 1 + // [val1] + // val3 + // val4 + // false + // [key4 key1 key3 1] + // [key4 key3] + // true + // map[val3:key3 val4:key4] + // 0 +} + +func ExampleNewFrom() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) + + n := gmap.NewFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} + +func ExampleNewHashMap() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) + + // Output: + // {"key1":"val1"} +} + +func ExampleNewHashMapFrom() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) + + n := gmap.NewFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} From 416a5173ae62f50f66a7aeb81df0b47b777b3f86 Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 16 Nov 2021 07:30:14 +0800 Subject: [PATCH 03/22] 1# Complete the AnyAnyMap Example Function (Not Implemented) 2# Implemented AnyAnyMap Example Function: 1.NewAnyAnyMap 2.NewAnyAnyMapFrom --- container/gmap/gmap_z_example_any_any_test.go | 340 ++++++++++-------- container/gmap/gmap_z_example_test.go | 24 ++ 2 files changed, 222 insertions(+), 142 deletions(-) diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index 3a0780e18..474cb0b74 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -13,148 +13,77 @@ import ( "github.com/gogf/gf/v2/frame/g" ) -func ExampleNew() { - m := gmap.New() - - // Add data. - m.Set("key1", "val1") - - // Print size. - fmt.Println(m.Size()) - - addMap := make(map[interface{}]interface{}) - addMap["key2"] = "val2" - addMap["key3"] = "val3" - addMap[1] = 1 - - fmt.Println(m.Values()) - - // Batch add data. - m.Sets(addMap) - - // Gets the value of the corresponding key. - fmt.Println(m.Get("key3")) - - // Get the value by key, or set it with given key-value if not exist. - fmt.Println(m.GetOrSet("key4", "val4")) - - // Set key-value if the key does not exist, then return true; or else return false. - fmt.Println(m.SetIfNotExist("key3", "val3")) - - // Remove key - m.Remove("key2") - fmt.Println(m.Keys()) - - // Batch remove keys. - m.Removes([]interface{}{"key1", 1}) - fmt.Println(m.Keys()) - - // Contains checks whether a key exists. - fmt.Println(m.Contains("key3")) - - // Flip exchanges key-value of the map, it will change key-value to value-key. - m.Flip() - fmt.Println(m.Map()) - - // Clear deletes all data of the map. - m.Clear() - - fmt.Println(m.Size()) - - // May Output: - // 1 - // [val1] - // val3 - // val4 - // false - // [key4 key1 key3 1] - // [key4 key3] - // true - // map[val3:key3 val4:key4] - // 0 -} - -func ExampleNewFrom() { - m := gmap.New() - - m.Set("key1", "val1") - fmt.Println(m) - - n := gmap.NewFrom(m.MapCopy(), true) - fmt.Println(n) +func ExampleAnyAnyMap_Iterator() { // Output: - // {"key1":"val1"} - // {"key1":"val1"} } -func ExampleNewHashMap() { - m := gmap.New() - - m.Set("key1", "val1") - fmt.Println(m) +func ExampleAnyAnyMap_Clone() { // Output: - // {"key1":"val1"} } -func ExampleNewHashMapFrom() { - m := gmap.New() - - m.Set("key1", "val1") - fmt.Println(m) - - n := gmap.NewFrom(m.MapCopy(), true) - fmt.Println(n) +func ExampleAnyAnyMap_Map() { // Output: - // {"key1":"val1"} - // {"key1":"val1"} } -func ExampleAnyAnyMap_Keys() { - var m gmap.Map - m.Sets(g.MapAnyAny{ - "k1": "v1", - "k2": "v2", - "k3": "v3", - "k4": "v4", - }) - fmt.Println(m.Keys()) - fmt.Println(m.Values()) +func ExampleAnyAnyMap_MapCopy() { - // May Output: - // [k1 k2 k3 k4] - // [v2 v3 v4 v1] + // Output: } -func ExampleAnyAnyMap_Values() { - var m gmap.Map - m.Sets(g.MapAnyAny{ - "k1": "v1", - "k2": "v2", - "k3": "v3", - "k4": "v4", - }) - fmt.Println(m.Keys()) - fmt.Println(m.Values()) +func ExampleAnyAnyMap_MapStrAny() { - // May Output: - // [k1 k2 k3 k4] - // [v2 v3 v4 v1] + // Output: } -func ExampleAnyAnyMap_Flip() { - var m gmap.Map - m.Sets(g.MapAnyAny{ - "k1": "v1", - "k2": "v2", +func ExampleAnyAnyMap_FilterEmpty() { + m := gmap.NewFrom(g.MapAnyAny{ + "k1": "", + "k2": nil, + "k3": 0, + "k4": 1, }) - m.Flip() + m.FilterEmpty() fmt.Println(m.Map()) // May Output: - // map[v1:k1 v2:k2] + // map[k4:1] +} + +func ExampleAnyAnyMap_FilterNil() { + m := gmap.NewFrom(g.MapAnyAny{ + "k1": "", + "k2": nil, + "k3": 0, + "k4": 1, + }) + m.FilterNil() + fmt.Println(m.Map()) + + // May Output: + // map[k1: k3:0 k4:1] +} + +func ExampleAnyAnyMap_Set() { + + // Output: +} + +func ExampleAnyAnyMap_Sets() { + + // Output: +} + +func ExampleAnyAnyMap_Search() { + + // Output: +} + +func ExampleAnyAnyMap_Get() { + + // Output: } func ExampleAnyAnyMap_Pop() { @@ -193,32 +122,39 @@ func ExampleAnyAnyMap_Pops() { // 1 } -func ExampleAnyAnyMap_FilterEmpty() { - m := gmap.NewFrom(g.MapAnyAny{ - "k1": "", - "k2": nil, - "k3": 0, - "k4": 1, - }) - m.FilterEmpty() - fmt.Println(m.Map()) +func ExampleAnyAnyMap_GetOrSet() { - // May Output: - // map[k4:1] + // Output: } -func ExampleAnyAnyMap_FilterNil() { - m := gmap.NewFrom(g.MapAnyAny{ - "k1": "", - "k2": nil, - "k3": 0, - "k4": 1, - }) - m.FilterNil() - fmt.Println(m.Map()) +func ExampleAnyAnyMap_GetOrSetFunc() { - // May Output: - // map[k1: k3:0 k4:1] + // Output: +} + +func ExampleAnyAnyMap_GetOrSetFuncLock() { + + // Output: +} + +func ExampleAnyAnyMap_GetVar() { + + // Output: +} + +func ExampleAnyAnyMap_GetVarOrSet() { + + // Output: +} + +func ExampleAnyAnyMap_GetVarOrSetFunc() { + + // Output: +} + +func ExampleAnyAnyMap_GetVarOrSetFuncLock() { + + // Output: } func ExampleAnyAnyMap_SetIfNotExist() { @@ -233,6 +169,106 @@ func ExampleAnyAnyMap_SetIfNotExist() { // map[k1:v1] } +func ExampleAnyAnyMap_SetIfNotExistFunc() { + + // Output: +} + +func ExampleAnyAnyMap_SetIfNotExistFuncLock() { + + // Output: +} + +func ExampleAnyAnyMap_Remove() { + + // Output: +} + +func ExampleAnyAnyMap_Removes() { + + // Output: +} + +func ExampleAnyAnyMap_Keys() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Keys()) + fmt.Println(m.Values()) + + // May Output: + // [k1 k2 k3 k4] + // [v2 v3 v4 v1] +} + +func ExampleAnyAnyMap_Values() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Keys()) + fmt.Println(m.Values()) + + // May Output: + // [k1 k2 k3 k4] + // [v2 v3 v4 v1] +} + +func ExampleAnyAnyMap_Contains() { + + // Output: +} + +func ExampleAnyAnyMap_Size() { + + // Output: +} + +func ExampleAnyAnyMap_IsEmpty() { + + // Output: +} + +func ExampleAnyAnyMap_Clear() { + + // Output: +} + +func ExampleAnyAnyMap_Replace() { + + // Output: +} + +func ExampleAnyAnyMap_LockFunc() { + + // Output: +} + +func ExampleAnyAnyMap_RLockFunc() { + + // Output: +} + +func ExampleAnyAnyMap_Flip() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + }) + m.Flip() + fmt.Println(m.Map()) + + // May Output: + // map[v1:k1 v2:k2] +} + func ExampleAnyAnyMap_Merge() { var m1, m2 gmap.Map m1.Set("key1", "val1") @@ -243,3 +279,23 @@ func ExampleAnyAnyMap_Merge() { // May Output: // map[key1:val1 key2:val2] } + +func ExampleAnyAnyMap_String() { + + // Output: +} + +func ExampleAnyAnyMap_MarshalJSON() { + + // Output: +} + +func ExampleAnyAnyMap_UnmarshalJSON() { + + // Output: +} + +func ExampleAnyAnyMap_UnmarshalValue() { + + // Output: +} diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_test.go index 1f1148300..424e69939 100644 --- a/container/gmap/gmap_z_example_test.go +++ b/container/gmap/gmap_z_example_test.go @@ -110,3 +110,27 @@ func ExampleNewHashMapFrom() { // {"key1":"val1"} // {"key1":"val1"} } + +func ExampleNewAnyAnyMap() { + m := gmap.NewAnyAnyMap() + + m.Set("key1", "val1") + fmt.Println(m) + + // Output: + // {"key1":"val1"} +} + +func ExampleNewAnyAnyMapFrom() { + m := gmap.NewAnyAnyMap() + + m.Set("key1", "val1") + fmt.Println(m) + + n := gmap.NewAnyAnyMapFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} From ef29ee4804c41dd40be9eda9a9fbfc82da01cfbf Mon Sep 17 00:00:00 2001 From: huangqian Date: Wed, 17 Nov 2021 23:18:35 +0800 Subject: [PATCH 04/22] Implemented AnyAnyMap Example Function: 1.Iterator 2.Clone 3.Map 4.MapCopy 5.MapStrAny 6.FilterEmpty 7.FilterNil 8.Set 9.Sets 10.Search 11.Get 12.Pop 13.Pops 14.GetOrSet 15.GetOrSetFunc 16.GetOrSetFuncLock 17.GetVar 18.GetVarOrSet 19.GetVarOrSetFunc 20.GetVarOrSetFuncLock --- container/gmap/gmap_hash_any_any_map.go | 2 +- container/gmap/gmap_z_example_any_any_test.go | 197 +++++++++++++++++- 2 files changed, 188 insertions(+), 11 deletions(-) diff --git a/container/gmap/gmap_hash_any_any_map.go b/container/gmap/gmap_hash_any_any_map.go index a1f4b3230..9b268ae0d 100644 --- a/container/gmap/gmap_hash_any_any_map.go +++ b/container/gmap/gmap_hash_any_any_map.go @@ -266,7 +266,7 @@ func (m *AnyAnyMap) GetVar(key interface{}) *gvar.Var { return gvar.New(m.Get(key)) } -// GetVarOrSet returns a Var with result from GetVarOrSet. +// GetVarOrSet returns a Var with result from GetOrSet. // The returned Var is un-concurrent safe. func (m *AnyAnyMap) GetVarOrSet(key interface{}, value interface{}) *gvar.Var { return gvar.New(m.GetOrSet(key, value)) diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index 474cb0b74..01af29acd 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -14,28 +14,96 @@ import ( ) func ExampleAnyAnyMap_Iterator() { + m := gmap.New() + for i := 0; i < 10; i++ { + m.Set(i, i*2) + } - // Output: + var totalKey, totalValue int + m.Iterator(func(k interface{}, v interface{}) bool { + totalKey += k.(int) + totalValue += v.(int) + + return totalKey < 10 + }) + + fmt.Println("totalKey:", totalKey) + fmt.Println("totalValue:", totalValue) + + // May Output: + // totalKey: 11 + // totalValue: 22 } func ExampleAnyAnyMap_Clone() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) + + n := m.Clone() + fmt.Println(n) // Output: + // {"key1":"val1"} + // {"key1":"val1"} } func ExampleAnyAnyMap_Map() { + // non concurrent-safety, a pointer to the underlying data + m1 := gmap.New() + m1.Set("key1", "val1") + fmt.Println("m1:", m1) + + n1 := m1.Map() + fmt.Println("before n1:", n1) + m1.Set("key1", "val2") + fmt.Println("after n1:", n1) + + // concurrent-safety, copy of underlying data + m2 := gmap.New(true) + m2.Set("key1", "val1") + fmt.Println("m1:", m2) + + n2 := m2.Map() + fmt.Println("before n2:", n2) + m2.Set("key1", "val2") + fmt.Println("after n2:", n2) // Output: + // m1: {"key1":"val1"} + // before n1: map[key1:val1] + // after n1: map[key1:val2] + // m1: {"key1":"val1"} + // before n2: map[key1:val1] + // after n2: map[key1:val1] } func ExampleAnyAnyMap_MapCopy() { + m := gmap.New() + + m.Set("key1", "val1") + m.Set("key2", "val2") + fmt.Println(m) + + n := m.MapCopy() + fmt.Println(n) // Output: + // {"key1":"val1","key2":"val2"} + // map[key1:val1 key2:val2] } func ExampleAnyAnyMap_MapStrAny() { + m := gmap.New() + m.Set(1001, "val1") + m.Set(1002, "val2") + + n := m.MapStrAny() + fmt.Println(n) // Output: + // map[1001:val1 1002:val2] } func ExampleAnyAnyMap_FilterEmpty() { @@ -67,23 +135,61 @@ func ExampleAnyAnyMap_FilterNil() { } func ExampleAnyAnyMap_Set() { + m := gmap.New() + + m.Set("key1", "val1") + fmt.Println(m) // Output: + // {"key1":"val1"} } func ExampleAnyAnyMap_Sets() { + m := gmap.New() + + addMap := make(map[interface{}]interface{}) + addMap["key1"] = "val1" + addMap["key2"] = "val2" + addMap["key3"] = "val3" + + m.Sets(addMap) + fmt.Println(m) // Output: + // {"key1":"val1","key2":"val2","key3":"val3"} } func ExampleAnyAnyMap_Search() { + m := gmap.New() + + m.Set("key1", "val1") + + value, found := m.Search("key1") + if found { + fmt.Println("find key1 value:", value) + } + + value, found = m.Search("key2") + if !found { + fmt.Println("key2 not find") + } // Output: + // find key1 value: val1 + // key2 not find } func ExampleAnyAnyMap_Get() { + m := gmap.New() + + m.Set("key1", "val1") + + fmt.Println("key1 value:", m.Get("key1")) + fmt.Println("key2 value:", m.Get("key2")) // Output: + // key1 value: val1 + // key2 value: } func ExampleAnyAnyMap_Pop() { @@ -94,14 +200,11 @@ func ExampleAnyAnyMap_Pop() { "k3": "v3", "k4": "v4", }) + fmt.Println(m.Pop()) - fmt.Println(m.Pops(2)) - fmt.Println(m.Size()) // May Output: // k1 v1 - // map[k2:v2 k4:v4] - // 1 } func ExampleAnyAnyMap_Pops() { @@ -112,49 +215,123 @@ func ExampleAnyAnyMap_Pops() { "k3": "v3", "k4": "v4", }) - fmt.Println(m.Pop()) + fmt.Println(m.Pops(-1)) + fmt.Println("size:", m.Size()) + + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) fmt.Println(m.Pops(2)) - fmt.Println(m.Size()) + fmt.Println("size:", m.Size()) // May Output: - // k1 v1 - // map[k2:v2 k4:v4] - // 1 + // map[k1:v1 k2:v2 k3:v3 k4:v4] + // size: 0 + // map[k1:v1 k2:v2] + // size: 2 } func ExampleAnyAnyMap_GetOrSet() { + m := gmap.New() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSet("key1", "NotExistValue")) + fmt.Println(m.GetOrSet("key2", "val2")) // Output: + // val1 + // val2 } func ExampleAnyAnyMap_GetOrSetFunc() { + m := gmap.New() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFunc("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFunc("key2", func() interface{} { + return "NotExistValue" + })) // Output: + // val1 + // NotExistValue } func ExampleAnyAnyMap_GetOrSetFuncLock() { + m := gmap.New() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} { + return "NotExistValue" + })) // Output: + // val1 + // NotExistValue } func ExampleAnyAnyMap_GetVar() { + m := gmap.New() + m.Set("key1", "val1") + + fmt.Println(m.GetVar("key1")) + fmt.Println(m.GetVar("key2").IsNil()) // Output: + // val1 + // true } func ExampleAnyAnyMap_GetVarOrSet() { + m := gmap.New() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSet("key1", "NotExistValue")) + fmt.Println(m.GetVarOrSet("key2", "val2")) // Output: + // val1 + // val2 } func ExampleAnyAnyMap_GetVarOrSetFunc() { + m := gmap.New() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} { + return "NotExistValue" + })) // Output: + // val1 + // NotExistValue } func ExampleAnyAnyMap_GetVarOrSetFuncLock() { + m := gmap.New() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} { + return "NotExistValue" + })) // Output: + // val1 + // NotExistValue } func ExampleAnyAnyMap_SetIfNotExist() { From 36927b4371ae7ed83d65f26feb6be907a398075e Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 18 Nov 2021 07:32:29 +0800 Subject: [PATCH 05/22] Implemented AnyAnyMap Example Function: 1.Remove 2.Removes 3.Keys 4.Values 5.Contains 6.Size 7.IsEmpty 8.Clear --- container/gmap/gmap_z_example_any_any_test.go | 92 ++++++++++++++++++- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index 01af29acd..c81ec27f0 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -347,23 +347,68 @@ func ExampleAnyAnyMap_SetIfNotExist() { } func ExampleAnyAnyMap_SetIfNotExistFunc() { + var m gmap.Map + fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) // Output: + // true + // false + // map[k1:v1] } func ExampleAnyAnyMap_SetIfNotExistFuncLock() { + var m gmap.Map + fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) // Output: + // true + // false + // map[k1:v1] } func ExampleAnyAnyMap_Remove() { + var m gmap.Map + m.Set("k1", "v1") + + fmt.Println(m.Remove("k1")) + fmt.Println(m.Remove("k2")) // Output: + // v1 + // } func ExampleAnyAnyMap_Removes() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + removeList := make([]interface{}, 2) + removeList = append(removeList, "k1") + removeList = append(removeList, "k2") + + m.Removes(removeList) + + fmt.Println(m.Map()) // Output: + // map[k3:v3 k4:v4] } func ExampleAnyAnyMap_Keys() { @@ -375,11 +420,9 @@ func ExampleAnyAnyMap_Keys() { "k4": "v4", }) fmt.Println(m.Keys()) - fmt.Println(m.Values()) // May Output: // [k1 k2 k3 k4] - // [v2 v3 v4 v1] } func ExampleAnyAnyMap_Values() { @@ -390,32 +433,71 @@ func ExampleAnyAnyMap_Values() { "k3": "v3", "k4": "v4", }) - fmt.Println(m.Keys()) fmt.Println(m.Values()) // May Output: - // [k1 k2 k3 k4] - // [v2 v3 v4 v1] + // [v1 v2 v3 v4] } func ExampleAnyAnyMap_Contains() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Contains("k1")) + fmt.Println(m.Contains("k5")) // Output: + // true + // false } func ExampleAnyAnyMap_Size() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Size()) // Output: + // 4 } func ExampleAnyAnyMap_IsEmpty() { + var m gmap.Map + fmt.Println(m.IsEmpty()) + + m.Set("k1", "v1") + fmt.Println(m.IsEmpty()) // Output: + // true + // false } func ExampleAnyAnyMap_Clear() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + m.Clear() + + fmt.Println(m.Map()) // Output: + // map[] } func ExampleAnyAnyMap_Replace() { From 7d6c9244cf6eb85aac4b41e27db8f1f668282959 Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 18 Nov 2021 21:37:27 +0800 Subject: [PATCH 06/22] Implemented AnyAnyMap Example Function: 1.Replace 2.LockFunc 3.RLockFunc 4.Flip 5.Merge 6.String 7.MarshalJSON 8.UnmarshalJSON 9.UnmarshalValue --- container/gmap/gmap_z_example_any_any_test.go | 106 +++++++++++++++++- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index c81ec27f0..a97129d26 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -8,6 +8,7 @@ package gmap_test import ( "fmt" + "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/container/gmap" "github.com/gogf/gf/v2/frame/g" @@ -501,31 +502,82 @@ func ExampleAnyAnyMap_Clear() { } func ExampleAnyAnyMap_Replace() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + }) + + var n gmap.Map + n.Sets(g.MapAnyAny{ + "k2": "v2", + }) + + fmt.Println(m.Map()) + + m.Replace(n.Map()) + fmt.Println(m.Map()) + + n.Set("k2", "v1") + fmt.Println(m.Map()) // Output: + // map[k1:v1] + // map[k2:v2] + // map[k2:v1] } func ExampleAnyAnyMap_LockFunc() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.LockFunc(func(m map[interface{}]interface{}) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) // Output: + // totalValue: 10 } func ExampleAnyAnyMap_RLockFunc() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.RLockFunc(func(m map[interface{}]interface{}) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) // Output: + // totalValue: 10 } func ExampleAnyAnyMap_Flip() { var m gmap.Map m.Sets(g.MapAnyAny{ "k1": "v1", - "k2": "v2", }) m.Flip() fmt.Println(m.Map()) - // May Output: - // map[v1:k1 v2:k2] + // Output: + // map[v1:k1] } func ExampleAnyAnyMap_Merge() { @@ -540,21 +592,69 @@ func ExampleAnyAnyMap_Merge() { } func ExampleAnyAnyMap_String() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + }) + + fmt.Println(m.String()) // Output: + // {"k1":"v1"} } func ExampleAnyAnyMap_MarshalJSON() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + bytes, err := m.MarshalJSON() + if err == nil { + fmt.Println(gconv.String(bytes)) + } // Output: + // {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} } func ExampleAnyAnyMap_UnmarshalJSON() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + var n gmap.Map + + err := n.UnmarshalJSON(gconv.Bytes(m.String())) + if err == nil { + fmt.Println(n.Map()) + } // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] } func ExampleAnyAnyMap_UnmarshalValue() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + var n gmap.Map + err := n.UnmarshalValue(m.String()) + if err == nil { + fmt.Println(n.Map()) + } // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] } From 076b15d71ff4323d90b750646cb2c1d9994f6cdb Mon Sep 17 00:00:00 2001 From: huangqian Date: Fri, 19 Nov 2021 07:27:19 +0800 Subject: [PATCH 07/22] Implemented IntAnyMap Example --- container/gmap/gmap_z_example_any_any_test.go | 2 +- container/gmap/gmap_z_example_int_any_test.go | 659 ++++++++++++++++++ container/gmap/gmap_z_example_test.go | 24 + 3 files changed, 684 insertions(+), 1 deletion(-) create mode 100644 container/gmap/gmap_z_example_int_any_test.go diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index a97129d26..b6f9b4930 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -117,7 +117,7 @@ func ExampleAnyAnyMap_FilterEmpty() { m.FilterEmpty() fmt.Println(m.Map()) - // May Output: + // Output: // map[k4:1] } diff --git a/container/gmap/gmap_z_example_int_any_test.go b/container/gmap/gmap_z_example_int_any_test.go new file mode 100644 index 000000000..fb4db9984 --- /dev/null +++ b/container/gmap/gmap_z_example_int_any_test.go @@ -0,0 +1,659 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with gm file, +// You can obtain one at https://github.com/gogf/gf. + +package gmap_test + +import ( + "fmt" + "github.com/gogf/gf/v2/container/gmap" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/util/gconv" +) + +func ExampleIntAnyMap_Iterator() { + m := gmap.NewIntAnyMap() + for i := 0; i < 10; i++ { + m.Set(i, i*2) + } + + var totalKey, totalValue int + m.Iterator(func(k int, v interface{}) bool { + totalKey += k + totalValue += v.(int) + + return totalKey < 10 + }) + + fmt.Println("totalKey:", totalKey) + fmt.Println("totalValue:", totalValue) + + // May Output: + // totalKey: 11 + // totalValue: 22 +} + +func ExampleIntAnyMap_Clone() { + m := gmap.NewIntAnyMap() + + m.Set(1, "val1") + fmt.Println(m) + + n := m.Clone() + fmt.Println(n) + + // Output: + // {"1":"val1"} + // {"1":"val1"} +} + +func ExampleIntAnyMap_Map() { + // non concurrent-safety, a pointer to the underlying data + m1 := gmap.NewIntAnyMap() + m1.Set(1, "val1") + fmt.Println("m1:", m1) + + n1 := m1.Map() + fmt.Println("before n1:", n1) + m1.Set(1, "val2") + fmt.Println("after n1:", n1) + + // concurrent-safety, copy of underlying data + m2 := gmap.New(true) + m2.Set(1, "val1") + fmt.Println("m1:", m2) + + n2 := m2.Map() + fmt.Println("before n2:", n2) + m2.Set(1, "val2") + fmt.Println("after n2:", n2) + + // Output: + // m1: {"1":"val1"} + // before n1: map[1:val1] + // after n1: map[1:val2] + // m1: {"1":"val1"} + // before n2: map[1:val1] + // after n2: map[1:val1] +} + +func ExampleIntAnyMap_MapCopy() { + m := gmap.NewIntAnyMap() + + m.Set(1, "val1") + m.Set(2, "val2") + fmt.Println(m) + + n := m.MapCopy() + fmt.Println(n) + + // Output: + // {"1":"val1","2":"val2"} + // map[1:val1 2:val2] +} + +func ExampleIntAnyMap_MapStrAny() { + m := gmap.NewIntAnyMap() + m.Set(1001, "val1") + m.Set(1002, "val2") + + n := m.MapStrAny() + fmt.Println(n) + + // Output: + // map[1001:val1 1002:val2] +} + +func ExampleIntAnyMap_FilterEmpty() { + m := gmap.NewIntAnyMapFrom(g.MapIntAny{ + 1: "", + 2: nil, + 3: 0, + 4: 1, + }) + m.FilterEmpty() + fmt.Println(m.Map()) + + // Output: + // map[4:1] +} + +func ExampleIntAnyMap_FilterNil() { + m := gmap.NewIntAnyMapFrom(g.MapIntAny{ + 1: "", + 2: nil, + 3: 0, + 4: 1, + }) + m.FilterNil() + fmt.Println(m.Map()) + + // Output: + // map[1: 3:0 4:1] +} + +func ExampleIntAnyMap_Set() { + m := gmap.NewIntAnyMap() + + m.Set(1, "val1") + fmt.Println(m) + + // Output: + // {"1":"val1"} +} + +func ExampleIntAnyMap_Sets() { + m := gmap.NewIntAnyMap() + + addMap := make(map[int]interface{}) + addMap[1] = "val1" + addMap[2] = "val2" + addMap[3] = "val3" + + m.Sets(addMap) + fmt.Println(m) + + // Output: + // {"1":"val1","2":"val2","3":"val3"} +} + +func ExampleIntAnyMap_Search() { + m := gmap.NewIntAnyMap() + + m.Set(1, "val1") + + value, found := m.Search(1) + if found { + fmt.Println("find key1 value:", value) + } + + value, found = m.Search(2) + if !found { + fmt.Println("key2 not find") + } + + // Output: + // find key1 value: val1 + // key2 not find +} + +func ExampleIntAnyMap_Get() { + m := gmap.NewIntAnyMap() + + m.Set(1, "val1") + + fmt.Println("key1 value:", m.Get(1)) + fmt.Println("key2 value:", m.Get(2)) + + // Output: + // key1 value: val1 + // key2 value: +} + +func ExampleIntAnyMap_Pop() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + fmt.Println(m.Pop()) + + // May Output: + // 1 v1 +} + +func ExampleIntAnyMap_Pops() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + fmt.Println(m.Pops(-1)) + fmt.Println("size:", m.Size()) + + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + fmt.Println(m.Pops(2)) + fmt.Println("size:", m.Size()) + + // May Output: + // map[1:v1 2:v2 3:v3 4:v4] + // size: 0 + // map[1:v1 2:v2] + // size: 2 +} + +func ExampleIntAnyMap_GetOrSet() { + m := gmap.NewIntAnyMap() + m.Set(1, "val1") + + fmt.Println(m.GetOrSet(1, "NotExistValue")) + fmt.Println(m.GetOrSet(2, "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleIntAnyMap_GetOrSetFunc() { + m := gmap.NewIntAnyMap() + m.Set(1, "val1") + + fmt.Println(m.GetOrSetFunc(1, func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFunc(2, func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleIntAnyMap_GetOrSetFuncLock() { + m := gmap.NewIntAnyMap() + m.Set(1, "val1") + + fmt.Println(m.GetOrSetFuncLock(1, func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFuncLock(2, func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleIntAnyMap_GetVar() { + m := gmap.NewIntAnyMap() + m.Set(1, "val1") + + fmt.Println(m.GetVar(1)) + fmt.Println(m.GetVar(2).IsNil()) + + // Output: + // val1 + // true +} + +func ExampleIntAnyMap_GetVarOrSet() { + m := gmap.NewIntAnyMap() + m.Set(1, "val1") + + fmt.Println(m.GetVarOrSet(1, "NotExistValue")) + fmt.Println(m.GetVarOrSet(2, "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleIntAnyMap_GetVarOrSetFunc() { + m := gmap.NewIntAnyMap() + m.Set(1, "val1") + + fmt.Println(m.GetVarOrSetFunc(1, func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFunc(2, func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleIntAnyMap_GetVarOrSetFuncLock() { + m := gmap.NewIntAnyMap() + m.Set(1, "val1") + + fmt.Println(m.GetVarOrSetFuncLock(1, func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFuncLock(2, func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleIntAnyMap_SetIfNotExist() { + var m gmap.IntAnyMap + fmt.Println(m.SetIfNotExist(1, "v1")) + fmt.Println(m.SetIfNotExist(1, "v1")) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[1:v1] +} + +func ExampleIntAnyMap_SetIfNotExistFunc() { + var m gmap.IntAnyMap + fmt.Println(m.SetIfNotExistFunc(1, func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFunc(1, func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[1:v1] +} + +func ExampleIntAnyMap_SetIfNotExistFuncLock() { + var m gmap.IntAnyMap + fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[1:v1] +} + +func ExampleIntAnyMap_Remove() { + var m gmap.IntAnyMap + m.Set(1, "v1") + + fmt.Println(m.Remove(1)) + fmt.Println(m.Remove(2)) + + // Output: + // v1 + // +} + +func ExampleIntAnyMap_Removes() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + removeList := make([]int, 2) + removeList = append(removeList, 1) + removeList = append(removeList, 2) + + m.Removes(removeList) + + fmt.Println(m.Map()) + + // Output: + // map[3:v3 4:v4] +} + +func ExampleIntAnyMap_Keys() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + fmt.Println(m.Keys()) + + // May Output: + // [1 2 3 4] +} + +func ExampleIntAnyMap_Values() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + fmt.Println(m.Values()) + + // May Output: + // [v1 v2 v3 v4] +} + +func ExampleIntAnyMap_Contains() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + fmt.Println(m.Contains(1)) + fmt.Println(m.Contains(5)) + + // Output: + // true + // false +} + +func ExampleIntAnyMap_Size() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + fmt.Println(m.Size()) + + // Output: + // 4 +} + +func ExampleIntAnyMap_IsEmpty() { + var m gmap.IntAnyMap + fmt.Println(m.IsEmpty()) + + m.Set(1, "v1") + fmt.Println(m.IsEmpty()) + + // Output: + // true + // false +} + +func ExampleIntAnyMap_Clear() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + m.Clear() + + fmt.Println(m.Map()) + + // Output: + // map[] +} + +func ExampleIntAnyMap_Replace() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + }) + + var n gmap.IntAnyMap + n.Sets(g.MapIntAny{ + 2: "v2", + }) + + fmt.Println(m.Map()) + + m.Replace(n.Map()) + fmt.Println(m.Map()) + + n.Set(2, "v1") + fmt.Println(m.Map()) + + // Output: + // map[1:v1] + // map[2:v2] + // map[2:v1] +} + +func ExampleIntAnyMap_LockFunc() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + m.LockFunc(func(m map[int]interface{}) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleIntAnyMap_RLockFunc() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + m.RLockFunc(func(m map[int]interface{}) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleIntAnyMap_Flip() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: 10, + }) + m.Flip() + fmt.Println(m.Map()) + + // Output: + // map[10:1] +} + +func ExampleIntAnyMap_Merge() { + var m1, m2 gmap.Map + m1.Set(1, "val1") + m2.Set(2, "val2") + m1.Merge(&m2) + fmt.Println(m1.Map()) + + // May Output: + // map[key1:val1 key2:val2] +} + +func ExampleIntAnyMap_String() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + }) + + fmt.Println(m.String()) + + // Output: + // {"1":"v1"} +} + +func ExampleIntAnyMap_MarshalJSON() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + bytes, err := m.MarshalJSON() + if err == nil { + fmt.Println(gconv.String(bytes)) + } + + // Output: + // {"1":"v1","2":"v2","3":"v3","4":"v4"} +} + +func ExampleIntAnyMap_UnmarshalJSON() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + var n gmap.Map + + err := n.UnmarshalJSON(gconv.Bytes(m.String())) + if err == nil { + fmt.Println(n.Map()) + } + + // Output: + // map[1:v1 2:v2 3:v3 4:v4] +} + +func ExampleIntAnyMap_UnmarshalValue() { + var m gmap.IntAnyMap + m.Sets(g.MapIntAny{ + 1: "v1", + 2: "v2", + 3: "v3", + 4: "v4", + }) + + var n gmap.Map + err := n.UnmarshalValue(m.String()) + if err == nil { + fmt.Println(n.Map()) + } + // Output: + // map[1:v1 2:v2 3:v3 4:v4] +} diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_test.go index 424e69939..7b00d8d8b 100644 --- a/container/gmap/gmap_z_example_test.go +++ b/container/gmap/gmap_z_example_test.go @@ -134,3 +134,27 @@ func ExampleNewAnyAnyMapFrom() { // {"key1":"val1"} // {"key1":"val1"} } + +func ExampleNewIntAnyMap() { + m := gmap.NewIntAnyMap() + + m.Set(1, "val1") + fmt.Println(m) + + // Output: + // {"1":"val1"} +} + +func ExampleNewIntAnyMapFrom() { + m := gmap.NewIntAnyMap() + + m.Set(1, "val1") + fmt.Println(m) + + n := gmap.NewIntAnyMapFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"1":"val1"} + // {"1":"val1"} +} From 9aeac45198c2e8e791629ea58d66e89bc3b31e0a Mon Sep 17 00:00:00 2001 From: huangqian Date: Fri, 19 Nov 2021 21:46:11 +0800 Subject: [PATCH 08/22] Implemented IntIntMap Example --- container/gmap/gmap_z_example_any_any_test.go | 4 +- container/gmap/gmap_z_example_int_any_test.go | 4 +- container/gmap/gmap_z_example_int_int_test.go | 587 ++++++++++++++++++ container/gmap/gmap_z_example_test.go | 24 + 4 files changed, 615 insertions(+), 4 deletions(-) create mode 100644 container/gmap/gmap_z_example_int_int_test.go diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index b6f9b4930..88c0e4e6e 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -64,7 +64,7 @@ func ExampleAnyAnyMap_Map() { // concurrent-safety, copy of underlying data m2 := gmap.New(true) m2.Set("key1", "val1") - fmt.Println("m1:", m2) + fmt.Println("m2:", m2) n2 := m2.Map() fmt.Println("before n2:", n2) @@ -75,7 +75,7 @@ func ExampleAnyAnyMap_Map() { // m1: {"key1":"val1"} // before n1: map[key1:val1] // after n1: map[key1:val2] - // m1: {"key1":"val1"} + // m2: {"key1":"val1"} // before n2: map[key1:val1] // after n2: map[key1:val1] } diff --git a/container/gmap/gmap_z_example_int_any_test.go b/container/gmap/gmap_z_example_int_any_test.go index fb4db9984..22cd4556a 100644 --- a/container/gmap/gmap_z_example_int_any_test.go +++ b/container/gmap/gmap_z_example_int_any_test.go @@ -63,7 +63,7 @@ func ExampleIntAnyMap_Map() { // concurrent-safety, copy of underlying data m2 := gmap.New(true) m2.Set(1, "val1") - fmt.Println("m1:", m2) + fmt.Println("m2:", m2) n2 := m2.Map() fmt.Println("before n2:", n2) @@ -74,7 +74,7 @@ func ExampleIntAnyMap_Map() { // m1: {"1":"val1"} // before n1: map[1:val1] // after n1: map[1:val2] - // m1: {"1":"val1"} + // m2: {"1":"val1"} // before n2: map[1:val1] // after n2: map[1:val1] } diff --git a/container/gmap/gmap_z_example_int_int_test.go b/container/gmap/gmap_z_example_int_int_test.go new file mode 100644 index 000000000..12347b92c --- /dev/null +++ b/container/gmap/gmap_z_example_int_int_test.go @@ -0,0 +1,587 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with gm file, +// You can obtain one at https://github.com/gogf/gf. + +package gmap_test + +import ( + "fmt" + "github.com/gogf/gf/v2/container/gmap" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/util/gconv" +) + +func ExampleIntIntMap_Iterator() { + m := gmap.NewIntIntMap() + for i := 0; i < 10; i++ { + m.Set(i, i*2) + } + + var totalKey, totalValue int + m.Iterator(func(k int, v int) bool { + totalKey += k + totalValue += v + + return totalKey < 10 + }) + + fmt.Println("totalKey:", totalKey) + fmt.Println("totalValue:", totalValue) + + // May Output: + // totalKey: 11 + // totalValue: 22 +} + +func ExampleIntIntMap_Clone() { + m := gmap.NewIntIntMap() + + m.Set(1, 1) + fmt.Println(m) + + n := m.Clone() + fmt.Println(n) + + // Output: + // {"1":1} + // {"1":1} +} + +func ExampleIntIntMap_Map() { + // non concurrent-safety, a pointer to the underlying data + m1 := gmap.NewIntIntMap() + m1.Set(1, 1) + fmt.Println("m1:", m1) + + n1 := m1.Map() + fmt.Println("before n1:", n1) + m1.Set(1, 2) + fmt.Println("after n1:", n1) + + // concurrent-safety, copy of underlying data + m2 := gmap.New(true) + m2.Set(1, "1") + fmt.Println("m2:", m2) + + n2 := m2.Map() + fmt.Println("before n2:", n2) + m2.Set(1, "2") + fmt.Println("after n2:", n2) + + // Output: + // m1: {"1":1} + // before n1: map[1:1] + // after n1: map[1:2] + // m2: {"1":"1"} + // before n2: map[1:1] + // after n2: map[1:1] +} + +func ExampleIntIntMap_MapCopy() { + m := gmap.NewIntIntMap() + + m.Set(1, 1) + m.Set(2, 2) + fmt.Println(m) + + n := m.MapCopy() + fmt.Println(n) + + // Output: + // {"1":1,"2":2} + // map[1:1 2:2] +} + +func ExampleIntIntMap_MapStrAny() { + m := gmap.NewIntIntMap() + m.Set(1001, 1) + m.Set(1002, 2) + + n := m.MapStrAny() + fmt.Println(n) + + // Output: + // map[1001:1 1002:2] +} + +func ExampleIntIntMap_FilterEmpty() { + m := gmap.NewIntIntMapFrom(g.MapIntInt{ + 1: 0, + 2: 1, + }) + m.FilterEmpty() + fmt.Println(m.Map()) + + // Output: + // map[2:1] +} + +func ExampleIntIntMap_Set() { + m := gmap.NewIntIntMap() + + m.Set(1, 1) + fmt.Println(m) + + // Output: + // {"1":1} +} + +func ExampleIntIntMap_Sets() { + m := gmap.NewIntIntMap() + + addMap := make(map[int]int) + addMap[1] = 1 + addMap[2] = 12 + addMap[3] = 123 + + m.Sets(addMap) + fmt.Println(m) + + // Output: + // {"1":1,"2":12,"3":123} +} + +func ExampleIntIntMap_Search() { + m := gmap.NewIntIntMap() + + m.Set(1, 1) + + value, found := m.Search(1) + if found { + fmt.Println("find key1 value:", value) + } + + value, found = m.Search(2) + if !found { + fmt.Println("key2 not find") + } + + // Output: + // find key1 value: 1 + // key2 not find +} + +func ExampleIntIntMap_Get() { + m := gmap.NewIntIntMap() + + m.Set(1, 1) + + fmt.Println("key1 value:", m.Get(1)) + fmt.Println("key2 value:", m.Get(2)) + + // Output: + // key1 value: 1 + // key2 value: 0 +} + +func ExampleIntIntMap_Pop() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + fmt.Println(m.Pop()) + + // May Output: + // 1 1 +} + +func ExampleIntIntMap_Pops() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + fmt.Println(m.Pops(-1)) + fmt.Println("size:", m.Size()) + + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + fmt.Println(m.Pops(2)) + fmt.Println("size:", m.Size()) + + // May Output: + // map[1:1 2:2 3:3 4:4] + // size: 0 + // map[1:1 2:2] + // size: 2 +} + +func ExampleIntIntMap_GetOrSet() { + m := gmap.NewIntIntMap() + m.Set(1, 1) + + fmt.Println(m.GetOrSet(1, 0)) + fmt.Println(m.GetOrSet(2, 2)) + + // Output: + // 1 + // 2 +} + +func ExampleIntIntMap_GetOrSetFunc() { + m := gmap.NewIntIntMap() + m.Set(1, 1) + + fmt.Println(m.GetOrSetFunc(1, func() int { + return 0 + })) + fmt.Println(m.GetOrSetFunc(2, func() int { + return 0 + })) + + // Output: + // 1 + // 0 +} + +func ExampleIntIntMap_GetOrSetFuncLock() { + m := gmap.NewIntIntMap() + m.Set(1, 1) + + fmt.Println(m.GetOrSetFuncLock(1, func() int { + return 0 + })) + fmt.Println(m.GetOrSetFuncLock(2, func() int { + return 0 + })) + + // Output: + // 1 + // 0 +} + +func ExampleIntIntMap_SetIfNotExist() { + var m gmap.IntIntMap + fmt.Println(m.SetIfNotExist(1, 1)) + fmt.Println(m.SetIfNotExist(1, 1)) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[1:1] +} + +func ExampleIntIntMap_SetIfNotExistFunc() { + var m gmap.IntIntMap + fmt.Println(m.SetIfNotExistFunc(1, func() int { + return 1 + })) + fmt.Println(m.SetIfNotExistFunc(1, func() int { + return 1 + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[1:1] +} + +func ExampleIntIntMap_SetIfNotExistFuncLock() { + var m gmap.IntIntMap + fmt.Println(m.SetIfNotExistFuncLock(1, func() int { + return 1 + })) + fmt.Println(m.SetIfNotExistFuncLock(1, func() int { + return 1 + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[1:1] +} + +func ExampleIntIntMap_Remove() { + var m gmap.IntIntMap + m.Set(1, 1) + + fmt.Println(m.Remove(1)) + fmt.Println(m.Remove(2)) + + // Output: + // 1 + // 0 +} + +func ExampleIntIntMap_Removes() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + removeList := make([]int, 2) + removeList = append(removeList, 1) + removeList = append(removeList, 2) + + m.Removes(removeList) + + fmt.Println(m.Map()) + + // Output: + // map[3:3 4:4] +} + +func ExampleIntIntMap_Keys() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + fmt.Println(m.Keys()) + + // May Output: + // [1 2 3 4] +} + +func ExampleIntIntMap_Values() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + fmt.Println(m.Values()) + + // May Output: + // [1 v2 v3 4] +} + +func ExampleIntIntMap_Contains() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + fmt.Println(m.Contains(1)) + fmt.Println(m.Contains(5)) + + // Output: + // true + // false +} + +func ExampleIntIntMap_Size() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + fmt.Println(m.Size()) + + // Output: + // 4 +} + +func ExampleIntIntMap_IsEmpty() { + var m gmap.IntIntMap + fmt.Println(m.IsEmpty()) + + m.Set(1, 1) + fmt.Println(m.IsEmpty()) + + // Output: + // true + // false +} + +func ExampleIntIntMap_Clear() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + m.Clear() + + fmt.Println(m.Map()) + + // Output: + // map[] +} + +func ExampleIntIntMap_Replace() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + }) + + var n gmap.IntIntMap + n.Sets(g.MapIntInt{ + 2: 2, + }) + + fmt.Println(m.Map()) + + m.Replace(n.Map()) + fmt.Println(m.Map()) + + n.Set(2, 1) + fmt.Println(m.Map()) + + // Output: + // map[1:1] + // map[2:2] + // map[2:1] +} + +func ExampleIntIntMap_LockFunc() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + m.LockFunc(func(m map[int]int) { + totalValue := 0 + for _, v := range m { + totalValue += v + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleIntIntMap_RLockFunc() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + m.RLockFunc(func(m map[int]int) { + totalValue := 0 + for _, v := range m { + totalValue += v + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleIntIntMap_Flip() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 10, + }) + m.Flip() + fmt.Println(m.Map()) + + // Output: + // map[10:1] +} + +func ExampleIntIntMap_Merge() { + var m1, m2 gmap.Map + m1.Set(1, "1") + m2.Set(2, "2") + m1.Merge(&m2) + fmt.Println(m1.Map()) + + // May Output: + // map[key1:1 key2:2] +} + +func ExampleIntIntMap_String() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + }) + + fmt.Println(m.String()) + + // Output: + // {"1":1} +} + +func ExampleIntIntMap_MarshalJSON() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + bytes, err := m.MarshalJSON() + if err == nil { + fmt.Println(gconv.String(bytes)) + } + + // Output: + // {"1":1,"2":2,"3":3,"4":4} +} + +func ExampleIntIntMap_UnmarshalJSON() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + var n gmap.Map + + err := n.UnmarshalJSON(gconv.Bytes(m.String())) + if err == nil { + fmt.Println(n.Map()) + } + + // Output: + // map[1:1 2:2 3:3 4:4] +} + +func ExampleIntIntMap_UnmarshalValue() { + var m gmap.IntIntMap + m.Sets(g.MapIntInt{ + 1: 1, + 2: 2, + 3: 3, + 4: 4, + }) + + var n gmap.Map + err := n.UnmarshalValue(m.String()) + if err == nil { + fmt.Println(n.Map()) + } + // Output: + // map[1:1 2:2 3:3 4:4] +} diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_test.go index 7b00d8d8b..018151b15 100644 --- a/container/gmap/gmap_z_example_test.go +++ b/container/gmap/gmap_z_example_test.go @@ -158,3 +158,27 @@ func ExampleNewIntAnyMapFrom() { // {"1":"val1"} // {"1":"val1"} } + +func ExampleNewIntIntMap() { + m := gmap.NewIntIntMap() + + m.Set(1, 1) + fmt.Println(m) + + // Output: + // {"1":"1"} +} + +func ExampleNewIntIntMapFrom() { + m := gmap.NewIntIntMap() + + m.Set(1, 1) + fmt.Println(m) + + n := gmap.NewIntIntMapFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"1":"1"} + // {"1":"1"} +} From 51fd87bc013e7fa52330058280eaa0fcb43d153e Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 07:49:56 +0800 Subject: [PATCH 09/22] Implemented StrAnyMap Example --- container/gmap/gmap_z_example_str_any_test.go | 657 ++++++++++++++++++ container/gmap/gmap_z_example_test.go | 78 ++- 2 files changed, 732 insertions(+), 3 deletions(-) create mode 100644 container/gmap/gmap_z_example_str_any_test.go diff --git a/container/gmap/gmap_z_example_str_any_test.go b/container/gmap/gmap_z_example_str_any_test.go new file mode 100644 index 000000000..0eebfacfb --- /dev/null +++ b/container/gmap/gmap_z_example_str_any_test.go @@ -0,0 +1,657 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with gm file, +// You can obtain one at https://github.com/gogf/gf. + +package gmap_test + +import ( + "fmt" + "github.com/gogf/gf/v2/util/gconv" + + "github.com/gogf/gf/v2/container/gmap" + "github.com/gogf/gf/v2/frame/g" +) + +func ExampleStrAnyMap_Iterator() { + m := gmap.NewStrAnyMap() + for i := 1; i <= 10; i++ { + m.Set(gconv.String(i), i*2) + } + + var totalValue int + m.Iterator(func(k string, v interface{}) bool { + totalValue += v.(int) + + return totalValue < 50 + }) + + fmt.Println("totalValue:", totalValue) + + // May Output: + // totalValue: 52 +} + +func ExampleStrAnyMap_Clone() { + m := gmap.NewStrAnyMap() + + m.Set("key1", "val1") + fmt.Println(m) + + n := m.Clone() + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} + +func ExampleStrAnyMap_Map() { + // non concurrent-safety, a pointer to the underlying data + m1 := gmap.NewStrAnyMap() + m1.Set("key1", "val1") + fmt.Println("m1:", m1) + + n1 := m1.Map() + fmt.Println("before n1:", n1) + m1.Set("key1", "val2") + fmt.Println("after n1:", n1) + + // concurrent-safety, copy of underlying data + m2 := gmap.New(true) + m2.Set("key1", "val1") + fmt.Println("m2:", m2) + + n2 := m2.Map() + fmt.Println("before n2:", n2) + m2.Set("key1", "val2") + fmt.Println("after n2:", n2) + + // Output: + // m1: {"key1":"val1"} + // before n1: map[key1:val1] + // after n1: map[key1:val2] + // m2: {"key1":"val1"} + // before n2: map[key1:val1] + // after n2: map[key1:val1] +} + +func ExampleStrAnyMap_MapCopy() { + m := gmap.NewStrAnyMap() + + m.Set("key1", "val1") + m.Set("key2", "val2") + fmt.Println(m) + + n := m.MapCopy() + fmt.Println(n) + + // Output: + // {"key1":"val1","key2":"val2"} + // map[key1:val1 key2:val2] +} + +func ExampleStrAnyMap_MapStrAny() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + m.Set("key2", "val2") + + n := m.MapStrAny() + fmt.Println(n) + + // Output: + // map[key1:val1 key2:val2] +} + +func ExampleStrAnyMap_FilterEmpty() { + m := gmap.NewStrAnyMapFrom(g.MapStrAny{ + "k1": "", + "k2": nil, + "k3": 0, + "k4": 1, + }) + m.FilterEmpty() + fmt.Println(m.Map()) + + // Output: + // map[k4:1] +} + +func ExampleStrAnyMap_FilterNil() { + m := gmap.NewStrAnyMapFrom(g.MapStrAny{ + "k1": "", + "k2": nil, + "k3": 0, + "k4": 1, + }) + m.FilterNil() + fmt.Println(m.Map()) + + // May Output: + // map[k1: k3:0 k4:1] +} + +func ExampleStrAnyMap_Set() { + m := gmap.NewStrAnyMap() + + m.Set("key1", "val1") + fmt.Println(m) + + // Output: + // {"key1":"val1"} +} + +func ExampleStrAnyMap_Sets() { + m := gmap.NewStrAnyMap() + + addMap := make(map[string]interface{}) + addMap["key1"] = "val1" + addMap["key2"] = "val2" + addMap["key3"] = "val3" + + m.Sets(addMap) + fmt.Println(m) + + // Output: + // {"key1":"val1","key2":"val2","key3":"val3"} +} + +func ExampleStrAnyMap_Search() { + m := gmap.NewStrAnyMap() + + m.Set("key1", "val1") + + value, found := m.Search("key1") + if found { + fmt.Println("find key1 value:", value) + } + + value, found = m.Search("key2") + if !found { + fmt.Println("key2 not find") + } + + // Output: + // find key1 value: val1 + // key2 not find +} + +func ExampleStrAnyMap_Get() { + m := gmap.NewStrAnyMap() + + m.Set("key1", "val1") + + fmt.Println("key1 value:", m.Get("key1")) + fmt.Println("key2 value:", m.Get("key2")) + + // Output: + // key1 value: val1 + // key2 value: +} + +func ExampleStrAnyMap_Pop() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Pop()) + + // May Output: + // k1 v1 +} + +func ExampleStrAnyMap_Pops() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Pops(-1)) + fmt.Println("size:", m.Size()) + + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Pops(2)) + fmt.Println("size:", m.Size()) + + // May Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] + // size: 0 + // map[k1:v1 k2:v2] + // size: 2 +} + +func ExampleStrAnyMap_GetOrSet() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSet("key1", "NotExistValue")) + fmt.Println(m.GetOrSet("key2", "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleStrAnyMap_GetOrSetFunc() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFunc("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFunc("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrAnyMap_GetOrSetFuncLock() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrAnyMap_GetVar() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVar("key1")) + fmt.Println(m.GetVar("key2").IsNil()) + + // Output: + // val1 + // true +} + +func ExampleStrAnyMap_GetVarOrSet() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSet("key1", "NotExistValue")) + fmt.Println(m.GetVarOrSet("key2", "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleStrAnyMap_GetVarOrSetFunc() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrAnyMap_GetVarOrSetFuncLock() { + m := gmap.NewStrAnyMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrAnyMap_SetIfNotExist() { + var m gmap.StrAnyMap + fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleStrAnyMap_SetIfNotExistFunc() { + var m gmap.StrAnyMap + fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleStrAnyMap_SetIfNotExistFuncLock() { + var m gmap.StrAnyMap + fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleStrAnyMap_Remove() { + var m gmap.StrAnyMap + m.Set("k1", "v1") + + fmt.Println(m.Remove("k1")) + fmt.Println(m.Remove("k2")) + + // Output: + // v1 + // +} + +func ExampleStrAnyMap_Removes() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + removeList := make([]string, 2) + removeList = append(removeList, "k1") + removeList = append(removeList, "k2") + + m.Removes(removeList) + + fmt.Println(m.Map()) + + // Output: + // map[k3:v3 k4:v4] +} + +func ExampleStrAnyMap_Keys() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Keys()) + + // May Output: + // [k1 k2 k3 k4] +} + +func ExampleStrAnyMap_Values() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Values()) + + // May Output: + // [v1 v2 v3 v4] +} + +func ExampleStrAnyMap_Contains() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Contains("k1")) + fmt.Println(m.Contains("k5")) + + // Output: + // true + // false +} + +func ExampleStrAnyMap_Size() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Size()) + + // Output: + // 4 +} + +func ExampleStrAnyMap_IsEmpty() { + var m gmap.StrAnyMap + fmt.Println(m.IsEmpty()) + + m.Set("k1", "v1") + fmt.Println(m.IsEmpty()) + + // Output: + // true + // false +} + +func ExampleStrAnyMap_Clear() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + m.Clear() + + fmt.Println(m.Map()) + + // Output: + // map[] +} + +func ExampleStrAnyMap_Replace() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + }) + + var n gmap.StrAnyMap + n.Sets(g.MapStrAny{ + "k2": "v2", + }) + + fmt.Println(m.Map()) + + m.Replace(n.Map()) + fmt.Println(m.Map()) + + n.Set("k2", "v1") + fmt.Println(m.Map()) + + // Output: + // map[k1:v1] + // map[k2:v2] + // map[k2:v1] +} + +func ExampleStrAnyMap_LockFunc() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.LockFunc(func(m map[string]interface{}) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleStrAnyMap_RLockFunc() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.RLockFunc(func(m map[string]interface{}) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleStrAnyMap_Flip() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + }) + m.Flip() + fmt.Println(m.Map()) + + // Output: + // map[v1:k1] +} + +func ExampleStrAnyMap_Merge() { + var m1, m2 gmap.StrAnyMap + m1.Set("key1", "val1") + m2.Set("key2", "val2") + m1.Merge(&m2) + fmt.Println(m1.Map()) + + // May Output: + // map[key1:val1 key2:val2] +} + +func ExampleStrAnyMap_String() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + }) + + fmt.Println(m.String()) + + // Output: + // {"k1":"v1"} +} + +func ExampleStrAnyMap_MarshalJSON() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + bytes, err := m.MarshalJSON() + if err == nil { + fmt.Println(gconv.String(bytes)) + } + + // Output: + // {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} +} + +func ExampleStrAnyMap_UnmarshalJSON() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + var n gmap.StrAnyMap + + err := n.UnmarshalJSON(gconv.Bytes(m.String())) + if err == nil { + fmt.Println(n.Map()) + } + + // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] +} + +func ExampleStrAnyMap_UnmarshalValue() { + var m gmap.StrAnyMap + m.Sets(g.MapStrAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + var n gmap.StrAnyMap + err := n.UnmarshalValue(m.String()) + if err == nil { + fmt.Println(n.Map()) + } + // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] +} diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_test.go index 018151b15..7336d898c 100644 --- a/container/gmap/gmap_z_example_test.go +++ b/container/gmap/gmap_z_example_test.go @@ -166,7 +166,7 @@ func ExampleNewIntIntMap() { fmt.Println(m) // Output: - // {"1":"1"} + // {"1":1} } func ExampleNewIntIntMapFrom() { @@ -179,6 +179,78 @@ func ExampleNewIntIntMapFrom() { fmt.Println(n) // Output: - // {"1":"1"} - // {"1":"1"} + // {"1":1} + // {"1":1} +} + +func ExampleNewStrAnyMap() { + m := gmap.NewStrAnyMap() + + m.Set("key1", "var1") + fmt.Println(m) + + // Output: + // {"key1":"var1"} +} + +func ExampleNewStrAnyMapFrom() { + m := gmap.NewStrAnyMap() + + m.Set("key1", "var1") + fmt.Println(m) + + n := gmap.NewStrAnyMapFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":"var1"} + // {"key1":"var1"} +} + +func ExampleNewStrIntMap() { + m := gmap.NewStrIntMap() + + m.Set("key1", 1) + fmt.Println(m) + + // Output: + // {"key1":1} +} + +func ExampleNewStrIntMapFrom() { + m := gmap.NewStrIntMap() + + m.Set("key1", 1) + fmt.Println(m) + + n := gmap.NewStrIntMapFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":1} + // {"key1":1} +} + +func ExampleNewStrStrMap() { + m := gmap.NewStrStrMap() + + m.Set("key1", "var1") + fmt.Println(m) + + // Output: + // {"key1":"var1"} +} + +func ExampleNewStrStrMapFrom() { + m := gmap.NewStrStrMap() + + m.Set("key1", "var1") + fmt.Println(m) + + n := gmap.NewStrStrMapFrom(m.MapCopy(), true) + fmt.Println(n) + + // Output: + // {"key1":"var1"} + // {"key1":"var1"} } From 49edad021688debf0a72a960b02095713b61d7a9 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 08:13:10 +0800 Subject: [PATCH 10/22] Implemented StrAnyMap Example --- container/gmap/gmap_z_example_str_any_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/gmap/gmap_z_example_str_any_test.go b/container/gmap/gmap_z_example_str_any_test.go index 0eebfacfb..5842d4d3b 100644 --- a/container/gmap/gmap_z_example_str_any_test.go +++ b/container/gmap/gmap_z_example_str_any_test.go @@ -59,7 +59,7 @@ func ExampleStrAnyMap_Map() { fmt.Println("after n1:", n1) // concurrent-safety, copy of underlying data - m2 := gmap.New(true) + m2 := gmap.NewStrAnyMap(true) m2.Set("key1", "val1") fmt.Println("m2:", m2) From 1db2c31ecc3c8e789e8afcd271ae8087c3586627 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 08:47:44 +0800 Subject: [PATCH 11/22] Implemented StrIntMap Example --- container/gmap/gmap_z_example_str_int_test.go | 593 ++++++++++++++++++ 1 file changed, 593 insertions(+) create mode 100644 container/gmap/gmap_z_example_str_int_test.go diff --git a/container/gmap/gmap_z_example_str_int_test.go b/container/gmap/gmap_z_example_str_int_test.go new file mode 100644 index 000000000..fe8dd7408 --- /dev/null +++ b/container/gmap/gmap_z_example_str_int_test.go @@ -0,0 +1,593 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with gm file, +// You can obtain one at https://github.com/gogf/gf. + +package gmap_test + +import ( + "fmt" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/util/gconv" + + "github.com/gogf/gf/v2/container/gmap" +) + +func ExampleStrIntMap_Iterator() { + m := gmap.NewStrIntMap() + for i := 0; i < 10; i++ { + m.Set(gconv.String(i), i*2) + } + + var totalValue int + m.Iterator(func(k string, v int) bool { + totalValue += v + + return totalValue < 50 + }) + + fmt.Println("totalValue:", totalValue) + + // May Output: + // totalValue: 52 +} + +func ExampleStrIntMap_Clone() { + m := gmap.NewStrIntMap() + + m.Set("key1", 1) + fmt.Println(m) + + n := m.Clone() + fmt.Println(n) + + // Output: + // {"key1":1} + // {"key1":1} +} + +func ExampleStrIntMap_Map() { + // non concurrent-safety, a pointer to the underlying data + m1 := gmap.NewStrIntMap() + m1.Set("key1", 1) + fmt.Println("m1:", m1) + + n1 := m1.Map() + fmt.Println("before n1:", n1) + m1.Set("key1", 2) + fmt.Println("after n1:", n1) + + // concurrent-safety, copy of underlying data + m2 := gmap.NewStrIntMap(true) + m2.Set("key1", 1) + fmt.Println("m2:", m2) + + n2 := m2.Map() + fmt.Println("before n2:", n2) + m2.Set("key1", 2) + fmt.Println("after n2:", n2) + + // Output: + // m1: {"key1":1} + // before n1: map[key1:1] + // after n1: map[key1:2] + // m2: {"key1":1} + // before n2: map[key1:1] + // after n2: map[key1:1] +} + +func ExampleStrIntMap_MapCopy() { + m := gmap.NewStrIntMap() + + m.Set("key1", 1) + m.Set("key2", 2) + fmt.Println(m) + + n := m.MapCopy() + fmt.Println(n) + + // Output: + // {"key1":1,"key2":2} + // map[key1:1 key2:2] +} + +func ExampleStrIntMap_MapStrAny() { + m := gmap.NewStrIntMap() + m.Set("key1", 1) + m.Set("key2", 2) + + n := m.MapStrAny() + fmt.Println(n) + + // Output: + // map[key1:1 key2:2] +} + +func ExampleStrIntMap_FilterEmpty() { + m := gmap.NewStrIntMapFrom(g.MapStrInt{ + "k1": 0, + "k2": 1, + }) + m.FilterEmpty() + fmt.Println(m.Map()) + + // Output: + // map[k2:1] +} + +func ExampleStrIntMap_Set() { + m := gmap.NewStrIntMap() + + m.Set("key1", 1) + fmt.Println(m) + + // Output: + // {"key1":1} +} + +func ExampleStrIntMap_Sets() { + m := gmap.NewStrIntMap() + + addMap := make(map[string]int) + addMap["key1"] = 1 + addMap["key2"] = 2 + addMap["key3"] = 3 + + m.Sets(addMap) + fmt.Println(m) + + // Output: + // {"key1":1,"key2":2,"key3":3} +} + +func ExampleStrIntMap_Search() { + m := gmap.NewStrIntMap() + + m.Set("key1", 1) + + value, found := m.Search("key1") + if found { + fmt.Println("find key1 value:", value) + } + + value, found = m.Search("key2") + if !found { + fmt.Println("key2 not find") + } + + // Output: + // find key1 value: 1 + // key2 not find +} + +func ExampleStrIntMap_Get() { + m := gmap.NewStrIntMap() + + m.Set("key1", 1) + + fmt.Println("key1 value:", m.Get("key1")) + fmt.Println("key2 value:", m.Get("key2")) + + // Output: + // key1 value: 1 + // key2 value: 0 +} + +func ExampleStrIntMap_Pop() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + fmt.Println(m.Pop()) + + // May Output: + // k1 1 +} + +func ExampleStrIntMap_Pops() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + fmt.Println(m.Pops(-1)) + fmt.Println("size:", m.Size()) + + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + fmt.Println(m.Pops(2)) + fmt.Println("size:", m.Size()) + + // May Output: + // map[k1:1 k2:2 k3:3 k4:4] + // size: 0 + // map[k1:1 k2:2] + // size: 2 +} + +func ExampleStrIntMap_GetOrSet() { + m := gmap.NewStrIntMap() + m.Set("key1", 1) + + fmt.Println(m.GetOrSet("key1", 0)) + fmt.Println(m.GetOrSet("key2", 2)) + + // Output: + // 1 + // 2 +} + +func ExampleStrIntMap_GetOrSetFunc() { + m := gmap.NewStrIntMap() + m.Set("key1", 1) + + fmt.Println(m.GetOrSetFunc("key1", func() int { + return 0 + })) + fmt.Println(m.GetOrSetFunc("key2", func() int { + return 0 + })) + + // Output: + // 1 + // 0 +} + +func ExampleStrIntMap_GetOrSetFuncLock() { + m := gmap.NewStrIntMap() + m.Set("key1", 1) + + fmt.Println(m.GetOrSetFuncLock("key1", func() int { + return 0 + })) + fmt.Println(m.GetOrSetFuncLock("key2", func() int { + return 0 + })) + + // Output: + // 1 + // 0 +} + +func ExampleStrIntMap_SetIfNotExist() { + var m gmap.StrIntMap + fmt.Println(m.SetIfNotExist("k1", 1)) + fmt.Println(m.SetIfNotExist("k1", 1)) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:1] +} + +func ExampleStrIntMap_SetIfNotExistFunc() { + var m gmap.StrIntMap + fmt.Println(m.SetIfNotExistFunc("k1", func() int { + return 1 + })) + fmt.Println(m.SetIfNotExistFunc("k1", func() int { + return 1 + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:1] +} + +func ExampleStrIntMap_SetIfNotExistFuncLock() { + var m gmap.StrIntMap + fmt.Println(m.SetIfNotExistFuncLock("k1", func() int { + return 1 + })) + fmt.Println(m.SetIfNotExistFuncLock("k1", func() int { + return 1 + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:1] +} + +func ExampleStrIntMap_Remove() { + var m gmap.StrIntMap + m.Set("k1", 1) + + fmt.Println(m.Remove("k1")) + fmt.Println(m.Remove("k2")) + + // Output: + // 1 + // 0 +} + +func ExampleStrIntMap_Removes() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + removeList := make([]string, 2) + removeList = append(removeList, "k1") + removeList = append(removeList, "k2") + + m.Removes(removeList) + + fmt.Println(m.Map()) + + // Output: + // map[k3:3 k4:4] +} + +func ExampleStrIntMap_Keys() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + fmt.Println(m.Keys()) + + // May Output: + // [k1 k2 k3 k4] +} + +func ExampleStrIntMap_Values() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + fmt.Println(m.Values()) + + // May Output: + // [1 2 3 4] +} + +func ExampleStrIntMap_Contains() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + fmt.Println(m.Contains("k1")) + fmt.Println(m.Contains("k5")) + + // Output: + // true + // false +} + +func ExampleStrIntMap_Size() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + fmt.Println(m.Size()) + + // Output: + // 4 +} + +func ExampleStrIntMap_IsEmpty() { + var m gmap.StrIntMap + fmt.Println(m.IsEmpty()) + + m.Set("k1", 1) + fmt.Println(m.IsEmpty()) + + // Output: + // true + // false +} + +func ExampleStrIntMap_Clear() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.Clear() + + fmt.Println(m.Map()) + + // Output: + // map[] +} + +func ExampleStrIntMap_Replace() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + }) + + var n gmap.StrIntMap + n.Sets(g.MapStrInt{ + "k2": 2, + }) + + fmt.Println(m.Map()) + + m.Replace(n.Map()) + fmt.Println(m.Map()) + + n.Set("k2", 1) + fmt.Println(m.Map()) + + // Output: + // map[k1:1] + // map[k2:2] + // map[k2:1] +} + +func ExampleStrIntMap_LockFunc() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.LockFunc(func(m map[string]int) { + totalValue := 0 + for _, v := range m { + totalValue += v + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleStrIntMap_RLockFunc() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.RLockFunc(func(m map[string]int) { + totalValue := 0 + for _, v := range m { + totalValue += v + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleStrIntMap_Flip() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + }) + m.Flip() + fmt.Println(m.Map()) + + var n gmap.StrIntMap + n.Sets(g.MapStrInt{ + "11": 1, + }) + n.Flip() + fmt.Println(n.Map()) + + // Output: + // map[1:0] + // map[1:11] +} + +func ExampleStrIntMap_Merge() { + var m1, m2 gmap.StrIntMap + m1.Set("key1", 1) + m2.Set("key2", 2) + m1.Merge(&m2) + fmt.Println(m1.Map()) + + // May Output: + // map[key1:1 key2:2] +} + +func ExampleStrIntMap_String() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + }) + + fmt.Println(m.String()) + + // Output: + // {"k1":1} +} + +func ExampleStrIntMap_MarshalJSON() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + bytes, err := m.MarshalJSON() + if err == nil { + fmt.Println(gconv.String(bytes)) + } + + // Output: + // {"k1":1,"k2":2,"k3":3,"k4":4} +} + +func ExampleStrIntMap_UnmarshalJSON() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + var n gmap.StrIntMap + + err := n.UnmarshalJSON(gconv.Bytes(m.String())) + if err == nil { + fmt.Println(n.Map()) + } + + // Output: + // map[k1:1 k2:2 k3:3 k4:4] +} + +func ExampleStrIntMap_UnmarshalValue() { + var m gmap.StrIntMap + m.Sets(g.MapStrInt{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + var n gmap.StrIntMap + err := n.UnmarshalValue(m.String()) + if err == nil { + fmt.Println(n.Map()) + } + // Output: + // map[k1:1 k2:2 k3:3 k4:4] +} From a99cc34243d3ed5658ae113ddae915600321d43f Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 12:12:32 +0800 Subject: [PATCH 12/22] Implemented StrStrMap Example --- container/gmap/gmap_z_example_str_str_test.go | 645 ++++++++++++++++++ 1 file changed, 645 insertions(+) create mode 100644 container/gmap/gmap_z_example_str_str_test.go diff --git a/container/gmap/gmap_z_example_str_str_test.go b/container/gmap/gmap_z_example_str_str_test.go new file mode 100644 index 000000000..02bf5f041 --- /dev/null +++ b/container/gmap/gmap_z_example_str_str_test.go @@ -0,0 +1,645 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with gm file, +// You can obtain one at https://github.com/gogf/gf. + +package gmap_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/util/gconv" + + "github.com/gogf/gf/v2/container/gmap" +) + +func ExampleStrStrMap_Iterator() { + m := gmap.NewStrStrMap() + for i := 0; i < 10; i++ { + m.Set("key"+gconv.String(i), "var"+gconv.String(i)) + } + + var str string + m.Iterator(func(k string, v string) bool { + + str += v + "|" + + return len(str) < 20 + }) + + fmt.Println("str:", str) + + // May Output: + // var0|var1|var2|var3| +} + +func ExampleStrStrMap_Clone() { + m := gmap.NewStrStrMap() + + m.Set("key1", "val1") + fmt.Println(m) + + n := m.Clone() + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} + +func ExampleStrStrMap_Map() { + // non concurrent-safety, a pointer to the underlying data + m1 := gmap.NewStrStrMap() + m1.Set("key1", "val1") + fmt.Println("m1:", m1) + + n1 := m1.Map() + fmt.Println("before n1:", n1) + m1.Set("key1", "val2") + fmt.Println("after n1:", n1) + + // concurrent-safety, copy of underlying data + m2 := gmap.NewStrStrMap(true) + m2.Set("key1", "val1") + fmt.Println("m2:", m2) + + n2 := m2.Map() + fmt.Println("before n2:", n2) + m2.Set("key1", "val2") + fmt.Println("after n2:", n2) + + // Output: + // m1: {"key1":"val1"} + // before n1: map[key1:val1] + // after n1: map[key1:val2] + // m2: {"key1":"val1"} + // before n2: map[key1:val1] + // after n2: map[key1:val1] +} + +func ExampleStrStrMap_MapCopy() { + m := gmap.NewStrStrMap() + + m.Set("key1", "val1") + m.Set("key2", "val2") + fmt.Println(m) + + n := m.MapCopy() + fmt.Println(n) + + // Output: + // {"key1":"val1","key2":"val2"} + // map[key1:val1 key2:val2] +} + +func ExampleStrStrMap_MapStrAny() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + m.Set("key2", "val2") + + n := m.MapStrAny() + fmt.Println(n) + + // Output: + // map[key1:val1 key2:val2] +} + +func ExampleStrStrMap_FilterEmpty() { + m := gmap.NewStrStrMapFrom(g.MapStrStr{ + "k1": "", + "k2": "v2", + }) + m.FilterEmpty() + fmt.Println(m.Map()) + + // Output: + // map[k2:v2] +} + +func ExampleStrStrMap_Set() { + m := gmap.NewStrStrMap() + + m.Set("key1", "val1") + fmt.Println(m) + + // Output: + // {"key1":"val1"} +} + +func ExampleStrStrMap_Sets() { + m := gmap.NewStrStrMap() + + addMap := make(map[string]string) + addMap["key1"] = "val1" + addMap["key2"] = "val2" + addMap["key3"] = "val3" + + m.Sets(addMap) + fmt.Println(m) + + // Output: + // {"key1":"val1","key2":"val2","key3":"val3"} +} + +func ExampleStrStrMap_Search() { + m := gmap.NewStrStrMap() + + m.Set("key1", "val1") + + value, found := m.Search("key1") + if found { + fmt.Println("find key1 value:", value) + } + + value, found = m.Search("key2") + if !found { + fmt.Println("key2 not find") + } + + // Output: + // find key1 value: val1 + // key2 not find +} + +func ExampleStrStrMap_Get() { + m := gmap.NewStrStrMap() + + m.Set("key1", "val1") + + fmt.Println("key1 value:", m.Get("key1")) + fmt.Println("key2 value:", m.Get("key2")) + + // Output: + // key1 value: val1 + // key2 value: +} + +/* +func ExampleStrStrMap_Pop() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Pop()) + + // May Output: + // k1 v1 +} + +func ExampleStrStrMap_Pops() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Pops(-1)) + fmt.Println("size:", m.Size()) + + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Pops(2)) + fmt.Println("size:", m.Size()) + + // May Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] + // size: 0 + // map[k1:v1 k2:v2] + // size: 2 +} + +func ExampleStrStrMap_GetOrSet() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSet("key1", "NotExistValue")) + fmt.Println(m.GetOrSet("key2", "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleStrStrMap_GetOrSetFunc() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFunc("key1", func() string { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFunc("key2", func() string { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrStrMap_GetOrSetFuncLock() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFuncLock("key1", func() string { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFuncLock("key2", func() string { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrStrMap_GetVar() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVar("key1")) + fmt.Println(m.GetVar("key2").IsNil()) + + // Output: + // val1 + // true +} + +func ExampleStrStrMap_GetVarOrSet() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSet("key1", "NotExistValue")) + fmt.Println(m.GetVarOrSet("key2", "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleStrStrMap_GetVarOrSetFunc() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFunc("key1", func() string { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFunc("key2", func() string { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrStrMap_GetVarOrSetFuncLock() { + m := gmap.NewStrStrMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFuncLock("key1", func() string { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFuncLock("key2", func() string { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleStrStrMap_SetIfNotExist() { + var m gmap.Map + fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleStrStrMap_SetIfNotExistFunc() { + var m gmap.Map + fmt.Println(m.SetIfNotExistFunc("k1", func() string { + return "v1" + })) + fmt.Println(m.SetIfNotExistFunc("k1", func() string { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleStrStrMap_SetIfNotExistFuncLock() { + var m gmap.Map + fmt.Println(m.SetIfNotExistFuncLock("k1", func() string { + return "v1" + })) + fmt.Println(m.SetIfNotExistFuncLock("k1", func() string { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleStrStrMap_Remove() { + var m gmap.Map + m.Set("k1", "v1") + + fmt.Println(m.Remove("k1")) + fmt.Println(m.Remove("k2")) + + // Output: + // v1 + // +} + +func ExampleStrStrMap_Removes() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + removeList := make([]string, 2) + removeList = append(removeList, "k1") + removeList = append(removeList, "k2") + + m.Removes(removeList) + + fmt.Println(m.Map()) + + // Output: + // map[k3:v3 k4:v4] +} + +func ExampleStrStrMap_Keys() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Keys()) + + // May Output: + // [k1 k2 k3 k4] +} + +func ExampleStrStrMap_Values() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Values()) + + // May Output: + // [v1 v2 v3 v4] +} + +func ExampleStrStrMap_Contains() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Contains("k1")) + fmt.Println(m.Contains("k5")) + + // Output: + // true + // false +} + +func ExampleStrStrMap_Size() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Size()) + + // Output: + // 4 +} + +func ExampleStrStrMap_IsEmpty() { + var m gmap.Map + fmt.Println(m.IsEmpty()) + + m.Set("k1", "v1") + fmt.Println(m.IsEmpty()) + + // Output: + // true + // false +} + +func ExampleStrStrMap_Clear() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + m.Clear() + + fmt.Println(m.Map()) + + // Output: + // map[] +} + +func ExampleStrStrMap_Replace() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + }) + + var n gmap.Map + n.Sets(g.MapStrStr{ + "k2": "v2", + }) + + fmt.Println(m.Map()) + + m.Replace(n.Map()) + fmt.Println(m.Map()) + + n.Set("k2", "v1") + fmt.Println(m.Map()) + + // Output: + // map[k1:v1] + // map[k2:v2] + // map[k2:v1] +} + +func ExampleStrStrMap_LockFunc() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.LockFunc(func(m map[string]string) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleStrStrMap_RLockFunc() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": 1, + "k2": 2, + "k3": 3, + "k4": 4, + }) + + m.RLockFunc(func(m map[string]string) { + totalValue := 0 + for _, v := range m { + totalValue += v.(int) + } + fmt.Println("totalValue:", totalValue) + }) + + // Output: + // totalValue: 10 +} + +func ExampleStrStrMap_Flip() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + }) + m.Flip() + fmt.Println(m.Map()) + + // Output: + // map[v1:k1] +} + +func ExampleStrStrMap_Merge() { + var m1, m2 gmap.Map + m1.Set("key1", "val1") + m2.Set("key2", "val2") + m1.Merge(&m2) + fmt.Println(m1.Map()) + + // May Output: + // map[key1:val1 key2:val2] +} + +func ExampleStrStrMap_String() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + }) + + fmt.Println(m.String()) + + // Output: + // {"k1":"v1"} +} + +func ExampleStrStrMap_MarshalJSON() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + bytes, err := m.MarshalJSON() + if err == nil { + fmt.Println(gconv.String(bytes)) + } + + // Output: + // {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} +} + +func ExampleStrStrMap_UnmarshalJSON() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + var n gmap.Map + + err := n.UnmarshalJSON(gconv.Bytes(m.String())) + if err == nil { + fmt.Println(n.Map()) + } + + // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] +} + +func ExampleStrStrMap_UnmarshalValue() { + var m gmap.Map + m.Sets(g.MapStrStr{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + var n gmap.Map + err := n.UnmarshalValue(m.String()) + if err == nil { + fmt.Println(n.Map()) + } + // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] +} +*/ From 7d75f6e7c5cc005f9740b7d5196532445b52ec22 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 12:13:05 +0800 Subject: [PATCH 13/22] Implemented StrStrMap Example --- container/gmap/gmap_z_example_list_test.go | 1 + container/gmap/gmap_z_example_tree_test.go | 1 + 2 files changed, 2 insertions(+) create mode 100644 container/gmap/gmap_z_example_list_test.go create mode 100644 container/gmap/gmap_z_example_tree_test.go diff --git a/container/gmap/gmap_z_example_list_test.go b/container/gmap/gmap_z_example_list_test.go new file mode 100644 index 000000000..4d2e77928 --- /dev/null +++ b/container/gmap/gmap_z_example_list_test.go @@ -0,0 +1 @@ +package gmap diff --git a/container/gmap/gmap_z_example_tree_test.go b/container/gmap/gmap_z_example_tree_test.go new file mode 100644 index 000000000..4d2e77928 --- /dev/null +++ b/container/gmap/gmap_z_example_tree_test.go @@ -0,0 +1 @@ +package gmap From 61bc38fff68f48aa3ec3f966073989515ad294fc Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 12:27:01 +0800 Subject: [PATCH 14/22] Implemented StrStrMap Example --- container/gmap/gmap_z_example_str_str_test.go | 150 ++++++------------ 1 file changed, 47 insertions(+), 103 deletions(-) diff --git a/container/gmap/gmap_z_example_str_str_test.go b/container/gmap/gmap_z_example_str_str_test.go index 02bf5f041..a068a1869 100644 --- a/container/gmap/gmap_z_example_str_str_test.go +++ b/container/gmap/gmap_z_example_str_str_test.go @@ -176,9 +176,8 @@ func ExampleStrStrMap_Get() { // key2 value: } -/* func ExampleStrStrMap_Pop() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -193,7 +192,7 @@ func ExampleStrStrMap_Pop() { } func ExampleStrStrMap_Pops() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -263,64 +262,8 @@ func ExampleStrStrMap_GetOrSetFuncLock() { // NotExistValue } -func ExampleStrStrMap_GetVar() { - m := gmap.NewStrStrMap() - m.Set("key1", "val1") - - fmt.Println(m.GetVar("key1")) - fmt.Println(m.GetVar("key2").IsNil()) - - // Output: - // val1 - // true -} - -func ExampleStrStrMap_GetVarOrSet() { - m := gmap.NewStrStrMap() - m.Set("key1", "val1") - - fmt.Println(m.GetVarOrSet("key1", "NotExistValue")) - fmt.Println(m.GetVarOrSet("key2", "val2")) - - // Output: - // val1 - // val2 -} - -func ExampleStrStrMap_GetVarOrSetFunc() { - m := gmap.NewStrStrMap() - m.Set("key1", "val1") - - fmt.Println(m.GetVarOrSetFunc("key1", func() string { - return "NotExistValue" - })) - fmt.Println(m.GetVarOrSetFunc("key2", func() string { - return "NotExistValue" - })) - - // Output: - // val1 - // NotExistValue -} - -func ExampleStrStrMap_GetVarOrSetFuncLock() { - m := gmap.NewStrStrMap() - m.Set("key1", "val1") - - fmt.Println(m.GetVarOrSetFuncLock("key1", func() string { - return "NotExistValue" - })) - fmt.Println(m.GetVarOrSetFuncLock("key2", func() string { - return "NotExistValue" - })) - - // Output: - // val1 - // NotExistValue -} - func ExampleStrStrMap_SetIfNotExist() { - var m gmap.Map + var m gmap.StrStrMap fmt.Println(m.SetIfNotExist("k1", "v1")) fmt.Println(m.SetIfNotExist("k1", "v1")) fmt.Println(m.Map()) @@ -332,7 +275,7 @@ func ExampleStrStrMap_SetIfNotExist() { } func ExampleStrStrMap_SetIfNotExistFunc() { - var m gmap.Map + var m gmap.StrStrMap fmt.Println(m.SetIfNotExistFunc("k1", func() string { return "v1" })) @@ -348,7 +291,7 @@ func ExampleStrStrMap_SetIfNotExistFunc() { } func ExampleStrStrMap_SetIfNotExistFuncLock() { - var m gmap.Map + var m gmap.StrStrMap fmt.Println(m.SetIfNotExistFuncLock("k1", func() string { return "v1" })) @@ -364,19 +307,19 @@ func ExampleStrStrMap_SetIfNotExistFuncLock() { } func ExampleStrStrMap_Remove() { - var m gmap.Map + var m gmap.StrStrMap m.Set("k1", "v1") fmt.Println(m.Remove("k1")) - fmt.Println(m.Remove("k2")) + fmt.Println(len(m.Remove("k2"))) // Output: // v1 - // + // 0 } func ExampleStrStrMap_Removes() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -397,7 +340,7 @@ func ExampleStrStrMap_Removes() { } func ExampleStrStrMap_Keys() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -411,7 +354,7 @@ func ExampleStrStrMap_Keys() { } func ExampleStrStrMap_Values() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -425,7 +368,7 @@ func ExampleStrStrMap_Values() { } func ExampleStrStrMap_Contains() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -442,7 +385,7 @@ func ExampleStrStrMap_Contains() { } func ExampleStrStrMap_Size() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -457,7 +400,7 @@ func ExampleStrStrMap_Size() { } func ExampleStrStrMap_IsEmpty() { - var m gmap.Map + var m gmap.StrStrMap fmt.Println(m.IsEmpty()) m.Set("k1", "v1") @@ -469,7 +412,7 @@ func ExampleStrStrMap_IsEmpty() { } func ExampleStrStrMap_Clear() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -486,12 +429,12 @@ func ExampleStrStrMap_Clear() { } func ExampleStrStrMap_Replace() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", }) - var n gmap.Map + var n gmap.StrStrMap n.Sets(g.MapStrStr{ "k2": "v2", }) @@ -511,49 +454,51 @@ func ExampleStrStrMap_Replace() { } func ExampleStrStrMap_LockFunc() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ - "k1": 1, - "k2": 2, - "k3": 3, - "k4": 4, + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", }) m.LockFunc(func(m map[string]string) { - totalValue := 0 - for _, v := range m { - totalValue += v.(int) + for k, v := range m { + fmt.Println("key:", k, " value:", v) } - fmt.Println("totalValue:", totalValue) }) // Output: - // totalValue: 10 + // key: k1 value: v1 + // key: k2 value: v2 + // key: k3 value: v3 + // key: k4 value: v4 } func ExampleStrStrMap_RLockFunc() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ - "k1": 1, - "k2": 2, - "k3": 3, - "k4": 4, + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", }) m.RLockFunc(func(m map[string]string) { - totalValue := 0 - for _, v := range m { - totalValue += v.(int) + for k, v := range m { + fmt.Println("key:", k, " value:", v) } - fmt.Println("totalValue:", totalValue) }) // Output: - // totalValue: 10 + // key: k1 value: v1 + // key: k2 value: v2 + // key: k3 value: v3 + // key: k4 value: v4 } func ExampleStrStrMap_Flip() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", }) @@ -565,7 +510,7 @@ func ExampleStrStrMap_Flip() { } func ExampleStrStrMap_Merge() { - var m1, m2 gmap.Map + var m1, m2 gmap.StrStrMap m1.Set("key1", "val1") m2.Set("key2", "val2") m1.Merge(&m2) @@ -576,7 +521,7 @@ func ExampleStrStrMap_Merge() { } func ExampleStrStrMap_String() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", }) @@ -588,7 +533,7 @@ func ExampleStrStrMap_String() { } func ExampleStrStrMap_MarshalJSON() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -606,7 +551,7 @@ func ExampleStrStrMap_MarshalJSON() { } func ExampleStrStrMap_UnmarshalJSON() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -614,7 +559,7 @@ func ExampleStrStrMap_UnmarshalJSON() { "k4": "v4", }) - var n gmap.Map + var n gmap.StrStrMap err := n.UnmarshalJSON(gconv.Bytes(m.String())) if err == nil { @@ -626,7 +571,7 @@ func ExampleStrStrMap_UnmarshalJSON() { } func ExampleStrStrMap_UnmarshalValue() { - var m gmap.Map + var m gmap.StrStrMap m.Sets(g.MapStrStr{ "k1": "v1", "k2": "v2", @@ -634,7 +579,7 @@ func ExampleStrStrMap_UnmarshalValue() { "k4": "v4", }) - var n gmap.Map + var n gmap.StrStrMap err := n.UnmarshalValue(m.String()) if err == nil { fmt.Println(n.Map()) @@ -642,4 +587,3 @@ func ExampleStrStrMap_UnmarshalValue() { // Output: // map[k1:v1 k2:v2 k3:v3 k4:v4] } -*/ From 3e5b3a6dad7ac2faff772a69c2cb333b5141c07c Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 12:37:09 +0800 Subject: [PATCH 15/22] Implemented StrStrMap Example --- container/gmap/gmap_z_example_str_str_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/container/gmap/gmap_z_example_str_str_test.go b/container/gmap/gmap_z_example_str_str_test.go index a068a1869..093f70793 100644 --- a/container/gmap/gmap_z_example_str_str_test.go +++ b/container/gmap/gmap_z_example_str_str_test.go @@ -468,7 +468,7 @@ func ExampleStrStrMap_LockFunc() { } }) - // Output: + // May Output: // key: k1 value: v1 // key: k2 value: v2 // key: k3 value: v3 @@ -490,7 +490,7 @@ func ExampleStrStrMap_RLockFunc() { } }) - // Output: + // May Output: // key: k1 value: v1 // key: k2 value: v2 // key: k3 value: v3 From 8ea4b71e7a056885d7c30654bc47ab36bea8ba20 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 20 Nov 2021 21:46:20 +0800 Subject: [PATCH 16/22] Implemented ListMap Example --- container/gmap/gmap_z_example_list_test.go | 615 ++++++++++++++++++++- container/gmap/gmap_z_example_test.go | 26 + 2 files changed, 640 insertions(+), 1 deletion(-) diff --git a/container/gmap/gmap_z_example_list_test.go b/container/gmap/gmap_z_example_list_test.go index 4d2e77928..0c9530198 100644 --- a/container/gmap/gmap_z_example_list_test.go +++ b/container/gmap/gmap_z_example_list_test.go @@ -1 +1,614 @@ -package gmap +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with gm file, +// You can obtain one at https://github.com/gogf/gf. + +package gmap_test + +import ( + "fmt" + "github.com/gogf/gf/v2/container/gmap" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/util/gconv" +) + +func ExampleListMap_Iterator() { + m := gmap.NewListMap() + for i := 0; i < 10; i++ { + m.Set(i, i*2) + } + + var totalKey, totalValue int + m.Iterator(func(k interface{}, v interface{}) bool { + totalKey += k.(int) + totalValue += v.(int) + + return totalKey < 10 + }) + + fmt.Println("totalKey:", totalKey) + fmt.Println("totalValue:", totalValue) + + // Output: + // totalKey: 10 + // totalValue: 20 +} + +func ExampleListMap_IteratorAsc() { + m := gmap.NewListMap() + for i := 0; i < 10; i++ { + m.Set(i, i*2) + } + + var totalKey, totalValue int + m.IteratorAsc(func(k interface{}, v interface{}) bool { + totalKey += k.(int) + totalValue += v.(int) + + return totalKey < 10 + }) + + fmt.Println("totalKey:", totalKey) + fmt.Println("totalValue:", totalValue) + + // Output: + // totalKey: 10 + // totalValue: 20 +} + +func ExampleListMap_IteratorDesc() { + m := gmap.NewListMap() + for i := 0; i < 10; i++ { + m.Set(i, i*2) + } + + var totalKey, totalValue int + m.IteratorDesc(func(k interface{}, v interface{}) bool { + totalKey += k.(int) + totalValue += v.(int) + + return totalKey < 10 + }) + + fmt.Println("totalKey:", totalKey) + fmt.Println("totalValue:", totalValue) + + // Output: + // totalKey: 17 + // totalValue: 34 +} + +func ExampleListMap_Clone() { + m := gmap.NewListMap() + + m.Set("key1", "val1") + fmt.Println(m) + + n := m.Clone() + fmt.Println(n) + + // Output: + // {"key1":"val1"} + // {"key1":"val1"} +} + +func ExampleListMap_Clear() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + m.Clear() + + fmt.Println(m.Map()) + + // Output: + // map[] +} + +func ExampleListMap_Replace() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + }) + + var n gmap.ListMap + n.Sets(g.MapAnyAny{ + "k2": "v2", + }) + + fmt.Println(m.Map()) + + m.Replace(n.Map()) + fmt.Println(m.Map()) + + // Output: + // map[k1:v1] + // map[k2:v2] +} + +func ExampleListMap_Map() { + m1 := gmap.NewListMap() + m1.Set("key1", "val1") + fmt.Println("m1:", m1) + + n1 := m1.Map() + fmt.Println("before n1:", n1) + m1.Set("key1", "val2") + fmt.Println("after n1:", n1) + + // Output: + // m1: {"key1":"val1"} + // before n1: map[key1:val1] + // after n1: map[key1:val1] +} + +func ExampleListMap_MapStrAny() { + m := gmap.NewListMap() + m.Set("key1", "val1") + m.Set("key2", "val2") + + n := m.MapStrAny() + fmt.Println(n) + + // Output: + // map[key1:val1 key2:val2] +} + +func ExampleListMap_FilterEmpty() { + m := gmap.NewListMapFrom(g.MapAnyAny{ + "k1": "", + "k2": nil, + "k3": 0, + "k4": 1, + }) + m.FilterEmpty() + fmt.Println(m.Map()) + + // Output: + // map[k4:1] +} + +func ExampleListMap_Set() { + m := gmap.NewListMap() + + m.Set("key1", "val1") + fmt.Println(m) + + // Output: + // {"key1":"val1"} +} + +func ExampleListMap_Sets() { + m := gmap.NewListMap() + + addMap := make(map[interface{}]interface{}) + addMap["key1"] = "val1" + addMap["key2"] = "val2" + addMap["key3"] = "val3" + + m.Sets(addMap) + fmt.Println(m) + + // Output: + // {"key1":"val1","key2":"val2","key3":"val3"} +} + +func ExampleListMap_Search() { + m := gmap.NewListMap() + + m.Set("key1", "val1") + + value, found := m.Search("key1") + if found { + fmt.Println("find key1 value:", value) + } + + value, found = m.Search("key2") + if !found { + fmt.Println("key2 not find") + } + + // Output: + // find key1 value: val1 + // key2 not find +} + +func ExampleListMap_Get() { + m := gmap.NewListMap() + + m.Set("key1", "val1") + + fmt.Println("key1 value:", m.Get("key1")) + fmt.Println("key2 value:", m.Get("key2")) + + // Output: + // key1 value: val1 + // key2 value: +} + +func ExampleListMap_Pop() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Pop()) + + // May Output: + // k1 v1 +} + +func ExampleListMap_Pops() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Pops(-1)) + fmt.Println("size:", m.Size()) + + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Pops(2)) + fmt.Println("size:", m.Size()) + + // May Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] + // size: 0 + // map[k1:v1 k2:v2] + // size: 2 +} + +func ExampleListMap_GetOrSet() { + m := gmap.NewListMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSet("key1", "NotExistValue")) + fmt.Println(m.GetOrSet("key2", "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleListMap_GetOrSetFunc() { + m := gmap.NewListMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFunc("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFunc("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleListMap_GetOrSetFuncLock() { + m := gmap.NewListMap() + m.Set("key1", "val1") + + fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleListMap_GetVar() { + m := gmap.NewListMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVar("key1")) + fmt.Println(m.GetVar("key2").IsNil()) + + // Output: + // val1 + // true +} + +func ExampleListMap_GetVarOrSet() { + m := gmap.NewListMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSet("key1", "NotExistValue")) + fmt.Println(m.GetVarOrSet("key2", "val2")) + + // Output: + // val1 + // val2 +} + +func ExampleListMap_GetVarOrSetFunc() { + m := gmap.NewListMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleListMap_GetVarOrSetFuncLock() { + m := gmap.NewListMap() + m.Set("key1", "val1") + + fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} { + return "NotExistValue" + })) + fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} { + return "NotExistValue" + })) + + // Output: + // val1 + // NotExistValue +} + +func ExampleListMap_SetIfNotExist() { + var m gmap.ListMap + fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleListMap_SetIfNotExistFunc() { + var m gmap.ListMap + fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleListMap_SetIfNotExistFuncLock() { + var m gmap.ListMap + fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { + return "v1" + })) + fmt.Println(m.Map()) + + // Output: + // true + // false + // map[k1:v1] +} + +func ExampleListMap_Remove() { + var m gmap.ListMap + m.Set("k1", "v1") + + fmt.Println(m.Remove("k1")) + fmt.Println(m.Remove("k2")) + + // Output: + // v1 + // +} + +func ExampleListMap_Removes() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + removeList := make([]interface{}, 2) + removeList = append(removeList, "k1") + removeList = append(removeList, "k2") + + m.Removes(removeList) + + fmt.Println(m.Map()) + + // Output: + // map[k3:v3 k4:v4] +} + +func ExampleListMap_Keys() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Keys()) + + // May Output: + // [k1 k2 k3 k4] +} + +func ExampleListMap_Values() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Values()) + + // May Output: + // [v1 v2 v3 v4] +} + +func ExampleListMap_Contains() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Contains("k1")) + fmt.Println(m.Contains("k5")) + + // Output: + // true + // false +} + +func ExampleListMap_Size() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + fmt.Println(m.Size()) + + // Output: + // 4 +} + +func ExampleListMap_IsEmpty() { + var m gmap.ListMap + fmt.Println(m.IsEmpty()) + + m.Set("k1", "v1") + fmt.Println(m.IsEmpty()) + + // Output: + // true + // false +} + +func ExampleListMap_Flip() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + }) + m.Flip() + fmt.Println(m.Map()) + + // Output: + // map[v1:k1] +} + +func ExampleListMap_Merge() { + var m1, m2 gmap.ListMap + m1.Set("key1", "val1") + m2.Set("key2", "val2") + m1.Merge(&m2) + fmt.Println(m1.Map()) + + // May Output: + // map[key1:val1 key2:val2] +} + +func ExampleListMap_String() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + }) + + fmt.Println(m.String()) + + // Output: + // {"k1":"v1"} +} + +func ExampleListMap_MarshalJSON() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + bytes, err := m.MarshalJSON() + if err == nil { + fmt.Println(gconv.String(bytes)) + } + + // Output: + // {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} +} + +func ExampleListMap_UnmarshalJSON() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + var n gmap.ListMap + + err := n.UnmarshalJSON(gconv.Bytes(m.String())) + if err == nil { + fmt.Println(n.Map()) + } + + // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] +} + +func ExampleListMap_UnmarshalValue() { + var m gmap.ListMap + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + + var n gmap.ListMap + err := n.UnmarshalValue(m.String()) + if err == nil { + fmt.Println(n.Map()) + } + // Output: + // map[k1:v1 k2:v2 k3:v3 k4:v4] +} diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_test.go index 7336d898c..2a84c0e06 100644 --- a/container/gmap/gmap_z_example_test.go +++ b/container/gmap/gmap_z_example_test.go @@ -254,3 +254,29 @@ func ExampleNewStrStrMapFrom() { // {"key1":"var1"} // {"key1":"var1"} } + +func ExampleNewListMap() { + m := gmap.NewListMap() + + m.Set("key1", "var1") + m.Set("key2", "var2") + fmt.Println(m) + + // Output: + // {"key1":"var1","key2":"var2"} +} + +func ExampleNewListMapFrom() { + m := gmap.NewListMap() + + m.Set("key1", "var1") + m.Set("key2", "var2") + fmt.Println(m) + + n := gmap.NewListMapFrom(m.Map(), true) + fmt.Println(n) + + // Output: + // {"key1":"var1","key2":"var2"} + // {"key1":"var1","key2":"var2"} +} From b58c5311d0c4e087891b1c6b0f95b7bbac73e113 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sun, 21 Nov 2021 12:44:59 +0800 Subject: [PATCH 17/22] Implemented TreeMap Example --- container/gmap/gmap_z_example_test.go | 29 ++++++++++++++++++++++ container/gmap/gmap_z_example_tree_test.go | 1 - 2 files changed, 29 insertions(+), 1 deletion(-) delete mode 100644 container/gmap/gmap_z_example_tree_test.go diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_test.go index 2a84c0e06..4ef5fe3b4 100644 --- a/container/gmap/gmap_z_example_test.go +++ b/container/gmap/gmap_z_example_test.go @@ -8,6 +8,7 @@ package gmap_test import ( "fmt" + "github.com/gogf/gf/v2/util/gutil" "github.com/gogf/gf/v2/container/gmap" ) @@ -280,3 +281,31 @@ func ExampleNewListMapFrom() { // {"key1":"var1","key2":"var2"} // {"key1":"var1","key2":"var2"} } + +func ExampleNewTreeMap() { + m := gmap.NewTreeMap(gutil.ComparatorString) + + m.Set("key2", "var2") + m.Set("key1", "var1") + + fmt.Println(m.Map()) + + // Output: + // map[key1:var1 key2:var2] +} + +func ExampleNewTreeMapFrom() { + m := gmap.NewTreeMap(gutil.ComparatorString) + + m.Set("key2", "var2") + m.Set("key1", "var1") + + fmt.Println(m.Map()) + + n := gmap.NewListMapFrom(m.Map(), true) + fmt.Println(n.Map()) + + // Output: + // map[key1:var1 key2:var2] + // map[key1:var1 key2:var2] +} diff --git a/container/gmap/gmap_z_example_tree_test.go b/container/gmap/gmap_z_example_tree_test.go deleted file mode 100644 index 4d2e77928..000000000 --- a/container/gmap/gmap_z_example_tree_test.go +++ /dev/null @@ -1 +0,0 @@ -package gmap From a3dfebffb4f1a36078e2850b829e8b79a71ded38 Mon Sep 17 00:00:00 2001 From: huangqian Date: Mon, 22 Nov 2021 00:26:34 +0800 Subject: [PATCH 18/22] output modify --- container/gmap/gmap_z_example_any_any_test.go | 57 +++++++++++-------- container/gmap/gmap_z_example_int_any_test.go | 37 ++++++------ container/gmap/gmap_z_example_int_int_test.go | 33 ++++++----- container/gmap/gmap_z_example_list_test.go | 51 ++++++++++------- container/gmap/gmap_z_example_str_any_test.go | 39 +++++++------ container/gmap/gmap_z_example_str_int_test.go | 33 ++++++----- container/gmap/gmap_z_example_str_str_test.go | 33 ++++++----- 7 files changed, 160 insertions(+), 123 deletions(-) diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index 88c0e4e6e..0b96bfd7e 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -8,6 +8,7 @@ package gmap_test import ( "fmt" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/container/gmap" @@ -101,10 +102,10 @@ func ExampleAnyAnyMap_MapStrAny() { m.Set(1002, "val2") n := m.MapStrAny() - fmt.Println(n) + fmt.Printf("%#v", n) // Output: - // map[1001:val1 1002:val2] + // map[string]interface {}{"1001":"val1", "1002":"val2"} } func ExampleAnyAnyMap_FilterEmpty() { @@ -129,10 +130,10 @@ func ExampleAnyAnyMap_FilterNil() { "k4": 1, }) m.FilterNil() - fmt.Println(m.Map()) + fmt.Printf("%#v", m.Map()) - // May Output: - // map[k1: k3:0 k4:1] + // Output: + // map[interface {}]interface {}{"k1":"", "k3":0, "k4":1} } func ExampleAnyAnyMap_Set() { @@ -338,7 +339,7 @@ func ExampleAnyAnyMap_GetVarOrSetFuncLock() { func ExampleAnyAnyMap_SetIfNotExist() { var m gmap.Map fmt.Println(m.SetIfNotExist("k1", "v1")) - fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.SetIfNotExist("k1", "v2")) fmt.Println(m.Map()) // Output: @@ -353,7 +354,7 @@ func ExampleAnyAnyMap_SetIfNotExistFunc() { return "v1" })) fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -369,7 +370,7 @@ func ExampleAnyAnyMap_SetIfNotExistFuncLock() { return "v1" })) fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -385,10 +386,12 @@ func ExampleAnyAnyMap_Remove() { fmt.Println(m.Remove("k1")) fmt.Println(m.Remove("k2")) + fmt.Println(m.Size()) // Output: // v1 // + // 0 } func ExampleAnyAnyMap_Removes() { @@ -612,7 +615,7 @@ func ExampleAnyAnyMap_MarshalJSON() { "k4": "v4", }) - bytes, err := m.MarshalJSON() + bytes, err := json.Marshal(&m) if err == nil { fmt.Println(gconv.String(bytes)) } @@ -632,7 +635,7 @@ func ExampleAnyAnyMap_UnmarshalJSON() { var n gmap.Map - err := n.UnmarshalJSON(gconv.Bytes(m.String())) + err := json.Unmarshal(gconv.Bytes(m.String()), &n) if err == nil { fmt.Println(n.Map()) } @@ -642,19 +645,27 @@ func ExampleAnyAnyMap_UnmarshalJSON() { } func ExampleAnyAnyMap_UnmarshalValue() { - var m gmap.Map - m.Sets(g.MapAnyAny{ - "k1": "v1", - "k2": "v2", - "k3": "v3", - "k4": "v4", - }) - - var n gmap.Map - err := n.UnmarshalValue(m.String()) - if err == nil { - fmt.Println(n.Map()) + type User struct { + Uid int + Name string + Pass1 string `gconv:"password1"` + Pass2 string `gconv:"password2"` } + + var ( + user = new(User) + params = g.MapAnyAny{ + "uid": 1, + "name": "john", + "PASS1": "123", + "PASS2": "456", + } + ) + err := gconv.Scan(params, user) + if err == nil { + fmt.Printf("%#v", user) + } + // Output: - // map[k1:v1 k2:v2 k3:v3 k4:v4] + // &gmap_test.User{Uid:1, Name:"john", Pass1:"123", Pass2:"456"} } diff --git a/container/gmap/gmap_z_example_int_any_test.go b/container/gmap/gmap_z_example_int_any_test.go index 22cd4556a..a8721e064 100644 --- a/container/gmap/gmap_z_example_int_any_test.go +++ b/container/gmap/gmap_z_example_int_any_test.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/gogf/gf/v2/container/gmap" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/util/gconv" ) @@ -100,10 +101,10 @@ func ExampleIntAnyMap_MapStrAny() { m.Set(1002, "val2") n := m.MapStrAny() - fmt.Println(n) + fmt.Printf("%#v", n) // Output: - // map[1001:val1 1002:val2] + // map[string]interface {}{"1001":"val1", "1002":"val2"} } func ExampleIntAnyMap_FilterEmpty() { @@ -128,10 +129,10 @@ func ExampleIntAnyMap_FilterNil() { 4: 1, }) m.FilterNil() - fmt.Println(m.Map()) + fmt.Printf("%#v", m.Map()) // Output: - // map[1: 3:0 4:1] + // map[int]interface {}{1:"", 3:0, 4:1} } func ExampleIntAnyMap_Set() { @@ -337,7 +338,7 @@ func ExampleIntAnyMap_GetVarOrSetFuncLock() { func ExampleIntAnyMap_SetIfNotExist() { var m gmap.IntAnyMap fmt.Println(m.SetIfNotExist(1, "v1")) - fmt.Println(m.SetIfNotExist(1, "v1")) + fmt.Println(m.SetIfNotExist(1, "v2")) fmt.Println(m.Map()) // Output: @@ -352,7 +353,7 @@ func ExampleIntAnyMap_SetIfNotExistFunc() { return "v1" })) fmt.Println(m.SetIfNotExistFunc(1, func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -368,7 +369,7 @@ func ExampleIntAnyMap_SetIfNotExistFuncLock() { return "v1" })) fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -384,10 +385,12 @@ func ExampleIntAnyMap_Remove() { fmt.Println(m.Remove(1)) fmt.Println(m.Remove(2)) + fmt.Println(m.Size()) // Output: // v1 // + // 0 } func ExampleIntAnyMap_Removes() { @@ -611,7 +614,7 @@ func ExampleIntAnyMap_MarshalJSON() { 4: "v4", }) - bytes, err := m.MarshalJSON() + bytes, err := json.Marshal(&m) if err == nil { fmt.Println(gconv.String(bytes)) } @@ -631,7 +634,7 @@ func ExampleIntAnyMap_UnmarshalJSON() { var n gmap.Map - err := n.UnmarshalJSON(gconv.Bytes(m.String())) + err := json.Unmarshal(gconv.Bytes(m.String()), &n) if err == nil { fmt.Println(n.Map()) } @@ -643,17 +646,17 @@ func ExampleIntAnyMap_UnmarshalJSON() { func ExampleIntAnyMap_UnmarshalValue() { var m gmap.IntAnyMap m.Sets(g.MapIntAny{ - 1: "v1", - 2: "v2", - 3: "v3", - 4: "v4", + 1: "goframe", + 2: "gin", + 3: "echo", }) - var n gmap.Map - err := n.UnmarshalValue(m.String()) + var goweb map[int]interface{} + + err := gconv.Scan(m.String(), &goweb) if err == nil { - fmt.Println(n.Map()) + fmt.Printf("%#v", goweb) } // Output: - // map[1:v1 2:v2 3:v3 4:v4] + // map[int]interface {}{1:"goframe", 2:"gin", 3:"echo"} } diff --git a/container/gmap/gmap_z_example_int_int_test.go b/container/gmap/gmap_z_example_int_int_test.go index 12347b92c..efa381459 100644 --- a/container/gmap/gmap_z_example_int_int_test.go +++ b/container/gmap/gmap_z_example_int_int_test.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/gogf/gf/v2/container/gmap" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/util/gconv" ) @@ -100,10 +101,10 @@ func ExampleIntIntMap_MapStrAny() { m.Set(1002, 2) n := m.MapStrAny() - fmt.Println(n) + fmt.Printf("%#v", n) // Output: - // map[1001:1 1002:2] + // map[string]interface {}{"1001":1, "1002":2} } func ExampleIntIntMap_FilterEmpty() { @@ -265,7 +266,7 @@ func ExampleIntIntMap_GetOrSetFuncLock() { func ExampleIntIntMap_SetIfNotExist() { var m gmap.IntIntMap fmt.Println(m.SetIfNotExist(1, 1)) - fmt.Println(m.SetIfNotExist(1, 1)) + fmt.Println(m.SetIfNotExist(1, 2)) fmt.Println(m.Map()) // Output: @@ -280,7 +281,7 @@ func ExampleIntIntMap_SetIfNotExistFunc() { return 1 })) fmt.Println(m.SetIfNotExistFunc(1, func() int { - return 1 + return 2 })) fmt.Println(m.Map()) @@ -296,7 +297,7 @@ func ExampleIntIntMap_SetIfNotExistFuncLock() { return 1 })) fmt.Println(m.SetIfNotExistFuncLock(1, func() int { - return 1 + return 2 })) fmt.Println(m.Map()) @@ -312,10 +313,12 @@ func ExampleIntIntMap_Remove() { fmt.Println(m.Remove(1)) fmt.Println(m.Remove(2)) + fmt.Println(m.Size()) // Output: // 1 // 0 + // 0 } func ExampleIntIntMap_Removes() { @@ -539,7 +542,7 @@ func ExampleIntIntMap_MarshalJSON() { 4: 4, }) - bytes, err := m.MarshalJSON() + bytes, err := json.Marshal(&m) if err == nil { fmt.Println(gconv.String(bytes)) } @@ -559,7 +562,7 @@ func ExampleIntIntMap_UnmarshalJSON() { var n gmap.Map - err := n.UnmarshalJSON(gconv.Bytes(m.String())) + err := json.Unmarshal(gconv.Bytes(m.String()), &n) if err == nil { fmt.Println(n.Map()) } @@ -571,17 +574,17 @@ func ExampleIntIntMap_UnmarshalJSON() { func ExampleIntIntMap_UnmarshalValue() { var m gmap.IntIntMap m.Sets(g.MapIntInt{ - 1: 1, - 2: 2, - 3: 3, - 4: 4, + 1: 1001, + 2: 1002, + 3: 1003, }) - var n gmap.Map - err := n.UnmarshalValue(m.String()) + var n map[int]int + + err := gconv.Scan(m.String(), &n) if err == nil { - fmt.Println(n.Map()) + fmt.Printf("%#v", n) } // Output: - // map[1:1 2:2 3:3 4:4] + // map[int]int{1:1001, 2:1002, 3:1003} } diff --git a/container/gmap/gmap_z_example_list_test.go b/container/gmap/gmap_z_example_list_test.go index 0c9530198..0a89e4f56 100644 --- a/container/gmap/gmap_z_example_list_test.go +++ b/container/gmap/gmap_z_example_list_test.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/gogf/gf/v2/container/gmap" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/util/gconv" ) @@ -153,10 +154,10 @@ func ExampleListMap_MapStrAny() { m.Set("key2", "val2") n := m.MapStrAny() - fmt.Println(n) + fmt.Printf("%#v", n) // Output: - // map[key1:val1 key2:val2] + // map[string]interface {}{"key1":"val1", "key2":"val2"} } func ExampleListMap_FilterEmpty() { @@ -376,7 +377,7 @@ func ExampleListMap_GetVarOrSetFuncLock() { func ExampleListMap_SetIfNotExist() { var m gmap.ListMap fmt.Println(m.SetIfNotExist("k1", "v1")) - fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.SetIfNotExist("k1", "v2")) fmt.Println(m.Map()) // Output: @@ -391,7 +392,7 @@ func ExampleListMap_SetIfNotExistFunc() { return "v1" })) fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -407,7 +408,7 @@ func ExampleListMap_SetIfNotExistFuncLock() { return "v1" })) fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -423,10 +424,12 @@ func ExampleListMap_Remove() { fmt.Println(m.Remove("k1")) fmt.Println(m.Remove("k2")) + fmt.Println(m.Size()) // Output: // v1 // + // 0 } func ExampleListMap_Removes() { @@ -566,7 +569,7 @@ func ExampleListMap_MarshalJSON() { "k4": "v4", }) - bytes, err := m.MarshalJSON() + bytes, err := json.Marshal(&m) if err == nil { fmt.Println(gconv.String(bytes)) } @@ -586,7 +589,7 @@ func ExampleListMap_UnmarshalJSON() { var n gmap.ListMap - err := n.UnmarshalJSON(gconv.Bytes(m.String())) + err := json.Unmarshal(gconv.Bytes(m.String()), &n) if err == nil { fmt.Println(n.Map()) } @@ -596,19 +599,27 @@ func ExampleListMap_UnmarshalJSON() { } func ExampleListMap_UnmarshalValue() { - var m gmap.ListMap - m.Sets(g.MapAnyAny{ - "k1": "v1", - "k2": "v2", - "k3": "v3", - "k4": "v4", - }) - - var n gmap.ListMap - err := n.UnmarshalValue(m.String()) - if err == nil { - fmt.Println(n.Map()) + type User struct { + Uid int + Name string + Pass1 string `gconv:"password1"` + Pass2 string `gconv:"password2"` } + + var ( + user = new(User) + params = g.MapAnyAny{ + "uid": 1, + "name": "john", + "PASS1": "123", + "PASS2": "456", + } + ) + err := gconv.Scan(params, user) + if err == nil { + fmt.Printf("%#v", user) + } + // Output: - // map[k1:v1 k2:v2 k3:v3 k4:v4] + // &gmap_test.User{Uid:1, Name:"john", Pass1:"123", Pass2:"456"} } diff --git a/container/gmap/gmap_z_example_str_any_test.go b/container/gmap/gmap_z_example_str_any_test.go index 5842d4d3b..93f99a7e2 100644 --- a/container/gmap/gmap_z_example_str_any_test.go +++ b/container/gmap/gmap_z_example_str_any_test.go @@ -8,6 +8,7 @@ package gmap_test import ( "fmt" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/container/gmap" @@ -98,10 +99,10 @@ func ExampleStrAnyMap_MapStrAny() { m.Set("key2", "val2") n := m.MapStrAny() - fmt.Println(n) + fmt.Printf("%#v", n) // Output: - // map[key1:val1 key2:val2] + // map[string]interface {}{"key1":"val1", "key2":"val2"} } func ExampleStrAnyMap_FilterEmpty() { @@ -126,10 +127,10 @@ func ExampleStrAnyMap_FilterNil() { "k4": 1, }) m.FilterNil() - fmt.Println(m.Map()) + fmt.Printf("%#v", m.Map()) - // May Output: - // map[k1: k3:0 k4:1] + // Output: + // map[string]interface {}{"k1":"", "k3":0, "k4":1} } func ExampleStrAnyMap_Set() { @@ -335,7 +336,7 @@ func ExampleStrAnyMap_GetVarOrSetFuncLock() { func ExampleStrAnyMap_SetIfNotExist() { var m gmap.StrAnyMap fmt.Println(m.SetIfNotExist("k1", "v1")) - fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.SetIfNotExist("k1", "v2")) fmt.Println(m.Map()) // Output: @@ -350,7 +351,7 @@ func ExampleStrAnyMap_SetIfNotExistFunc() { return "v1" })) fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -366,7 +367,7 @@ func ExampleStrAnyMap_SetIfNotExistFuncLock() { return "v1" })) fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -382,10 +383,12 @@ func ExampleStrAnyMap_Remove() { fmt.Println(m.Remove("k1")) fmt.Println(m.Remove("k2")) + fmt.Println(m.Size()) // Output: // v1 // + // 0 } func ExampleStrAnyMap_Removes() { @@ -609,7 +612,7 @@ func ExampleStrAnyMap_MarshalJSON() { "k4": "v4", }) - bytes, err := m.MarshalJSON() + bytes, err := json.Marshal(&m) if err == nil { fmt.Println(gconv.String(bytes)) } @@ -629,7 +632,7 @@ func ExampleStrAnyMap_UnmarshalJSON() { var n gmap.StrAnyMap - err := n.UnmarshalJSON(gconv.Bytes(m.String())) + err := json.Unmarshal(gconv.Bytes(m.String()), &n) if err == nil { fmt.Println(n.Map()) } @@ -641,17 +644,17 @@ func ExampleStrAnyMap_UnmarshalJSON() { func ExampleStrAnyMap_UnmarshalValue() { var m gmap.StrAnyMap m.Sets(g.MapStrAny{ - "k1": "v1", - "k2": "v2", - "k3": "v3", - "k4": "v4", + "goframe": "https://goframe.org", + "gin": "https://gin-gonic.com/", + "echo": "https://echo.labstack.com/", }) - var n gmap.StrAnyMap - err := n.UnmarshalValue(m.String()) + var goweb map[string]interface{} + + err := gconv.Scan(m.String(), &goweb) if err == nil { - fmt.Println(n.Map()) + fmt.Printf("%#v", goweb) } // Output: - // map[k1:v1 k2:v2 k3:v3 k4:v4] + // map[string]interface {}{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"} } diff --git a/container/gmap/gmap_z_example_str_int_test.go b/container/gmap/gmap_z_example_str_int_test.go index fe8dd7408..d1a74caae 100644 --- a/container/gmap/gmap_z_example_str_int_test.go +++ b/container/gmap/gmap_z_example_str_int_test.go @@ -9,6 +9,7 @@ package gmap_test import ( "fmt" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/container/gmap" @@ -98,10 +99,10 @@ func ExampleStrIntMap_MapStrAny() { m.Set("key2", 2) n := m.MapStrAny() - fmt.Println(n) + fmt.Printf("%#v", n) // Output: - // map[key1:1 key2:2] + // map[string]interface {}{"key1":1, "key2":2} } func ExampleStrIntMap_FilterEmpty() { @@ -263,7 +264,7 @@ func ExampleStrIntMap_GetOrSetFuncLock() { func ExampleStrIntMap_SetIfNotExist() { var m gmap.StrIntMap fmt.Println(m.SetIfNotExist("k1", 1)) - fmt.Println(m.SetIfNotExist("k1", 1)) + fmt.Println(m.SetIfNotExist("k1", 2)) fmt.Println(m.Map()) // Output: @@ -278,7 +279,7 @@ func ExampleStrIntMap_SetIfNotExistFunc() { return 1 })) fmt.Println(m.SetIfNotExistFunc("k1", func() int { - return 1 + return 2 })) fmt.Println(m.Map()) @@ -294,7 +295,7 @@ func ExampleStrIntMap_SetIfNotExistFuncLock() { return 1 })) fmt.Println(m.SetIfNotExistFuncLock("k1", func() int { - return 1 + return 2 })) fmt.Println(m.Map()) @@ -310,10 +311,12 @@ func ExampleStrIntMap_Remove() { fmt.Println(m.Remove("k1")) fmt.Println(m.Remove("k2")) + fmt.Println(m.Size()) // Output: // 1 // 0 + // 0 } func ExampleStrIntMap_Removes() { @@ -545,7 +548,7 @@ func ExampleStrIntMap_MarshalJSON() { "k4": 4, }) - bytes, err := m.MarshalJSON() + bytes, err := json.Marshal(&m) if err == nil { fmt.Println(gconv.String(bytes)) } @@ -565,7 +568,7 @@ func ExampleStrIntMap_UnmarshalJSON() { var n gmap.StrIntMap - err := n.UnmarshalJSON(gconv.Bytes(m.String())) + err := json.Unmarshal(gconv.Bytes(m.String()), &n) if err == nil { fmt.Println(n.Map()) } @@ -577,17 +580,17 @@ func ExampleStrIntMap_UnmarshalJSON() { func ExampleStrIntMap_UnmarshalValue() { var m gmap.StrIntMap m.Sets(g.MapStrInt{ - "k1": 1, - "k2": 2, - "k3": 3, - "k4": 4, + "goframe": 1, + "gin": 2, + "echo": 3, }) - var n gmap.StrIntMap - err := n.UnmarshalValue(m.String()) + var goweb map[string]int + + err := gconv.Scan(m.String(), &goweb) if err == nil { - fmt.Println(n.Map()) + fmt.Printf("%#v", goweb) } // Output: - // map[k1:1 k2:2 k3:3 k4:4] + // map[string]int{"echo":3, "gin":2, "goframe":1} } diff --git a/container/gmap/gmap_z_example_str_str_test.go b/container/gmap/gmap_z_example_str_str_test.go index 093f70793..c63d346db 100644 --- a/container/gmap/gmap_z_example_str_str_test.go +++ b/container/gmap/gmap_z_example_str_str_test.go @@ -8,6 +8,7 @@ package gmap_test import ( "fmt" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" @@ -100,10 +101,10 @@ func ExampleStrStrMap_MapStrAny() { m.Set("key2", "val2") n := m.MapStrAny() - fmt.Println(n) + fmt.Printf("%#v", n) // Output: - // map[key1:val1 key2:val2] + // map[string]interface {}{"key1":"val1", "key2":"val2"} } func ExampleStrStrMap_FilterEmpty() { @@ -265,7 +266,7 @@ func ExampleStrStrMap_GetOrSetFuncLock() { func ExampleStrStrMap_SetIfNotExist() { var m gmap.StrStrMap fmt.Println(m.SetIfNotExist("k1", "v1")) - fmt.Println(m.SetIfNotExist("k1", "v1")) + fmt.Println(m.SetIfNotExist("k1", "v2")) fmt.Println(m.Map()) // Output: @@ -280,7 +281,7 @@ func ExampleStrStrMap_SetIfNotExistFunc() { return "v1" })) fmt.Println(m.SetIfNotExistFunc("k1", func() string { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -296,7 +297,7 @@ func ExampleStrStrMap_SetIfNotExistFuncLock() { return "v1" })) fmt.Println(m.SetIfNotExistFuncLock("k1", func() string { - return "v1" + return "v2" })) fmt.Println(m.Map()) @@ -312,10 +313,12 @@ func ExampleStrStrMap_Remove() { fmt.Println(m.Remove("k1")) fmt.Println(len(m.Remove("k2"))) + fmt.Println(m.Size()) // Output: // v1 // 0 + // 0 } func ExampleStrStrMap_Removes() { @@ -541,7 +544,7 @@ func ExampleStrStrMap_MarshalJSON() { "k4": "v4", }) - bytes, err := m.MarshalJSON() + bytes, err := json.Marshal(&m) if err == nil { fmt.Println(gconv.String(bytes)) } @@ -561,7 +564,7 @@ func ExampleStrStrMap_UnmarshalJSON() { var n gmap.StrStrMap - err := n.UnmarshalJSON(gconv.Bytes(m.String())) + err := json.Unmarshal(gconv.Bytes(m.String()), &n) if err == nil { fmt.Println(n.Map()) } @@ -573,17 +576,17 @@ func ExampleStrStrMap_UnmarshalJSON() { func ExampleStrStrMap_UnmarshalValue() { var m gmap.StrStrMap m.Sets(g.MapStrStr{ - "k1": "v1", - "k2": "v2", - "k3": "v3", - "k4": "v4", + "goframe": "https://goframe.org", + "gin": "https://gin-gonic.com/", + "echo": "https://echo.labstack.com/", }) - var n gmap.StrStrMap - err := n.UnmarshalValue(m.String()) + var goweb map[string]string + + err := gconv.Scan(m.String(), &goweb) if err == nil { - fmt.Println(n.Map()) + fmt.Printf("%#v", goweb) } // Output: - // map[k1:v1 k2:v2 k3:v3 k4:v4] + // map[string]string{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"} } From c94513f4aabb24a7de9bf0955857d0439a8edd5d Mon Sep 17 00:00:00 2001 From: huangqian Date: Mon, 22 Nov 2021 08:59:21 +0800 Subject: [PATCH 19/22] add "bail" rule Example --- util/gvalid/gvalid_z_example_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/util/gvalid/gvalid_z_example_test.go b/util/gvalid/gvalid_z_example_test.go index 3f04cab1b..8b284b7a8 100644 --- a/util/gvalid/gvalid_z_example_test.go +++ b/util/gvalid/gvalid_z_example_test.go @@ -442,6 +442,30 @@ func ExampleValidator_RequiredWithoutAll() { // The HusbandName field is required } +func ExampleValidator_Bail() { + type BizReq struct { + Account string `v:"bail|required|length:6,16|same:QQ"` + QQ string + Password string `v:"required|same:Password2"` + Password2 string `v:"required"` + } + var ( + ctx = context.Background() + req = BizReq{ + Account: "gf", + QQ: "123456", + Password: "goframe.org", + Password2: "goframe.org", + } + ) + if err := g.Validator().CheckStruct(ctx, req); err != nil { + fmt.Println(err) + } + + // output: + // The Account value `gf` length must be between 6 and 16 +} + func ExampleValidator_Date() { type BizReq struct { Date1 string `v:"date"` From 2728ab9e42d1e8e59d287fbf5cd5365266484052 Mon Sep 17 00:00:00 2001 From: huangqian Date: Mon, 22 Nov 2021 21:04:24 +0800 Subject: [PATCH 20/22] add "ci" rule Example --- util/gvalid/gvalid_z_example_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/util/gvalid/gvalid_z_example_test.go b/util/gvalid/gvalid_z_example_test.go index 8b284b7a8..efa6f90fb 100644 --- a/util/gvalid/gvalid_z_example_test.go +++ b/util/gvalid/gvalid_z_example_test.go @@ -466,6 +466,27 @@ func ExampleValidator_Bail() { // The Account value `gf` length must be between 6 and 16 } +func ExampleValidator_CaseInsensitive() { + type BizReq struct { + Account string `v:"required"` + Password string `v:"required|ci|same:Password2"` + Password2 string `v:"required"` + } + var ( + ctx = context.Background() + req = BizReq{ + Account: "gf", + Password: "Goframe.org", // Diff from Password2, but because of "ci", rule check passed + Password2: "goframe.org", + } + ) + if err := g.Validator().CheckStruct(ctx, req); err != nil { + fmt.Println(err) + } + + // output: +} + func ExampleValidator_Date() { type BizReq struct { Date1 string `v:"date"` From 7e3d7b35756a89581e261520e22b3d12c1521bc8 Mon Sep 17 00:00:00 2001 From: huangqian Date: Mon, 22 Nov 2021 23:11:40 +0800 Subject: [PATCH 21/22] modify UnmarshalValue Sample --- container/gmap/gmap_z_example_any_any_test.go | 19 +++++++++---------- container/gmap/gmap_z_example_int_any_test.go | 15 +++++++-------- container/gmap/gmap_z_example_int_int_test.go | 12 +++++------- container/gmap/gmap_z_example_list_test.go | 19 +++++++++---------- container/gmap/gmap_z_example_str_any_test.go | 12 +++++------- container/gmap/gmap_z_example_str_int_test.go | 12 +++++------- container/gmap/gmap_z_example_str_str_test.go | 12 +++++------- 7 files changed, 45 insertions(+), 56 deletions(-) diff --git a/container/gmap/gmap_z_example_any_any_test.go b/container/gmap/gmap_z_example_any_any_test.go index 0b96bfd7e..6908978b7 100644 --- a/container/gmap/gmap_z_example_any_any_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -653,19 +653,18 @@ func ExampleAnyAnyMap_UnmarshalValue() { } var ( - user = new(User) - params = g.MapAnyAny{ - "uid": 1, - "name": "john", - "PASS1": "123", - "PASS2": "456", + m gmap.AnyAnyMap + user = User{ + Uid: 1, + Name: "john", + Pass1: "123", + Pass2: "456", } ) - err := gconv.Scan(params, user) - if err == nil { - fmt.Printf("%#v", user) + if err := gconv.Scan(user, &m); err == nil { + fmt.Printf("%#v", m.Map()) } // Output: - // &gmap_test.User{Uid:1, Name:"john", Pass1:"123", Pass2:"456"} + // map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"} } diff --git a/container/gmap/gmap_z_example_int_any_test.go b/container/gmap/gmap_z_example_int_any_test.go index a8721e064..a89b2d1db 100644 --- a/container/gmap/gmap_z_example_int_any_test.go +++ b/container/gmap/gmap_z_example_int_any_test.go @@ -645,18 +645,17 @@ func ExampleIntAnyMap_UnmarshalJSON() { func ExampleIntAnyMap_UnmarshalValue() { var m gmap.IntAnyMap - m.Sets(g.MapIntAny{ + + goWeb := map[int]interface{}{ 1: "goframe", 2: "gin", 3: "echo", - }) - - var goweb map[int]interface{} - - err := gconv.Scan(m.String(), &goweb) - if err == nil { - fmt.Printf("%#v", goweb) } + + if err := gconv.Scan(goWeb, &m); err == nil { + fmt.Printf("%#v", m.Map()) + } + // Output: // map[int]interface {}{1:"goframe", 2:"gin", 3:"echo"} } diff --git a/container/gmap/gmap_z_example_int_int_test.go b/container/gmap/gmap_z_example_int_int_test.go index efa381459..fb8f36f91 100644 --- a/container/gmap/gmap_z_example_int_int_test.go +++ b/container/gmap/gmap_z_example_int_int_test.go @@ -573,17 +573,15 @@ func ExampleIntIntMap_UnmarshalJSON() { func ExampleIntIntMap_UnmarshalValue() { var m gmap.IntIntMap - m.Sets(g.MapIntInt{ + + n := map[int]int{ 1: 1001, 2: 1002, 3: 1003, - }) + } - var n map[int]int - - err := gconv.Scan(m.String(), &n) - if err == nil { - fmt.Printf("%#v", n) + if err := gconv.Scan(n, &m); err == nil { + fmt.Printf("%#v", m.Map()) } // Output: // map[int]int{1:1001, 2:1002, 3:1003} diff --git a/container/gmap/gmap_z_example_list_test.go b/container/gmap/gmap_z_example_list_test.go index 0a89e4f56..0f6645d50 100644 --- a/container/gmap/gmap_z_example_list_test.go +++ b/container/gmap/gmap_z_example_list_test.go @@ -607,19 +607,18 @@ func ExampleListMap_UnmarshalValue() { } var ( - user = new(User) - params = g.MapAnyAny{ - "uid": 1, - "name": "john", - "PASS1": "123", - "PASS2": "456", + m gmap.AnyAnyMap + user = User{ + Uid: 1, + Name: "john", + Pass1: "123", + Pass2: "456", } ) - err := gconv.Scan(params, user) - if err == nil { - fmt.Printf("%#v", user) + if err := gconv.Scan(user, &m); err == nil { + fmt.Printf("%#v", m.Map()) } // Output: - // &gmap_test.User{Uid:1, Name:"john", Pass1:"123", Pass2:"456"} + // map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"} } diff --git a/container/gmap/gmap_z_example_str_any_test.go b/container/gmap/gmap_z_example_str_any_test.go index 93f99a7e2..bc34545a8 100644 --- a/container/gmap/gmap_z_example_str_any_test.go +++ b/container/gmap/gmap_z_example_str_any_test.go @@ -643,17 +643,15 @@ func ExampleStrAnyMap_UnmarshalJSON() { func ExampleStrAnyMap_UnmarshalValue() { var m gmap.StrAnyMap - m.Sets(g.MapStrAny{ + + goWeb := map[string]interface{}{ "goframe": "https://goframe.org", "gin": "https://gin-gonic.com/", "echo": "https://echo.labstack.com/", - }) + } - var goweb map[string]interface{} - - err := gconv.Scan(m.String(), &goweb) - if err == nil { - fmt.Printf("%#v", goweb) + if err := gconv.Scan(goWeb, &m); err == nil { + fmt.Printf("%#v", m.Map()) } // Output: // map[string]interface {}{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"} diff --git a/container/gmap/gmap_z_example_str_int_test.go b/container/gmap/gmap_z_example_str_int_test.go index d1a74caae..bdd2f5eac 100644 --- a/container/gmap/gmap_z_example_str_int_test.go +++ b/container/gmap/gmap_z_example_str_int_test.go @@ -579,17 +579,15 @@ func ExampleStrIntMap_UnmarshalJSON() { func ExampleStrIntMap_UnmarshalValue() { var m gmap.StrIntMap - m.Sets(g.MapStrInt{ + + goWeb := map[string]int{ "goframe": 1, "gin": 2, "echo": 3, - }) + } - var goweb map[string]int - - err := gconv.Scan(m.String(), &goweb) - if err == nil { - fmt.Printf("%#v", goweb) + if err := gconv.Scan(goWeb, &m); err == nil { + fmt.Printf("%#v", m.Map()) } // Output: // map[string]int{"echo":3, "gin":2, "goframe":1} diff --git a/container/gmap/gmap_z_example_str_str_test.go b/container/gmap/gmap_z_example_str_str_test.go index c63d346db..9ac5bee6d 100644 --- a/container/gmap/gmap_z_example_str_str_test.go +++ b/container/gmap/gmap_z_example_str_str_test.go @@ -575,17 +575,15 @@ func ExampleStrStrMap_UnmarshalJSON() { func ExampleStrStrMap_UnmarshalValue() { var m gmap.StrStrMap - m.Sets(g.MapStrStr{ + + goWeb := map[string]string{ "goframe": "https://goframe.org", "gin": "https://gin-gonic.com/", "echo": "https://echo.labstack.com/", - }) + } - var goweb map[string]string - - err := gconv.Scan(m.String(), &goweb) - if err == nil { - fmt.Printf("%#v", goweb) + if err := gconv.Scan(goWeb, &m); err == nil { + fmt.Printf("%#v", m.Map()) } // Output: // map[string]string{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"} From a237495b6615a8b81936028980de7fd3c976d3f0 Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 23 Nov 2021 07:44:35 +0800 Subject: [PATCH 22/22] modify CheckStruct to Run --- util/gvalid/gvalid_z_example_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/gvalid/gvalid_z_example_test.go b/util/gvalid/gvalid_z_example_test.go index f85321eaf..1699318f6 100644 --- a/util/gvalid/gvalid_z_example_test.go +++ b/util/gvalid/gvalid_z_example_test.go @@ -458,7 +458,7 @@ func ExampleValidator_Bail() { Password2: "goframe.org", } ) - if err := g.Validator().CheckStruct(ctx, req); err != nil { + if err := g.Validator().Data(req).Run(ctx); err != nil { fmt.Println(err) } @@ -480,7 +480,7 @@ func ExampleValidator_CaseInsensitive() { Password2: "goframe.org", } ) - if err := g.Validator().CheckStruct(ctx, req); err != nil { + if err := g.Validator().Data(req).Run(ctx); err != nil { fmt.Println(err) }