mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
8
Commits
c88fd3db86
...
66fd8cf651
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66fd8cf651 | ||
|
|
a6be1828b9 | ||
|
|
8f5d297572 | ||
|
|
a1a4fdc598 | ||
|
|
1610076d22 | ||
|
|
cacbf97223 | ||
|
|
cd156dba5f | ||
|
|
3a71a8697d |
@@ -13,7 +13,7 @@ Optional类型代表一个可选的值,它要么包含一个实际值,要么
|
||||
## 用法
|
||||
```go
|
||||
import (
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
```
|
||||
|
||||
@@ -54,7 +54,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -84,7 +84,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -123,7 +123,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -152,7 +152,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -261,7 +261,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -304,7 +304,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -334,7 +334,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -368,7 +368,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -399,7 +399,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -7,6 +7,7 @@ function 函数包控制函数执行流程,包含部分函数式编程。
|
||||
## 源码:
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go)
|
||||
- [https://github.com/duke-git/lancet/blob/main/function/predicate.go](https://github.com/duke-git/lancet/blob/main/function/predicate.go)
|
||||
- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -32,6 +33,10 @@ import (
|
||||
- [Schedule](#Schedule)
|
||||
- [Pipeline](#Pipeline)
|
||||
- [Watcher](#Watcher)
|
||||
- [And](#And)
|
||||
- [Or](#Or)
|
||||
- [Negate](#Negate)
|
||||
- [Nor](#Nor)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -404,3 +409,157 @@ func longRunningTask() {
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### <span id="And">And</span>
|
||||
|
||||
<p>返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑and操作。只有当所有谓词判断函数对于给定的值都返回true时,返回true, 否则返回false。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func And[T any](predicates ...func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
isNumericAndLength5 := function.And(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(isNumericAndLength5("12345"))
|
||||
fmt.Println(isNumericAndLength5("1234"))
|
||||
fmt.Println(isNumericAndLength5("abcde"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Or">Or</span>
|
||||
|
||||
<p>返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑or操作。只有当所有谓词判断函数对于给定的值都返回false时,返回false, 否则返回true。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Or[T any](predicates ...func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
containsDigitOrSpecialChar := function.Or(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return strings.ContainsAny(s, "!@#$%") },
|
||||
)
|
||||
|
||||
fmt.Println(containsDigitOrSpecialChar("hello!"))
|
||||
fmt.Println(containsDigitOrSpecialChar("hello"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Negate">Negate</span>
|
||||
|
||||
<p>返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Negate[T any](predicate func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Define some simple predicates for demonstration
|
||||
isUpperCase := func(s string) bool {
|
||||
return strings.ToUpper(s) == s
|
||||
}
|
||||
isLowerCase := func(s string) bool {
|
||||
return strings.ToLower(s) == s
|
||||
}
|
||||
isMixedCase := function.Negate(function.Or(isUpperCase, isLowerCase))
|
||||
|
||||
fmt.Println(isMixedCase("ABC"))
|
||||
fmt.Println(isMixedCase("AbC"))
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Nor">Nor</span>
|
||||
|
||||
<p>返回一个组合谓词函数,表示给定值上所有谓词逻辑非或 (nor) 的结果。只有当所有谓词函数对给定值都返回false时,该组合谓词函数才返回true。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Nor[T any](predicates ...func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
match := function.Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(match("dbcdckkeee"))
|
||||
|
||||
|
||||
match = function.Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(match("0123456789"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
@@ -13,7 +13,7 @@ Optional is a type that may or may not contain a non-nil value.
|
||||
## Usage
|
||||
```go
|
||||
import (
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
```
|
||||
|
||||
@@ -54,7 +54,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -83,7 +83,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -121,7 +121,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -149,7 +149,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -177,7 +177,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -215,7 +215,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -255,7 +255,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -298,7 +298,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -328,7 +328,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -362,7 +362,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -393,7 +393,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
"github.com/duke-git/lancet/v2/datastructure/optional"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -7,6 +7,7 @@ Package function can control the flow of function execution and support part of
|
||||
## Source:
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go)
|
||||
- [https://github.com/duke-git/lancet/blob/main/function/predicate.go](https://github.com/duke-git/lancet/blob/main/function/predicate.go)
|
||||
- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -32,6 +33,10 @@ import (
|
||||
- [Schedule](#Schedule)
|
||||
- [Pipeline](#Pipeline)
|
||||
- [Watcher](#Watcher)
|
||||
- [And](#And)
|
||||
- [Or](#Or)
|
||||
- [Negate](#Negate)
|
||||
- [Nor](#Nor)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -403,3 +408,158 @@ func longRunningTask() {
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
### <span id="And">And</span>
|
||||
|
||||
<p>Returns a composed predicate that represents the logical AND of a list of predicates. It evaluates to true only if all predicates evaluate to true for the given value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func And[T any](predicates ...func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
isNumericAndLength5 := function.And(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(isNumericAndLength5("12345"))
|
||||
fmt.Println(isNumericAndLength5("1234"))
|
||||
fmt.Println(isNumericAndLength5("abcde"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Or">Or</span>
|
||||
|
||||
<p>Returns a composed predicate that represents the logical OR of a list of predicates. It evaluates to true if at least one of the predicates evaluates to true for the given value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Or[T any](predicates ...func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
containsDigitOrSpecialChar := function.Or(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return strings.ContainsAny(s, "!@#$%") },
|
||||
)
|
||||
|
||||
fmt.Println(containsDigitOrSpecialChar("hello!"))
|
||||
fmt.Println(containsDigitOrSpecialChar("hello"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Negate">Negate</span>
|
||||
|
||||
<p>Returns a predicate that represents the logical negation of this predicate.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Negate[T any](predicate func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Define some simple predicates for demonstration
|
||||
isUpperCase := func(s string) bool {
|
||||
return strings.ToUpper(s) == s
|
||||
}
|
||||
isLowerCase := func(s string) bool {
|
||||
return strings.ToLower(s) == s
|
||||
}
|
||||
isMixedCase := function.Negate(function.Or(isUpperCase, isLowerCase))
|
||||
|
||||
fmt.Println(isMixedCase("ABC"))
|
||||
fmt.Println(isMixedCase("AbC"))
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Nor">Nor</span>
|
||||
|
||||
<p>Returns a composed predicate that represents the logical NOR of a list of predicates. It evaluates to true only if all predicates evaluate to false for the given value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Nor[T any](predicates ...func(T) bool) func(T) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/function"
|
||||
)
|
||||
|
||||
func main() {
|
||||
match := function.Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(match("dbcdckkeee"))
|
||||
|
||||
|
||||
match = function.Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(match("0123456789"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
@@ -32,3 +32,16 @@ func Or[T any](predicates ...func(T) bool) func(T) bool {
|
||||
return false // False if all predicates are false
|
||||
}
|
||||
}
|
||||
|
||||
// Nor returns a composed predicate that represents the logical NOR of a list of predicates.
|
||||
// It evaluates to true only if all predicates evaluate to false for the given value.
|
||||
func Nor[T any](predicates ...func(T) bool) func(T) bool {
|
||||
return func(value T) bool {
|
||||
for _, predicate := range predicates {
|
||||
if predicate(value) {
|
||||
return false // If any predicate evaluates to true, the NOR result is false
|
||||
}
|
||||
}
|
||||
return true // Only returns true if all predicates evaluate to false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package function
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExampleNegate() {
|
||||
// Define some simple predicates for demonstration
|
||||
isUpperCase := func(s string) bool {
|
||||
return strings.ToUpper(s) == s
|
||||
}
|
||||
isLowerCase := func(s string) bool {
|
||||
return strings.ToLower(s) == s
|
||||
}
|
||||
isMixedCase := Negate(Or(isUpperCase, isLowerCase))
|
||||
|
||||
fmt.Println(isMixedCase("ABC"))
|
||||
fmt.Println(isMixedCase("AbC"))
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleOr() {
|
||||
containsDigitOrSpecialChar := Or(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return strings.ContainsAny(s, "!@#$%") },
|
||||
)
|
||||
|
||||
fmt.Println(containsDigitOrSpecialChar("hello!"))
|
||||
fmt.Println(containsDigitOrSpecialChar("hello"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleAnd() {
|
||||
isNumericAndLength5 := And(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(isNumericAndLength5("12345"))
|
||||
fmt.Println(isNumericAndLength5("1234"))
|
||||
fmt.Println(isNumericAndLength5("abcde"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleNor() {
|
||||
match := Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(match("dbcdckkeee"))
|
||||
|
||||
match = Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
fmt.Println(match("0123456789"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
// func ExamplePredicatesMix() {
|
||||
// a := Or(
|
||||
// func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
// func(s string) bool { return strings.ContainsAny(s, "!") },
|
||||
// )
|
||||
|
||||
// b := And(
|
||||
// func(s string) bool { return strings.ContainsAny(s, "hello") },
|
||||
// func(s string) bool { return strings.ContainsAny(s, "!") },
|
||||
// )
|
||||
|
||||
// c := Negate(And(a, b))
|
||||
// fmt.Println(c("hello!"))
|
||||
|
||||
// c = Nor(a, b)
|
||||
// fmt.Println(c("hello!"))
|
||||
|
||||
// // Output:
|
||||
// // false
|
||||
// // false
|
||||
// }
|
||||
@@ -54,6 +54,26 @@ func TestPredicatesAndPure(t *testing.T) {
|
||||
assert.ShouldBeFalse(isNumericAndLength5("abcde"))
|
||||
}
|
||||
|
||||
func TestPredicatesNorPure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestPredicatesNorPure")
|
||||
|
||||
match := Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
assert.ShouldBeTrue(match("dbcdckkeee"))
|
||||
|
||||
match = Nor(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
assert.ShouldBeFalse(match("0123456789"))
|
||||
}
|
||||
|
||||
func TestPredicatesMix(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -72,4 +92,7 @@ func TestPredicatesMix(t *testing.T) {
|
||||
c := Negate(And(a, b))
|
||||
|
||||
assert.ShouldBeFalse(c("hello!"))
|
||||
|
||||
c = Nor(a, b)
|
||||
assert.ShouldBeFalse(c("hello!"))
|
||||
}
|
||||
|
||||
+7
-3
@@ -307,8 +307,7 @@ func HasKey[K comparable, V any](m map[K]V, key K) bool {
|
||||
|
||||
// MapToStruct converts map to struct
|
||||
// Play: todo
|
||||
func MapToStruct(m map[string]interface{}, structObj interface{}) error {
|
||||
|
||||
func MapToStruct(m map[string]any, structObj any) error {
|
||||
for k, v := range m {
|
||||
err := setStructField(structObj, k, v)
|
||||
if err != nil {
|
||||
@@ -341,7 +340,12 @@ func setStructField(structObj any, fieldName string, fieldValue any) error {
|
||||
|
||||
if fieldVal.Type() != val.Type() {
|
||||
|
||||
if m, ok := fieldValue.(map[string]interface{}); ok {
|
||||
if val.CanConvert(fieldVal.Type()) {
|
||||
fieldVal.Set(val.Convert(fieldVal.Type()))
|
||||
return nil
|
||||
}
|
||||
|
||||
if m, ok := fieldValue.(map[string]any); ok {
|
||||
|
||||
if fieldVal.Kind() == reflect.Struct {
|
||||
return MapToStruct(m, fieldVal.Addr().Interface())
|
||||
|
||||
+13
-6
@@ -62,18 +62,25 @@ var _ = func() struct{} {
|
||||
*/
|
||||
// Play: https://go.dev/play/p/4K7KBEPgS5M
|
||||
func MapTo(src any, dst any) error {
|
||||
|
||||
dstRef := reflect.ValueOf(dst)
|
||||
|
||||
if dstRef.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("dst is not ptr")
|
||||
}
|
||||
|
||||
dstElem := dstRef.Type().Elem()
|
||||
if dstElem.Kind() == reflect.Struct {
|
||||
srcMap := src.(map[string]interface{})
|
||||
return MapToStruct(srcMap, dst)
|
||||
}
|
||||
|
||||
dstRef = reflect.Indirect(dstRef)
|
||||
|
||||
srcRef := reflect.ValueOf(src)
|
||||
if srcRef.Kind() == reflect.Ptr || srcRef.Kind() == reflect.Interface {
|
||||
srcRef = srcRef.Elem()
|
||||
}
|
||||
|
||||
if f, ok := mapHandlers[srcRef.Kind()]; ok {
|
||||
return f(srcRef, dstRef)
|
||||
}
|
||||
@@ -121,10 +128,7 @@ func convertSlice(src reflect.Value, dst reflect.Value) error {
|
||||
|
||||
func convertMap(src reflect.Value, dst reflect.Value) error {
|
||||
if src.Kind() != reflect.Map || dst.Kind() != reflect.Struct {
|
||||
// if src.Kind() == reflect.Map {
|
||||
// return convertMap(src, dst)
|
||||
// } else
|
||||
if src.Kind() == reflect.Interface {
|
||||
if src.Kind() == reflect.Interface && dst.IsValid() {
|
||||
return convertMap(src.Elem(), dst)
|
||||
} else {
|
||||
return fmt.Errorf("src or dst type error,%s,%s", src.Type().String(), dst.Type().String())
|
||||
@@ -132,7 +136,9 @@ func convertMap(src reflect.Value, dst reflect.Value) error {
|
||||
}
|
||||
dstType := dst.Type()
|
||||
num := dstType.NumField()
|
||||
|
||||
exist := map[string]int{}
|
||||
|
||||
for i := 0; i < num; i++ {
|
||||
k := dstType.Field(i).Tag.Get("json")
|
||||
if k == "" {
|
||||
@@ -141,7 +147,6 @@ func convertMap(src reflect.Value, dst reflect.Value) error {
|
||||
if strings.Contains(k, ",") {
|
||||
taglist := strings.Split(k, ",")
|
||||
if taglist[0] == "" {
|
||||
|
||||
k = dstType.Field(i).Name
|
||||
} else {
|
||||
k = taglist[0]
|
||||
@@ -153,9 +158,11 @@ func convertMap(src reflect.Value, dst reflect.Value) error {
|
||||
}
|
||||
|
||||
keys := src.MapKeys()
|
||||
|
||||
for _, key := range keys {
|
||||
if index, ok := exist[key.String()]; ok {
|
||||
v := dst.Field(index)
|
||||
|
||||
if v.Kind() == reflect.Struct {
|
||||
err := convertMap(src.MapIndex(key), v)
|
||||
if err != nil {
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
|
||||
type (
|
||||
Person struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Phone string `json:"phone"`
|
||||
Address Address `json:"address"`
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Phone string `json:"phone"`
|
||||
Address *Address `json:"address"`
|
||||
}
|
||||
|
||||
Address struct {
|
||||
@@ -38,6 +38,7 @@ func TestStructType(t *testing.T) {
|
||||
var p Person
|
||||
err := MapTo(src, &p)
|
||||
assert.IsNil(err)
|
||||
|
||||
assert.Equal(src["name"], p.Name)
|
||||
assert.Equal(src["age"], p.Age)
|
||||
assert.Equal(src["phone"], p.Phone)
|
||||
|
||||
+41
-12
@@ -18,14 +18,14 @@ const (
|
||||
// DefaultRetryTimes times of retry
|
||||
DefaultRetryTimes = 5
|
||||
// DefaultRetryDuration time duration of two retries
|
||||
DefaultRetryDuration = time.Second * 3
|
||||
DefaultRetryLinearInterval = time.Second * 3
|
||||
)
|
||||
|
||||
// RetryConfig is config for retry
|
||||
type RetryConfig struct {
|
||||
context context.Context
|
||||
retryTimes uint
|
||||
retryDuration time.Duration
|
||||
context context.Context
|
||||
retryTimes uint
|
||||
backoffStrategy BackoffStrategy
|
||||
}
|
||||
|
||||
// RetryFunc is function that retry executes
|
||||
@@ -42,11 +42,17 @@ func RetryTimes(n uint) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// RetryDuration set duration of retries.
|
||||
// Play: https://go.dev/play/p/nk2XRmagfVF
|
||||
func RetryDuration(d time.Duration) Option {
|
||||
// RetryWithLinearBackoff set linear strategy backoff
|
||||
// todo: Add playground link
|
||||
func RetryWithLinearBackoff(interval time.Duration) Option {
|
||||
if interval <= 0 {
|
||||
panic("programming error: retry interval should not be lower or equal to 0")
|
||||
}
|
||||
|
||||
return func(rc *RetryConfig) {
|
||||
rc.retryDuration = d
|
||||
rc.backoffStrategy = &linear{
|
||||
interval: interval,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,21 +69,26 @@ func Context(ctx context.Context) Option {
|
||||
// Play: https://go.dev/play/p/nk2XRmagfVF
|
||||
func Retry(retryFunc RetryFunc, opts ...Option) error {
|
||||
config := &RetryConfig{
|
||||
retryTimes: DefaultRetryTimes,
|
||||
retryDuration: DefaultRetryDuration,
|
||||
context: context.TODO(),
|
||||
retryTimes: DefaultRetryTimes,
|
||||
context: context.TODO(),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(config)
|
||||
}
|
||||
|
||||
if config.backoffStrategy == nil {
|
||||
config.backoffStrategy = &linear{
|
||||
interval: DefaultRetryLinearInterval,
|
||||
}
|
||||
}
|
||||
|
||||
var i uint
|
||||
for i < config.retryTimes {
|
||||
err := retryFunc()
|
||||
if err != nil {
|
||||
select {
|
||||
case <-time.After(config.retryDuration):
|
||||
case <-time.After(config.backoffStrategy.CalculateInterval()):
|
||||
case <-config.context.Done():
|
||||
return errors.New("retry is cancelled")
|
||||
}
|
||||
@@ -93,3 +104,21 @@ func Retry(retryFunc RetryFunc, opts ...Option) error {
|
||||
|
||||
return fmt.Errorf("function %s run failed after %d times retry", funcName, i)
|
||||
}
|
||||
|
||||
// BackoffStrategy is an interface that defines a method for calculating backoff intervals.
|
||||
type BackoffStrategy interface {
|
||||
// CalculateInterval returns the time.Duration after which the next retry attempt should be made.
|
||||
CalculateInterval() time.Duration
|
||||
}
|
||||
|
||||
// linear is a struct that implements the BackoffStrategy interface using a linear backoff strategy.
|
||||
type linear struct {
|
||||
// interval specifies the fixed duration to wait between retry attempts.
|
||||
interval time.Duration
|
||||
}
|
||||
|
||||
// CalculateInterval is the method implementation for the linear struct.
|
||||
// It returns the fixed interval defined in the linear struct.
|
||||
func (l *linear) CalculateInterval() time.Duration {
|
||||
return l.interval
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func ExampleContext() {
|
||||
}
|
||||
|
||||
Retry(increaseNumber,
|
||||
RetryDuration(time.Microsecond*50),
|
||||
RetryWithLinearBackoff(time.Microsecond*50),
|
||||
Context(ctx),
|
||||
)
|
||||
|
||||
@@ -30,7 +30,7 @@ func ExampleContext() {
|
||||
// 4
|
||||
}
|
||||
|
||||
func ExampleRetryDuration() {
|
||||
func ExampleRetryWithLinearBackoff() {
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
@@ -40,7 +40,7 @@ func ExampleRetryDuration() {
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryDuration(time.Microsecond*50))
|
||||
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func ExampleRetry() {
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryDuration(time.Microsecond*50))
|
||||
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
+4
-4
@@ -20,7 +20,7 @@ func TestRetryFailed(t *testing.T) {
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryDuration(time.Microsecond*50))
|
||||
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
|
||||
|
||||
assert.IsNotNil(err)
|
||||
assert.Equal(DefaultRetryTimes, number)
|
||||
@@ -40,7 +40,7 @@ func TestRetrySucceeded(t *testing.T) {
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryDuration(time.Microsecond*50))
|
||||
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
|
||||
|
||||
assert.IsNil(err)
|
||||
assert.Equal(DefaultRetryTimes, number)
|
||||
@@ -57,7 +57,7 @@ func TestSetRetryTimes(t *testing.T) {
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryDuration(time.Microsecond*50), RetryTimes(3))
|
||||
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50), RetryTimes(3))
|
||||
|
||||
assert.IsNotNil(err)
|
||||
assert.Equal(3, number)
|
||||
@@ -79,7 +79,7 @@ func TestCancelRetry(t *testing.T) {
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber,
|
||||
RetryDuration(time.Microsecond*50),
|
||||
RetryWithLinearBackoff(time.Microsecond*50),
|
||||
Context(ctx),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user