Skip to content

Commit

Permalink
doc: add play ground demo
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Jul 18, 2024
1 parent 73fb8fe commit 4af074d
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 19 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ import "github.com/duke-git/lancet/v2/maputil"
[[play](https://go.dev/play/p/isZZHOsDhFc)]
- **<big>GetOrSet</big>** : returns value of the given key or set the given value value if not present.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#GetOrSet)]
[[play](https://go.dev/play/p/IVQwO1OkEJC)]
- **<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)]
Expand Down Expand Up @@ -1209,11 +1210,13 @@ import "github.com/duke-git/lancet/v2/retry"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#BackoffStrategy)]
- **<big>RetryWithCustomBackoff</big>** : set abitary custom backoff strategy.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithCustomBackoff)]
[[play](https://go.dev/play/p/jIm_o2vb5Y4)]
- **<big>RetryWithLinearBackoff</big>** : set linear strategy backoff.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithLinearBackoff)]
[[play](https://go.dev/play/p/PDet2ZQZwcB)]
- **<big>RetryWithExponentialWithJitterBackoff</big>** : set exponential strategy backoff.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]

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


<h3 id="slice"> 18. Slice contains some functions to manipulate slice. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
Expand Down Expand Up @@ -1418,6 +1421,7 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/UR323iZLDpv)]
- **<big>UniqueByField</big>** : remove duplicate elements in struct slice by struct field.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueByField)]
[[play](https://go.dev/play/p/6cifcZSPIGu)]
- **<big>Union</big>** : creates a slice of unique elements, in order, from all given slices.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Union)]
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
Expand Down
9 changes: 7 additions & 2 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,10 @@ import "github.com/duke-git/lancet/v2/maputil"
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
- **<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>GetOrSet</big>** : 返回给定键的值,如果不存在则设置该值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#GetOrSet)]
[[play](https://go.dev/play/p/isZZHOsDhFc)]
[[play](https://go.dev/play/p/IVQwO1OkEJC)]
- **<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)]
Expand Down Expand Up @@ -1209,10 +1210,13 @@ import "github.com/duke-git/lancet/v2/retry"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#BackoffStrategy)]
- **<big>RetryWithCustomBackoff</big>** : 设置自定义退避策略。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithCustomBackoff)]
[[play](https://go.dev/play/p/jIm_o2vb5Y4)]
- **<big>RetryWithLinearBackoff</big>** : 设置线性策略退避。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithLinearBackoff)]
[[play](https://go.dev/play/p/PDet2ZQZwcB)]
- **<big>RetryWithExponentialWithJitterBackoff</big>** : 设置指数策略退避。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]
[[play](https://go.dev/play/p/xp1avQmn16X)]



Expand Down Expand Up @@ -1416,8 +1420,9 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>UniqueBy</big>** : 对切片的每个元素调用 iteratee 函数,然后删除重复元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueBy)]
[[play](https://go.dev/play/p/UR323iZLDpv)]
- **<big>UniqueByField</big>** : 根据struct字段对struct切片去重复
- **<big>UniqueByField</big>** : 根据struct字段对struct切片去重复
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByField)]
[[play](https://go.dev/play/p/6cifcZSPIGu)]
- **<big>Union</big>** : 合并多个切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Union)]
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
Expand Down
2 changes: 1 addition & 1 deletion docs/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ func main() {
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
```

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

```go
package main
Expand Down
6 changes: 3 additions & 3 deletions docs/api/packages/retry.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func main() {
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
```

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

```go
package main
Expand Down Expand Up @@ -384,7 +384,7 @@ func main() {
func RetryWithLinearBackoff(interval time.Duration) Option
```

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

```go
package main
Expand Down Expand Up @@ -429,7 +429,7 @@ func main() {
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
```

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

```go
package main
Expand Down
2 changes: 1 addition & 1 deletion docs/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,7 @@ func main() {
func UniqueByField[T any](slice []T, field string) ([]T, error)
```

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

```go
import (
Expand Down
2 changes: 1 addition & 1 deletion docs/en/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ func main() {
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
```

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

```go
package main
Expand Down
8 changes: 4 additions & 4 deletions docs/en/api/packages/retry.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func main() {
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
```

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

```go
package main
Expand Down Expand Up @@ -361,7 +361,7 @@ func main() {
return errors.New("error occurs")
}

err := retry,Retry(increaseNumber, retry.RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50}))
err := retry.Retry(increaseNumber, retry.RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50}))
if err != nil {
return
}
Expand All @@ -384,7 +384,7 @@ func main() {
func RetryWithLinearBackoff(interval time.Duration) Option
```

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

```go
package main
Expand Down Expand Up @@ -429,7 +429,7 @@ func main() {
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
```

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

```go
package main
Expand Down
2 changes: 1 addition & 1 deletion docs/en/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,7 @@ func main() {
func UniqueByField[T any](slice []T, field string) ([]T, error)
```

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

```go
import (
Expand Down
2 changes: 1 addition & 1 deletion maputil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator fun
}

// GetOrSet returns value of the given key or set the given value value if not present.
// Play: todo
// Play: https://go.dev/play/p/IVQwO1OkEJC
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V {
if v, ok := m[key]; ok {
return v
Expand Down
6 changes: 3 additions & 3 deletions retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func RetryTimes(n uint) Option {
}

// RetryWithCustomBackoff set abitary custom backoff strategy
// Play: todo
// Play: https://go.dev/play/p/jIm_o2vb5Y4
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option {
if backoffStrategy == nil {
panic("programming error: backoffStrategy must be not nil")
Expand All @@ -57,7 +57,7 @@ func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option {
}

// RetryWithLinearBackoff set linear strategy backoff
// Play: todo
// Play: https://go.dev/play/p/PDet2ZQZwcB
func RetryWithLinearBackoff(interval time.Duration) Option {
if interval <= 0 {
panic("programming error: retry interval should not be lower or equal to 0")
Expand All @@ -71,7 +71,7 @@ func RetryWithLinearBackoff(interval time.Duration) Option {
}

// RetryWithExponentialWithJitterBackoff set exponential strategy backoff
// Play: todo
// Play: https://go.dev/play/p/xp1avQmn16X
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option {
if interval <= 0 {
panic("programming error: retry interval should not be lower or equal to 0")
Expand Down
2 changes: 1 addition & 1 deletion slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
}

// UniqueByField remove duplicate elements in struct slice by struct field.
// Play: todo
// Play: https://go.dev/play/p/6cifcZSPIGu
func UniqueByField[T any](slice []T, field string) ([]T, error) {
seen := map[any]struct{}{}

Expand Down

0 comments on commit 4af074d

Please sign in to comment.