Skip to content

Commit

Permalink
Allows registration of struct dependencies (#18)
Browse files Browse the repository at this point in the history
* Allows registration of struct dependencies
  • Loading branch information
matzefriedrich authored Sep 1, 2024
1 parent b8d5f56 commit fd95efe
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.8.3] - 2024-09-01

### Fixed

* Allows registration of (immutable) struct dependencies


## [v0.8.2] - 2024-08-29

### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package registration

import (
"context"
"github.com/matzefriedrich/parsley/pkg/registration"
"github.com/matzefriedrich/parsley/pkg/resolving"
"github.com/matzefriedrich/parsley/pkg/types"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_Register_struct_dependency(t *testing.T) {

// Arrange
registry := registration.NewServiceRegistry()

config := someConfig{b: true}
registryErr := registration.RegisterInstance[someConfig](registry, config)

resolver := resolving.NewResolver(registry)
ctx := resolving.NewScopedContext(context.Background())

// Act
actual, err := resolving.ResolveRequiredService[someConfig](resolver, ctx)

// Arrange
assert.NoError(t, registryErr)
assert.NoError(t, err)
assert.True(t, actual.b)
}

func Test_Register_service_with_struct_dependency(t *testing.T) {

// Arrange
registry := registration.NewServiceRegistry()
registry.Register(newAppWithStructDependency, types.LifetimeTransient)

config := someConfig{b: true}
registryErr := registration.RegisterInstance[someConfig](registry, config)

resolver := resolving.NewResolver(registry)
ctx := resolving.NewScopedContext(context.Background())

// Act
actual, err := resolving.ResolveRequiredService[*appWithStructDependency](resolver, ctx)

// Arrange
assert.NoError(t, registryErr)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.True(t, actual.config.b)
}

func Test_Register_immutable_service_with_struct_dependency(t *testing.T) {

// Arrange
registry := registration.NewServiceRegistry()
registry.Register(newImmutableAppWithStructDependency, types.LifetimeTransient)

config := someConfig{b: true}
registryErr := registration.RegisterInstance[someConfig](registry, config)

resolver := resolving.NewResolver(registry)
ctx := resolving.NewScopedContext(context.Background())

// Act
actual, err := resolving.ResolveRequiredService[appWithStructDependency](resolver, ctx)

// Arrange
assert.NoError(t, registryErr)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.True(t, actual.config.b)
}

type someConfig struct {
b bool
}

type appWithStructDependency struct {
config someConfig
}

func newAppWithStructDependency(config someConfig) *appWithStructDependency {
return &appWithStructDependency{config: config}
}

func newImmutableAppWithStructDependency(config someConfig) appWithStructDependency {
return appWithStructDependency{config: config}
}
1 change: 1 addition & 0 deletions pkg/registration/activator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func CreateServiceActivatorFrom[T any](instance T) (func() T, error) {
case reflect.Func:
case reflect.Interface:
case reflect.Pointer:
case reflect.Struct:
default:
return nil, types.NewRegistryError(types.ErrorActivatorFunctionInvalidReturnType)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/registration/service_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func CreateServiceRegistration(activatorFunc any, lifetimeScope types.LifetimeSc
fallthrough
case reflect.Pointer:
fallthrough
case reflect.Struct:
fallthrough
case reflect.Interface:
requiredTypes := info.ParameterTypes()
return newServiceRegistration(serviceType, lifetimeScope, value, requiredTypes...), nil
Expand Down
1 change: 1 addition & 0 deletions pkg/resolving/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func ResolveRequiredServices[T any](resolver types.Resolver, ctx context.Context
case reflect.Interface:
case reflect.Pointer:
case reflect.Slice:
case reflect.Struct:
default:
return []T{}, types.NewResolverError(types.ErrorActivatorFunctionInvalidReturnType)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/service_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func ServiceTypeFrom(t reflect.Type) ServiceType {
case reflect.Slice:
t = t.Elem()
isList = true
case reflect.Struct:
default:
panic("unsupported type: " + t.String())
}
Expand Down

0 comments on commit fd95efe

Please sign in to comment.