Skip to content

Commit

Permalink
refactor(context): refactor Context (#144)
Browse files Browse the repository at this point in the history
* refactor(bootstrap): refactor Context

Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>

* refactor(bootstrap): refactor Context

Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>

* refactor(context): refactor Context

Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>

---------

Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
  • Loading branch information
flc1125 authored Mar 12, 2024
1 parent b4e8a21 commit 1e2af18
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 109 deletions.
58 changes: 0 additions & 58 deletions bootstrap/context.go

This file was deleted.

47 changes: 0 additions & 47 deletions bootstrap/context_test.go

This file was deleted.

19 changes: 19 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package context

import (
"context"
)

type Provider func(ctx context.Context) (context.Context, error)

func Chain(ctx context.Context, providers ...Provider) (context.Context, error) {
var err error
for _, provider := range providers {
if provider != nil {
if ctx, err = provider(ctx); err != nil {
return ctx, err
}
}
}
return ctx, nil
}
48 changes: 48 additions & 0 deletions context/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package context

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestContext(t *testing.T) {
type (
mockProviderStruct1 struct{}
mockProviderStruct2 struct{}
mockProviderStruct3 struct{}
)

var (
mockProvider1 = func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, mockProviderStruct1{}, "mockProvider1"), nil
}

mockProvider2 = func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, mockProviderStruct2{}, "mockProvider2"), nil
}

mockProvider3 = func(ctx context.Context) (context.Context, error) {
return ctx, errors.New("mockProvider3")
}
)

ctx1, err1 := Chain(
context.Background(),
mockProvider1, mockProvider2,
)
assert.NoError(t, err1)
assert.Equal(t, "mockProvider1", ctx1.Value(mockProviderStruct1{}))
assert.Equal(t, "mockProvider2", ctx1.Value(mockProviderStruct2{}))

ctx2, err2 := Chain(
context.Background(),
mockProvider1, mockProvider3,
)
assert.Error(t, err2)
assert.NotNil(t, ctx2)
assert.Equal(t, "mockProvider1", ctx2.Value(mockProviderStruct1{}))
assert.Nil(t, ctx2.Value(mockProviderStruct3{}))
}
8 changes: 4 additions & 4 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ func TestChainWithErr(t *testing.T) {
// context
chain3 := ChainWithErr(
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "foo", "bar"), nil
return context.WithValue(ctx, "foo", "bar"), nil //nolint:revive,staticcheck
},
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "bar", "baz"), nil
return context.WithValue(ctx, "bar", "baz"), nil //nolint:revive,staticcheck
},
)

Expand All @@ -319,9 +319,9 @@ func TestChainWithErr(t *testing.T) {
// context with error
chain4 := ChainWithErr(
func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, "foo", "bar"), nil
return context.WithValue(ctx, "foo", "bar"), nil //nolint:revive,staticcheck
},
func(ctx context.Context) (context.Context, error) {
func(context.Context) (context.Context, error) {
return nil, assert.AnError
},
)
Expand Down

0 comments on commit 1e2af18

Please sign in to comment.