From 1df13ab55e01e310d522088bb50f4a4254d42724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Mon, 4 Mar 2024 17:40:47 +0800 Subject: [PATCH] feat(helper): Added `Tap/With/When` method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Flcă‚› --- helper/README.md | 52 ++++++++++++++++++++++++++ helper/helper.go | 29 +++++++++++++++ helper/helper_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++ utils/utils.go | 2 + 4 files changed, 170 insertions(+) create mode 100644 helper/README.md create mode 100644 helper/helper.go create mode 100644 helper/helper_test.go diff --git a/helper/README.md b/helper/README.md new file mode 100644 index 00000000..ad924e88 --- /dev/null +++ b/helper/README.md @@ -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} +} +``` \ No newline at end of file diff --git a/helper/helper.go b/helper/helper.go new file mode 100644 index 00000000..5f4790b7 --- /dev/null +++ b/helper/helper.go @@ -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 +} diff --git a/helper/helper_test.go b/helper/helper_test.go new file mode 100644 index 00000000..dcf8a908 --- /dev/null +++ b/helper/helper_test.go @@ -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) +} diff --git a/utils/utils.go b/utils/utils.go index b45b00d9..ae5d2de5 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,6 +5,7 @@ import ( "reflect" ) +// Deprecated: use helper.Tap instead // Tap calls the given callback with the given value then returns the value. func Tap(value interface{}, callbacks ...func(interface{})) interface{} { for _, callback := range callbacks { @@ -14,6 +15,7 @@ func Tap(value interface{}, callbacks ...func(interface{})) interface{} { return value } +// Deprecated: use helper.With instead // With calls the given callbacks with the given value then return the value. func With(value interface{}, callbacks ...func(interface{}) interface{}) interface{} { for _, callback := range callbacks {