Skip to content

Commit

Permalink
Adds the scope.go module to the resolving package (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
matzefriedrich authored Jul 16, 2024
1 parent 153e108 commit 2fd6730
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Once all service types are registered, a resolver service (the actual container)

````golang
resolver := resolving.NewResolver(registry)
scope := internal.NewScopedContext(context.Background())
scope := resolving.NewScopedContext(context.Background())
consumerInstance, _ := resolving.ResolveRequiredService[FooConsumer](resolver, scope)
````

Expand Down
11 changes: 3 additions & 8 deletions internal/core/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ type InstanceBag struct {
}

const (
parsleyContext = "__parsley"
ParsleyContext = "__parsley"
)

func NewScopedContext(ctx context.Context) context.Context {
instances := make(map[uint64]interface{})
return context.WithValue(ctx, parsleyContext, instances)
}

// NewGlobalInstanceBag Creates a new InstanceBag object with global scope.
func NewGlobalInstanceBag() *InstanceBag {
return &InstanceBag{
Expand Down Expand Up @@ -47,7 +42,7 @@ func (b *InstanceBag) TryResolveInstance(ctx context.Context, registration types
if found {
return instance, true
}
scopedInstances, hasParsleyContext := ctx.Value(parsleyContext).(map[uint64]interface{})
scopedInstances, hasParsleyContext := ctx.Value(ParsleyContext).(map[uint64]interface{})
if hasParsleyContext {
instance, found = scopedInstances[id]
if found {
Expand All @@ -69,7 +64,7 @@ func (b *InstanceBag) KeepInstance(ctx context.Context, registration types.Servi
}
}
case types.LifetimeScoped:
scopedInstances, hasParsleyContext := ctx.Value(parsleyContext).(map[uint64]interface{})
scopedInstances, hasParsleyContext := ctx.Value(ParsleyContext).(map[uint64]interface{})
if hasParsleyContext {
scopedInstances[id] = instance
}
Expand Down
3 changes: 1 addition & 2 deletions internal/tests/registry_register_multiple_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tests
import (
"context"
"errors"
"github.com/matzefriedrich/parsley/internal/core"
"github.com/matzefriedrich/parsley/pkg/registration"
"github.com/matzefriedrich/parsley/pkg/resolving"
"github.com/matzefriedrich/parsley/pkg/types"
Expand Down Expand Up @@ -115,7 +114,7 @@ func Test_Registry_register_multiple_types_mixed_lifetime_scopes_2(t *testing.T)
}

r := resolving.NewResolver(sut)
scope := core.NewScopedContext(context.Background())
scope := resolving.NewScopedContext(context.Background())
resolvedServices1, err := resolving.ResolveRequiredServices[multiFoo](r, scope)
resolvedServices2, _ := resolving.ResolveRequiredServices[multiFoo](r, scope)

Expand Down
9 changes: 4 additions & 5 deletions internal/tests/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"reflect"
"testing"

"github.com/matzefriedrich/parsley/internal/core"
"github.com/matzefriedrich/parsley/pkg/registration"
"github.com/matzefriedrich/parsley/pkg/resolving"

Expand Down Expand Up @@ -42,7 +41,7 @@ func Test_Registry_NewResolver_resolve_type_with_dependencies(t *testing.T) {

// Assert
r := resolving.NewResolver(sut)
parsleyContext := core.NewScopedContext(context.Background())
parsleyContext := resolving.NewScopedContext(context.Background())
resolved, _ := resolving.ResolveRequiredService[FooConsumer](r, parsleyContext)
assert.NotNil(t, resolved)

Expand All @@ -62,7 +61,7 @@ func Test_Registry_NewResolver_resolve_scoped_from_same_context_must_be_return_s

// Assert
r := resolving.NewResolver(sut)
parsleyContext := core.NewScopedContext(context.Background())
parsleyContext := resolving.NewScopedContext(context.Background())
consumer1, _ := resolving.ResolveRequiredService[FooConsumer](r, parsleyContext)
assert.NotNil(t, consumer1)

Expand All @@ -87,12 +86,12 @@ func Test_Registry_NewResolver_resolve_scoped_from_different_context_must_be_ret
// Assert
r := resolving.NewResolver(sut)

parsleyContext1 := core.NewScopedContext(context.Background())
parsleyContext1 := resolving.NewScopedContext(context.Background())
consumer1, _ := resolving.ResolveRequiredService[FooConsumer](r, parsleyContext1)

assert.NotNil(t, consumer1)

parsleyContext2 := core.NewScopedContext(context.Background())
parsleyContext2 := resolving.NewScopedContext(context.Background())
consumer2, _ := resolving.ResolveRequiredService[FooConsumer](r, parsleyContext2)
assert.NotNil(t, consumer2)

Expand Down
3 changes: 1 addition & 2 deletions internal/tests/resolver_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"context"
"github.com/matzefriedrich/parsley/internal/core"
"github.com/matzefriedrich/parsley/pkg/registration"
"github.com/matzefriedrich/parsley/pkg/resolving"
"github.com/matzefriedrich/parsley/pkg/types"
Expand All @@ -19,7 +18,7 @@ func Test_Resolver_ResolveRequiredService_factory_function_receives_current_reso

r := resolving.NewResolver(sut)

ctx := core.NewScopedContext(context.Background())
ctx := resolving.NewScopedContext(context.Background())

// Act
serviceFactory, _ := resolving.ResolveRequiredService[FactoryService](r, ctx)
Expand Down
3 changes: 1 addition & 2 deletions internal/tests/resolver_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"context"
"github.com/matzefriedrich/parsley/internal/core"
"github.com/matzefriedrich/parsley/pkg/registration"
"github.com/matzefriedrich/parsley/pkg/resolving"
"github.com/stretchr/testify/assert"
Expand All @@ -20,7 +19,7 @@ func Test_Resolver_ResolveWithOptions_inject_unregistered_service_instance(t *te
// Assert
r := resolving.NewResolver(sut)

parsleyContext := core.NewScopedContext(context.Background())
parsleyContext := resolving.NewScopedContext(context.Background())
consumers, _ := r.ResolveWithOptions(parsleyContext, registration.ServiceType[FooConsumer](), resolving.WithInstance[Foo](NewFoo()))
assert.Equal(t, 1, len(consumers))

Expand Down
3 changes: 1 addition & 2 deletions internal/tests/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"context"
"github.com/matzefriedrich/parsley/internal/core"
"github.com/matzefriedrich/parsley/pkg/registration"
"github.com/matzefriedrich/parsley/pkg/resolving"
"github.com/matzefriedrich/parsley/pkg/types"
Expand All @@ -19,7 +18,7 @@ func Test_Resolver_Resolve_returns_err_if_circular_dependency_detected(t *testin

r := resolving.NewResolver(registry)

scope := core.NewScopedContext(context.Background())
scope := resolving.NewScopedContext(context.Background())

// Act
_, err := r.Resolve(scope, registration.ServiceType[Foo0]())
Expand Down
11 changes: 11 additions & 0 deletions pkg/resolving/scope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package resolving

import (
"context"
"github.com/matzefriedrich/parsley/internal/core"
)

func NewScopedContext(ctx context.Context) context.Context {
instances := make(map[uint64]interface{})
return context.WithValue(ctx, core.ParsleyContext, instances)
}

0 comments on commit 2fd6730

Please sign in to comment.