Skip to content

Commit

Permalink
feat(foundation): Added BootstrapFunc and TerminateFunc
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 committed Apr 7, 2024
1 parent 79709d3 commit 4543aae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions foundation/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,23 @@ func (*BaseProvider) Bootstrap(ctx context.Context) (context.Context, error) {
func (*BaseProvider) Terminate(ctx context.Context) (context.Context, error) {
return ctx, nil
}

type BootstrapFunc func(context.Context) (context.Context, error)

func (f BootstrapFunc) Bootstrap(ctx context.Context) (context.Context, error) {
return f(ctx)
}

func (f BootstrapFunc) Terminate(ctx context.Context) (context.Context, error) {
return ctx, nil
}

type TerminateFunc func(context.Context) (context.Context, error)

func (f TerminateFunc) Bootstrap(ctx context.Context) (context.Context, error) {
return ctx, nil
}

func (f TerminateFunc) Terminate(ctx context.Context) (context.Context, error) {
return f(ctx)
}
40 changes: 40 additions & 0 deletions foundation/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package foundation

import (
"context"
"testing"

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

type providerFuncKey struct{}

func TestProvider_BootstrapFunc(t *testing.T) {
p := BootstrapFunc(func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, providerFuncKey{}, "test"), nil
})

ctx := context.Background()
ctx, err := p.Bootstrap(ctx)
assert.NoError(t, err)
assert.Equal(t, "test", ctx.Value(providerFuncKey{}))

ctx, err = p.Terminate(ctx)
assert.NoError(t, err)
assert.Equal(t, "test", ctx.Value(providerFuncKey{}))
}

func TestProvider_BootstrapFuncError(t *testing.T) {
p := TerminateFunc(func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, providerFuncKey{}, "test"), nil
})

ctx := context.Background()
ctx, err := p.Bootstrap(ctx)
assert.NoError(t, err)
assert.Nil(t, ctx.Value(providerFuncKey{}))

ctx, err = p.Terminate(ctx)
assert.NoError(t, err)
assert.Equal(t, "test", ctx.Value(providerFuncKey{}))
}

0 comments on commit 4543aae

Please sign in to comment.