Skip to content

Commit

Permalink
refactor(helper): Chain to Pipe
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 14, 2024
1 parent 98db9f7 commit f0cc0a4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240213162025-012b6fc9bca9 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -130,6 +132,8 @@ golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20240205150955-31a09d347014 h1:g/4bk7P6TPMkAUbUhquq98xey1slwvuVJPosdBqYJlU=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 h1:JpwMPBpFN3uKhdaekDpiNlImDdkUAyiJ6ez/uxGaUSo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240213162025-012b6fc9bca9 h1:hZB7eLIaYlW9qXRfCq/qDaPdbeY3757uARz5Vvfv+cY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240213162025-012b6fc9bca9/go.mod h1:YUWgXUFRPfoYK1IHMuxH5K6nPEXSCzIMljnQ59lLRCk=
google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
Expand Down
20 changes: 10 additions & 10 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import (
"encoding/json"
)

func If[T any](condition bool, trueVal T, falseVal T) T {
if condition {
return trueVal
}

return falseVal
}

func Tap[T any](value T, callbacks ...func(T)) T {
for _, callback := range callbacks {
if callback != nil {
Expand All @@ -24,7 +32,7 @@ func With[T any](value T, callbacks ...func(T) T) T {
return value
}

func Chain[T any](fns ...func(T) T) func(T) T {
func Pipe[T any](fns ...func(T) T) func(T) T {
return func(v T) T {
for _, fn := range fns {
v = fn(v)
Expand All @@ -33,7 +41,7 @@ func Chain[T any](fns ...func(T) T) func(T) T {
}
}

func ChainWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error) {
func PipeWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error) {
var err error
return func(v T) (T, error) {
for _, fn := range fns {
Expand Down Expand Up @@ -61,11 +69,3 @@ func Scan(src any, dest any) error {

return json.Unmarshal(bytes, dest)
}

func If[T any](condition bool, trueVal T, falseVal T) T {
if condition {
return trueVal
}

return falseVal
}
16 changes: 8 additions & 8 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func TestWhen(t *testing.T) {
assert.Equal(t, 18, f3.Age)
}

func TestChain(t *testing.T) {
func TestPipe(t *testing.T) {
// chain functions
chain := Chain(
chain := Pipe(
func(s string) string {
return s + "1"
},
Expand All @@ -138,7 +138,7 @@ func TestChain(t *testing.T) {
assert.Equal(t, "0123", chain("0"))

// chain functions
chain2 := Chain(
chain2 := Pipe(
func(foo *foo) *foo {
foo.Name = "bar"
return foo
Expand Down Expand Up @@ -262,9 +262,9 @@ func TestScan_ComplexStruct(t *testing.T) {
assert.Equal(t, "A2", b.Companies[1].Name)
}

func TestChainWithErr(t *testing.T) {
func TestPipeWithErr(t *testing.T) {
// chain functions
chain := ChainWithErr(
chain := PipeWithErr(
func(s string) (string, error) {
return s + "1", nil
},
Expand All @@ -281,7 +281,7 @@ func TestChainWithErr(t *testing.T) {
assert.Equal(t, "0123", got)

// chain functions
chain2 := ChainWithErr(
chain2 := PipeWithErr(
func(foo *foo) (*foo, error) {
foo.Name = "bar"
return foo, nil
Expand All @@ -302,7 +302,7 @@ func TestChainWithErr(t *testing.T) {
assert.Equal(t, 18, got2.Age)

// context
chain3 := ChainWithErr(
chain3 := PipeWithErr(
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "foo", "bar"), nil //nolint:revive,staticcheck
},
Expand All @@ -317,7 +317,7 @@ func TestChainWithErr(t *testing.T) {
assert.Equal(t, "baz", ctx.Value("bar"))

// context with error
chain4 := ChainWithErr(
chain4 := PipeWithErr(
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "foo", "bar"), nil //nolint:revive,staticcheck
},
Expand Down

0 comments on commit f0cc0a4

Please sign in to comment.