From f0cc0a4d23655501b92c6cfa1ee6f2d9a8f8cd42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Thu, 14 Mar 2024 16:30:29 +0800 Subject: [PATCH] refactor(helper): Chain to Pipe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Flcă‚› --- go.mod | 1 + go.sum | 4 ++++ helper/helper.go | 20 ++++++++++---------- helper/helper_test.go | 16 ++++++++-------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 9caacbe8..2e14f670 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d2cb08b3..8452cfc0 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/helper/helper.go b/helper/helper.go index dda8ad80..9479565d 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -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 { @@ -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) @@ -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 { @@ -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 -} diff --git a/helper/helper_test.go b/helper/helper_test.go index 9c7bbe98..aed3605d 100644 --- a/helper/helper_test.go +++ b/helper/helper_test.go @@ -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" }, @@ -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 @@ -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 }, @@ -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 @@ -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 }, @@ -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 },