Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(helper): Added ChainWithErr #143

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)
}
Loading