Skip to content

Commit

Permalink
doc: update doc for v2.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed May 14, 2024
1 parent 967e6a3 commit 6e04985
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 131 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,15 @@ import "github.com/duke-git/lancet/v2/maputil"
- **<big>HasKey</big>** : checks if map has key or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#HasKey)]
[[play](https://go.dev/play/p/isZZHOsDhFc)]
- **<big>MapToStruct</big>** : converts map to struct.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#MapToStruct)]
[[play](https://go.dev/play/p/7wYyVfX38Dp)]
- **<big>ToSortedSlicesDefault</big>** : converts a map to two slices sorted by key: one for the keys and another for the values.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToSortedSlicesDefault)]
[[play](https://go.dev/play/p/43gEM2po-qy)]
- **<big>ToSortedSlicesWithComparator</big>** : converts a map to two slices sorted by key and using a custom comparison function: one for the keys and another for the values.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToSortedSlicesWithComparator)]
[[play](https://go.dev/play/p/0nlPo6YLdt3)]
- **<big>NewConcurrentMap</big>** : creates a ConcurrentMap with specific shard count.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#NewConcurrentMap)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
Expand Down Expand Up @@ -1440,9 +1445,10 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Break)]
- **<big>RightPadding</big>** : adds padding to the right end of a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#RightPadding)]
[[play](https://go.dev/play/p/0_2rlLEMBXL)]
- **<big>LeftPadding</big>** : adds padding to the left begin of a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#LeftPadding)]

[[play](https://go.dev/play/p/jlQVoelLl2k)]


<h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
Expand Down
8 changes: 8 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,15 @@ import "github.com/duke-git/lancet/v2/maputil"
- **<big>HasKey</big>** : 检查 map 是否包含某个 key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#HasKey)]
[[play](https://go.dev/play/p/isZZHOsDhFc)]
- **<big>MapToStruct</big>** : 将map转成struct。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#MapToStruct)]
[[play](https://go.dev/play/p/7wYyVfX38Dp)]
- **<big>ToSortedSlicesDefault</big>** : 将map的key和value转化成两个根据key的值从小到大排序的切片,value切片中元素的位置与key对应。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToSortedSlicesDefault)]
[[play](https://go.dev/play/p/43gEM2po-qy)]
- **<big>ToSortedSlicesWithComparator</big>** : 将map的key和value转化成两个使用比较器函数根据key的值自定义排序规则的切片,value切片中元素的位置与key对应。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToSortedSlicesWithComparator)]
[[play](https://go.dev/play/p/0nlPo6YLdt3)]
- **<big>NewConcurrentMap</big>** : ConcurrentMap 协程安全的 map 结构。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#NewConcurrentMap)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
Expand Down Expand Up @@ -1439,8 +1444,11 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Break)]
- **<big>RightPadding</big>** : 在切片的右部添加元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#RightPadding)]
[[play](https://go.dev/play/p/0_2rlLEMBXL)]
- **<big>LeftPadding</big>** : 在切片的左部添加元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#LeftPadding)]
[[play](https://go.dev/play/p/jlQVoelLl2k)]



<h3 id="stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
Expand Down
151 changes: 93 additions & 58 deletions docs/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -948,14 +948,6 @@ func main() {

<p>检查map是否包含某个key。用于代替以下样板代码:</p>

```go
_, haskey := amap["baz"];

if haskey {
fmt.Println("map has key baz")
}
```

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

```go
Expand Down Expand Up @@ -990,6 +982,49 @@ func main() {
}
```

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

<p>将map转成struct。</p>

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

```go
func MapToStruct(m map[string]any, structObj any) error
```

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

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)

func main() {
personReqMap := map[string]any{
"name": "Nothin",
"max_age": 35,
"page": 1,
"pageSize": 10,
}

type PersonReq struct {
Name string `json:"name"`
MaxAge int `json:"max_age"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
var personReq PersonReq
_ = maputil.MapToStruct(personReqMap, &personReq)
fmt.Println(personReq)

// Output:
// {Nothin 35 1 10}
}
```

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

<p>将map的key和value转化成两个根据key的值从小到大排序的切片,value切片中元素的位置与key对应。</p>
Expand All @@ -1000,7 +1035,7 @@ func main() {
func ToSortedSlicesDefault[K constraints.Ordered, V any](m map[K]V) ([]K, []V)
```

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

```go
package main
Expand All @@ -1012,19 +1047,19 @@ import (

func main() {
m := map[int]string{
1: "a",
3: "c",
2: "b",
}
1: "a",
3: "c",
2: "b",
}

keys, values := ToSortedSlicesDefault(m)
keys, values := ToSortedSlicesDefault(m)

fmt.Println(keys)
fmt.Println(values)
fmt.Println(keys)
fmt.Println(values)

// Output:
// [1 2 3]
// [a b c]
// Output:
// [1 2 3]
// [a b c]
}
```

Expand All @@ -1038,7 +1073,7 @@ func main() {
func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator func(a, b K) bool) ([]K, []V)
```

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

```go
package main
Expand All @@ -1050,35 +1085,35 @@ import (

func main() {
m1 := map[time.Time]string{
time.Date(2024, 3, 31, 0, 0, 0, 0, time.UTC): "today",
time.Date(2024, 3, 30, 0, 0, 0, 0, time.UTC): "yesterday",
time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC): "tomorrow",
}

keys1, values1 := ToSortedSlicesWithComparator(m1, func(a, b time.Time) bool {
return a.Before(b)
})

m2 := map[int]string{
1: "a",
3: "c",
2: "b",
}
keys2, values2 := ToSortedSlicesWithComparator(m2, func(a, b int) bool {
return a > b
})

fmt.Println(keys2)
fmt.Println(values2)

fmt.Println(keys1)
fmt.Println(values1)

// Output:
// [2024-03-30 00:00:00 +0000 UTC 2024-03-31 00:00:00 +0000 UTC 2024-04-01 00:00:00 +0000 UTC]
// [yesterday today tomorrow]
// [3 2 1]
// [c b a]
time.Date(2024, 3, 31, 0, 0, 0, 0, time.UTC): "today",
time.Date(2024, 3, 30, 0, 0, 0, 0, time.UTC): "yesterday",
time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC): "tomorrow",
}

keys1, values1 := maputil.ToSortedSlicesWithComparator(m1, func(a, b time.Time) bool {
return a.Before(b)
})

m2 := map[int]string{
1: "a",
3: "c",
2: "b",
}
keys2, values2 := maputil.ToSortedSlicesWithComparator(m2, func(a, b int) bool {
return a > b
})

fmt.Println(keys2)
fmt.Println(values2)

fmt.Println(keys1)
fmt.Println(values1)

// Output:
// [3 2 1]
// [c b a]
// [2024-03-30 00:00:00 +0000 UTC 2024-03-31 00:00:00 +0000 UTC 2024-04-01 00:00:00 +0000 UTC]
// [yesterday today tomorrow]
}
```

Expand Down Expand Up @@ -1144,15 +1179,15 @@ func main() {
wg1.Wait()

var wg2 sync.WaitGroup
wg2.Add(5)
wg2.Add(5)
for j := 0; j < 5; j++ {
go func(n int) {
val, ok := cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok)
wg2.Done()
wg2.Done()
}(j)
}
wg2.Wait()
wg2.Wait()

// output: (order may change)
// 1 true
Expand Down Expand Up @@ -1198,15 +1233,15 @@ func main() {
wg1.Wait()

var wg2 sync.WaitGroup
wg2.Add(5)
wg2.Add(5)
for j := 0; j < 5; j++ {
go func(n int) {
val, ok := cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok)
wg2.Done()
wg2.Done()
}(j)
}
wg2.Wait()
wg2.Wait()

// output: (order may change)
// 1 true
Expand Down Expand Up @@ -1296,7 +1331,7 @@ func main() {
wg1.Wait()

var wg2 sync.WaitGroup
wg2.Add(5)
wg2.Add(5)
for j := 0; j < 5; j++ {
go func(n int) {
cm.Delete(fmt.Sprintf("%d", n))
Expand Down Expand Up @@ -1342,7 +1377,7 @@ func main() {
wg1.Wait()

var wg2 sync.WaitGroup
wg2.Add(5)
wg2.Add(5)
for j := 0; j < 5; j++ {
go func(n int) {
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
Expand Down Expand Up @@ -1392,7 +1427,7 @@ func main() {
wg1.Wait()

var wg2 sync.WaitGroup
wg2.Add(5)
wg2.Add(5)

for j := 0; j < 5; j++ {
go func(n int) {
Expand Down
6 changes: 3 additions & 3 deletions docs/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -2615,7 +2615,7 @@ func main() {
func Break[T any](values []T, predicate func(T) bool) ([]T, []T)
```

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

```go
import (
Expand Down Expand Up @@ -2648,7 +2648,7 @@ func main() {
func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T
```

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

```go
import (
Expand All @@ -2675,7 +2675,7 @@ func main() {
func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T
```

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

```go
import (
Expand Down
Loading

0 comments on commit 6e04985

Please sign in to comment.