From e2a934294a277d27680f3d51e901908967a11b2a Mon Sep 17 00:00:00 2001 From: yunxuanhao Date: Tue, 10 Sep 2024 10:28:05 +0800 Subject: [PATCH 1/2] add maputil.GetValue --- docs/api/packages/maputil.md | 41 +++++++++++++++++++++++++++++++++ docs/en/api/packages/maputil.md | 41 +++++++++++++++++++++++++++++++++ maputil/map.go | 9 ++++++++ maputil/map_test.go | 19 +++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index c85cb40f..f4066832 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -75,6 +75,7 @@ import ( - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) - [SortByKey](#SortByKey) +- [GetValues](#GetValues)
@@ -2261,4 +2262,44 @@ func main() { // Output: // map[1:a 2:b 3:c 4:d] } +``` + +### GetValue + +

返回给定键的值,如果键不存在,则返回默认值。

+ +函数签名: + +```go +func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V +``` + +示例:[运行](https://go.dev/play/p/dNo6YBqtLa0) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + + result1 := maputil.GetValue(m, 1, "default") + result2 := maputil.GetValue(m, 6, "default") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // a + // default +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index 243e55ae..66348b8d 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -74,6 +74,7 @@ import ( - [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) +- [GetValue](#GetValue)
@@ -2276,4 +2277,44 @@ func main() { // Output: // map[1:a 2:b 3:c 4:d] } +``` + +### GetValue + +

returns the value of the given key or a default value if the key is not present.

+ +Signature: + +```go +func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V +``` + +Example:[运行](https://go.dev/play/p/dNo6YBqtLa0) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + + result1 := maputil.GetValue(m, 1, "default") + result2 := maputil.GetValue(m, 6, "default") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // a + // default +} ``` \ No newline at end of file diff --git a/maputil/map.go b/maputil/map.go index e60222e5..be7b526b 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -473,3 +473,12 @@ func SortByKey[K constraints.Ordered, V any](m map[K]V, less func(a, b K) bool) return } + +// GetValue returns the value of the given key or a default value if the key is not present. +// Play: https://go.dev/play/p/dNo6YBqtLa0 +func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V { + if v, ok := m[key]; ok { + return v + } + return defaultValue +} diff --git a/maputil/map_test.go b/maputil/map_test.go index 90b33bb5..aa3344ed 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -751,3 +751,22 @@ func TestSortByKey(t *testing.T) { assert.Equal(expected2, result2) } + +func TestGetValue(t *testing.T) { + + t.Parallel() + + assert := internal.NewAssert(t, "GetValue") + + m1 := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + result1 := GetValue(m1, 1, "123") + assert.Equal("a", result1) + + result2 := GetValue(m1, 5, "123") + assert.Equal("123", result2) +} From 73d2a5c9970044fb09f4fcf799cb8363c6c43355 Mon Sep 17 00:00:00 2001 From: yunxuanhao Date: Tue, 10 Sep 2024 14:34:18 +0800 Subject: [PATCH 2/2] rename GetOrDefault --- docs/api/packages/maputil.md | 12 ++++++------ docs/en/api/packages/maputil.md | 12 ++++++------ maputil/map.go | 7 +++---- maputil/map_test.go | 10 +++++----- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index 97a79cd6..b4bc9f28 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -77,7 +77,7 @@ import ( - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) - [SortByKey](#SortByKey) -- [GetValues](#GetValues) +- [GetOrDefault](#GetOrDefault)
@@ -2266,17 +2266,17 @@ func main() { } ``` -### GetValue +### GetOrDefault

返回给定键的值,如果键不存在,则返回默认值。

函数签名: ```go -func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V +func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V ``` -示例:[运行](https://go.dev/play/p/dNo6YBqtLa0) +示例:[运行](https://go.dev/play/p/99QjSYSBdiM) ```go package main @@ -2294,8 +2294,8 @@ func main() { 2: "b", } - result1 := maputil.GetValue(m, 1, "default") - result2 := maputil.GetValue(m, 6, "default") + result1 := maputil.GetOrDefault(m, 1, "default") + result2 := maputil.GetOrDefault(m, 6, "default") fmt.Println(result1) fmt.Println(result2) diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index 089dda50..6c84405c 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -77,7 +77,7 @@ import ( - [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) -- [GetValue](#GetValue) +- [GetOrDefault](#GetOrDefault)
@@ -2282,17 +2282,17 @@ func main() { } ``` -### GetValue +### GetOrDefault

returns the value of the given key or a default value if the key is not present.

Signature: ```go -func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V +func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V ``` -Example:[运行](https://go.dev/play/p/dNo6YBqtLa0) +Example:[运行](https://go.dev/play/p/99QjSYSBdiM) ```go package main @@ -2310,8 +2310,8 @@ func main() { 2: "b", } - result1 := maputil.GetValue(m, 1, "default") - result2 := maputil.GetValue(m, 6, "default") + result1 := maputil.GetOrDefault(m, 1, "default") + result2 := maputil.GetOrDefault(m, 6, "default") fmt.Println(result1) fmt.Println(result2) diff --git a/maputil/map.go b/maputil/map.go index d24d80ae..4f1fbf5b 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -658,10 +658,9 @@ func convertMap(src reflect.Value, dst reflect.Value) error { return nil } - -// GetValue returns the value of the given key or a default value if the key is not present. -// Play: https://go.dev/play/p/dNo6YBqtLa0 -func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V { +// GetOrDefault returns the value of the given key or a default value if the key is not present. +// Play: https://go.dev/play/p/99QjSYSBdiM +func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V { if v, ok := m[key]; ok { return v } diff --git a/maputil/map_test.go b/maputil/map_test.go index 2f211953..4a6c5f21 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -870,11 +870,11 @@ func TestBaseType(t *testing.T) { assert.EqualValues(64, number) } -func TestGetValue(t *testing.T) { +func TestGetOrDefault(t *testing.T) { t.Parallel() - assert := internal.NewAssert(t, "GetValue") + assert := internal.NewAssert(t, "GetOrDefault") m1 := map[int]string{ 3: "c", @@ -882,9 +882,9 @@ func TestGetValue(t *testing.T) { 4: "d", 2: "b", } - result1 := GetValue(m1, 1, "123") + result1 := GetOrDefault(m1, 1, "123") assert.Equal("a", result1) - result2 := GetValue(m1, 5, "123") + result2 := GetOrDefault(m1, 5, "123") assert.Equal("123", result2) -} \ No newline at end of file +}