Skip to content

Commit

Permalink
feat(helper): Added ChainWithErr
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 12, 2024
1 parent 12e8420 commit f5785f0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
12 changes: 12 additions & 0 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func Chain[T any](fns ...func(T) T) func(T) T {
}
}

func ChainWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error) {
var err error
return func(v T) (T, error) {
for _, fn := range fns {
if v, err = fn(v); err != nil {
return v, err
}
}
return v, nil
}
}

func When[T any](value T, condition bool, callbacks ...func(T) T) T {
if condition {
return With(value, callbacks...)
Expand Down
70 changes: 70 additions & 0 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helper

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -260,3 +261,72 @@ func TestScan_ComplexStruct(t *testing.T) {
assert.Equal(t, "A1", b.Companies[0].Name)
assert.Equal(t, "A2", b.Companies[1].Name)
}

func TestChainWithErr(t *testing.T) {
// chain functions
chain := ChainWithErr(
func(s string) (string, error) {
return s + "1", nil
},
func(s string) (string, error) {
return s + "2", nil
},
func(s string) (string, error) {
return s + "3", nil
},
)

got, err := chain("0")
assert.Nil(t, err)
assert.Equal(t, "0123", got)

// chain functions
chain2 := ChainWithErr(
func(foo *foo) (*foo, error) {
foo.Name = "bar"
return foo, nil
},
func(foo *foo) (*foo, error) {
foo.Age = 18
return foo, nil
},
)

f := &foo{Name: "foo"}
assert.Equal(t, "foo", f.Name)
assert.Equal(t, 0, f.Age)

got2, err := chain2(f)
assert.Nil(t, err)
assert.Equal(t, "bar", got2.Name)
assert.Equal(t, 18, got2.Age)

// context
chain3 := ChainWithErr(
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "foo", "bar"), nil

Check warning on line 307 in helper/helper_test.go

View workflow job for this annotation

GitHub Actions / lint

context-keys-type: should not use basic type string as key in context.WithValue (revive)
},
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "bar", "baz"), nil

Check warning on line 310 in helper/helper_test.go

View workflow job for this annotation

GitHub Actions / lint

context-keys-type: should not use basic type string as key in context.WithValue (revive)
},
)

ctx, err := chain3(context.Background())
assert.NoError(t, err)
assert.Equal(t, "bar", ctx.Value("foo"))
assert.Equal(t, "baz", ctx.Value("bar"))

// context with error
chain4 := ChainWithErr(
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "foo", "bar"), nil

Check warning on line 322 in helper/helper_test.go

View workflow job for this annotation

GitHub Actions / lint

context-keys-type: should not use basic type string as key in context.WithValue (revive)
},
func(ctx context.Context) (context.Context, error) {

Check warning on line 324 in helper/helper_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return nil, assert.AnError
},
)

ctx, err = chain4(context.Background())
assert.Error(t, err)
assert.Nil(t, ctx)
}

0 comments on commit f5785f0

Please sign in to comment.