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: wrap, definers #6

Merged
merged 1 commit into from
Jun 26, 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
5 changes: 4 additions & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ func (c *Container) Close() error {
}

// DefineContainers defines supplied containers using given config and root container
func DefineContainers[C, CF any](ctx context.Context, cfg CF, root C, containers ...interface {
func DefineContainers[C, CF any](ctx context.Context, cfg CF, definers []func(context.Context, CF, C), root C, containers ...interface {
Define(context.Context, CF, C)
}) C {
for _, d := range definers {
d(ctx, cfg, root)
}
for _, d := range containers {
d.Define(ctx, cfg, root)
}
Expand Down
12 changes: 12 additions & 0 deletions plumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type D[T any] struct {
resolve func()
deps []Dependency
listeners []func()
wrappers []func(T) T
}

// String return names of underlaying type
Expand All @@ -89,6 +90,9 @@ func (d *D[T]) define(resolve func()) {
resolve()
d.resolved = true
d.resolving = false
for _, w := range d.wrappers {
d.value = w(d.value)
}
for _, l := range d.listeners {
l()
}
Expand Down Expand Up @@ -193,6 +197,14 @@ func (d *D[T]) WhenResolved(callback func()) *D[T] {
return d
}

// Wrap registers a wrapping callback that will be triggered when dependency is resolved
// The callback allows to augment the original value. Wrapping should be used mostly to
// redefine the dependency for a different test environments
func (d *D[T]) Wrap(wrappers ...func(T) T) *D[T] {
d.wrappers = append(d.wrappers, wrappers...)
return d
}

// R represents a runnable dependency wrapper
// It is meant to be supplied into the Pipeline()
type R[T any] struct {
Expand Down