Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(maputil): add GetValue #243

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import (
- [ConcurrentMap_Range](#ConcurrentMap_Range)
- [GetOrSet](#GetOrSet)
- [SortByKey](#SortByKey)
- [GetValues](#GetValues)


<div STYLE="page-break-after: always;"></div>
Expand Down Expand Up @@ -2263,4 +2264,44 @@ func main() {
// Output:
// map[1:a 2:b 3:c 4:d]
}
```

### <span id="GetValue">GetValue</span>

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

<b>函数签名:</b>

```go
func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V
```

<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/dNo6YBqtLa0)</span></b>

```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
}
```
41 changes: 41 additions & 0 deletions docs/en/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import (
- [ConcurrentMap_Has](#ConcurrentMap_Has)
- [ConcurrentMap_Range](#ConcurrentMap_Range)
- [GetOrSet](#GetOrSet)
- [GetValue](#GetValue)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -2279,4 +2280,44 @@ func main() {
// Output:
// map[1:a 2:b 3:c 4:d]
}
```

### <span id="GetValue">GetValue</span>

<p>returns the value of the given key or a default value if the key is not present.</p>

<b>Signature:</b>

```go
func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V
```

<b>Example:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/dNo6YBqtLa0)</span></b>

```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
}
```
10 changes: 10 additions & 0 deletions maputil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,13 @@ 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 {
if v, ok := m[key]; ok {
return v
}
return defaultValue
}
19 changes: 19 additions & 0 deletions maputil/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,22 @@ func TestBaseType(t *testing.T) {
MapTo(tc["u64"], &number)
assert.EqualValues(64, number)
}

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)
}
Loading