Skip to content

Commit

Permalink
feat: wrap, definers
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsmejkal committed Jun 26, 2024
1 parent 2c8f225 commit 136a245
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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

0 comments on commit 136a245

Please sign in to comment.