Skip to content

Commit

Permalink
feat(helper): Added Tap/With/When method
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
  • Loading branch information
flc1125 committed Mar 4, 2024
1 parent d5a8290 commit 1df13ab
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
52 changes: 52 additions & 0 deletions helper/README.md
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}
}
```
29 changes: 29 additions & 0 deletions helper/helper.go
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
}
87 changes: 87 additions & 0 deletions helper/helper_test.go
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"

Check failure on line 21 in helper/helper_test.go

View workflow job for this annotation

GitHub Actions / lint

string `bar` has 3 occurrences, make it a constant (goconst)
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) {

Check failure on line 40 in helper/helper_test.go

View workflow job for this annotation

GitHub Actions / lint

SA4009: argument f is overwritten before first use (staticcheck)
f = 20

Check failure on line 41 in helper/helper_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to f (ineffassign)
})
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)
}
2 changes: 2 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 1df13ab

Please sign in to comment.