-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper): Added
Tap/With/When
method
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
- Loading branch information
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# helper | ||
|
||
## Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-kratos-ecosystem/components/v2/helper" | ||
) | ||
|
||
type User struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
func main() { | ||
user := &User{Name: "foo"} | ||
|
||
// Tap | ||
user = helper.Tap(user, func(u *User) { | ||
u.Name = "bar" | ||
u.Age = 18 | ||
}) | ||
fmt.Println(user) | ||
// output: | ||
// &{bar 18} | ||
|
||
// With | ||
user = helper.With(user, func(u *User) *User { | ||
u.Name = "baz" | ||
u.Age = 19 | ||
return u | ||
}) | ||
fmt.Println(user) | ||
// output: | ||
// &{baz 19} | ||
|
||
// When | ||
user = helper.When(user, true, func(u *User) *User { | ||
u.Name = "Flc" | ||
u.Age = 20 | ||
return u | ||
}) | ||
|
||
fmt.Println(user) | ||
// output: | ||
// &{Flc 20} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package helper | ||
|
||
func Tap[T any](value T, callbacks ...func(T)) T { | ||
for _, callback := range callbacks { | ||
if callback != nil { | ||
callback(value) | ||
} | ||
} | ||
|
||
return value | ||
} | ||
|
||
func With[T any](value T, callbacks ...func(T) T) T { | ||
for _, callback := range callbacks { | ||
if callback != nil { | ||
value = callback(value) | ||
} | ||
} | ||
|
||
return value | ||
} | ||
|
||
func When[T any](value T, condition bool, callbacks ...func(T) T) T { | ||
if condition { | ||
return With(value, callbacks...) | ||
} | ||
|
||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package helper | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type foo struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
func TestTap_Struct(t *testing.T) { | ||
f := &foo{Name: "foo"} | ||
|
||
assert.Equal(t, "foo", f.Name) | ||
assert.Equal(t, 0, f.Age) | ||
|
||
f = Tap(f, func(f *foo) { | ||
f.Name = "bar" | ||
f.Age = 18 | ||
}) | ||
assert.Equal(t, "bar", f.Name) | ||
assert.Equal(t, 18, f.Age) | ||
} | ||
|
||
func TestTap_Int(t *testing.T) { | ||
f := new(int) | ||
*f = 10 | ||
|
||
assert.Equal(t, 10, *f) | ||
f = Tap(f, func(f *int) { | ||
*f = 20 | ||
}) | ||
assert.Equal(t, 20, *f) | ||
|
||
b := 10 | ||
assert.Equal(t, 10, b) | ||
b = Tap(b, func(f int) { | ||
f = 20 | ||
}) | ||
assert.Equal(t, 10, b) | ||
|
||
b2 := Tap(&b, func(f *int) { | ||
*f = 20 | ||
}) | ||
assert.Equal(t, 20, *b2) | ||
} | ||
|
||
func TestWith(t *testing.T) { | ||
f := &foo{Name: "foo"} | ||
|
||
assert.Equal(t, "foo", f.Name) | ||
assert.Equal(t, 0, f.Age) | ||
|
||
f2 := With(f, func(f *foo) *foo { | ||
f.Name = "bar" | ||
f.Age = 18 | ||
return f | ||
}) | ||
assert.Equal(t, "bar", f2.Name) | ||
assert.Equal(t, 18, f2.Age) | ||
} | ||
|
||
func TestWhen(t *testing.T) { | ||
f := &foo{Name: "foo"} | ||
|
||
assert.Equal(t, "foo", f.Name) | ||
assert.Equal(t, 0, f.Age) | ||
|
||
f2 := When(f, true, func(f *foo) *foo { | ||
f.Name = "bar" | ||
f.Age = 18 | ||
return f | ||
}) | ||
assert.Equal(t, "bar", f2.Name) | ||
assert.Equal(t, 18, f2.Age) | ||
|
||
f3 := When(f, false, func(f *foo) *foo { | ||
f.Name = "baz" | ||
f.Age = 20 | ||
return f | ||
}) | ||
assert.Equal(t, "bar", f3.Name) | ||
assert.Equal(t, 18, f3.Age) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters