Autoformat code

This commit is contained in:
Mike Bierlee 2023-03-07 00:46:07 +03:00
parent dfac28102e
commit b8caa00be1
2 changed files with 45 additions and 29 deletions

View file

@ -140,7 +140,7 @@ public void autowire(Type)(shared(DependencyContainer) container, Type instance)
} }
private void printDebugAutowiringCandidate(TypeInfo candidateInstanceType, private void printDebugAutowiringCandidate(TypeInfo candidateInstanceType,
void* candidateInstanceAddress, TypeInfo instanceType, void* instanceAddress, string member) void* candidateInstanceAddress, TypeInfo instanceType, void* instanceAddress, string member)
{ {
debug debug
{ {
@ -150,7 +150,7 @@ private void printDebugAutowiringCandidate(TypeInfo candidateInstanceType,
} }
private void printDebugAutowiringArray(TypeInfo superTypeInfo, private void printDebugAutowiringArray(TypeInfo superTypeInfo,
TypeInfo instanceType, void* instanceAddress, string member) TypeInfo instanceType, void* instanceAddress, string member)
{ {
debug debug
{ {
@ -160,7 +160,7 @@ private void printDebugAutowiringArray(TypeInfo superTypeInfo,
} }
private void autowireMember(string member, size_t memberIndex, Type)( private void autowireMember(string member, size_t memberIndex, Type)(
shared(DependencyContainer) container, Type instance) shared(DependencyContainer) container, Type instance)
{ {
foreach (attribute; __traits(getAttributes, Type.tupleof[memberIndex])) foreach (attribute; __traits(getAttributes, Type.tupleof[memberIndex]))
{ {
@ -186,7 +186,7 @@ private void autowireMember(string member, size_t memberIndex, Type)(
} }
private void injectInstance(string member, size_t memberIndex, QualifierType, Type)( private void injectInstance(string member, size_t memberIndex, QualifierType, Type)(
shared(DependencyContainer) container, Type instance) shared(DependencyContainer) container, Type instance)
{ {
if (instance.tupleof[memberIndex] is null) if (instance.tupleof[memberIndex] is null)
{ {
@ -196,18 +196,18 @@ private void injectInstance(string member, size_t memberIndex, QualifierType, Ty
static if (isDynamicArray!MemberType) static if (isDynamicArray!MemberType)
{ {
injectMultipleInstances!(member, memberIndex, isOptional, MemberType)(container, injectMultipleInstances!(member, memberIndex, isOptional, MemberType)(container,
instance); instance);
} }
else else
{ {
injectSingleInstance!(member, memberIndex, isOptional, MemberType, QualifierType)(container, injectSingleInstance!(member, memberIndex, isOptional, MemberType, QualifierType)(container,
instance); instance);
} }
} }
} }
private void injectMultipleInstances(string member, size_t memberIndex, private void injectMultipleInstances(string member, size_t memberIndex,
bool isOptional, MemberType, Type)(shared(DependencyContainer) container, Type instance) bool isOptional, MemberType, Type)(shared(DependencyContainer) container, Type instance)
{ {
alias MemberElementType = ElementType!MemberType; alias MemberElementType = ElementType!MemberType;
static if (isOptional) static if (isOptional)
@ -227,8 +227,8 @@ private void injectMultipleInstances(string member, size_t memberIndex,
} }
private void injectSingleInstance(string member, size_t memberIndex, private void injectSingleInstance(string member, size_t memberIndex,
bool isOptional, MemberType, QualifierType, Type)( bool isOptional, MemberType, QualifierType, Type)(
shared(DependencyContainer) container, Type instance) shared(DependencyContainer) container, Type instance)
{ {
debug (poodinisVerbose) debug (poodinisVerbose)
{ {
@ -241,7 +241,7 @@ private void injectSingleInstance(string member, size_t memberIndex,
static if (!is(QualifierType == UseMemberType)) static if (!is(QualifierType == UseMemberType))
{ {
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType,
assignNewInstance, isOptional)(container); assignNewInstance, isOptional)(container);
debug (poodinisVerbose) debug (poodinisVerbose)
{ {
qualifiedInstanceType = typeid(QualifierType); qualifiedInstanceType = typeid(QualifierType);
@ -250,7 +250,7 @@ private void injectSingleInstance(string member, size_t memberIndex,
else else
{ {
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, qualifiedInstance = createOrResolveInstance!(MemberType, MemberType,
assignNewInstance, isOptional)(container); assignNewInstance, isOptional)(container);
} }
instance.tupleof[memberIndex] = qualifiedInstance; instance.tupleof[memberIndex] = qualifiedInstance;
@ -258,18 +258,18 @@ private void injectSingleInstance(string member, size_t memberIndex,
debug (poodinisVerbose) debug (poodinisVerbose)
{ {
printDebugAutowiringCandidate(qualifiedInstanceType, printDebugAutowiringCandidate(qualifiedInstanceType,
&qualifiedInstance, typeid(Type), &instance, member); &qualifiedInstance, typeid(Type), &instance, member);
} }
} }
private QualifierType createOrResolveInstance(MemberType, QualifierType, private QualifierType createOrResolveInstance(MemberType, QualifierType,
bool createNew, bool isOptional)(shared(DependencyContainer) container) bool createNew, bool isOptional)(shared(DependencyContainer) container)
{ {
static if (createNew) static if (createNew)
{ {
auto instanceFactory = new InstanceFactory(); auto instanceFactory = new InstanceFactory();
instanceFactory.factoryParameters = InstanceFactoryParameters(typeid(MemberType), instanceFactory.factoryParameters = InstanceFactoryParameters(typeid(MemberType),
CreatesSingleton.no); CreatesSingleton.no);
return cast(MemberType) instanceFactory.getInstance(); return cast(MemberType) instanceFactory.getInstance();
} }
else else
@ -286,7 +286,7 @@ private QualifierType createOrResolveInstance(MemberType, QualifierType,
} }
private void injectValue(string member, size_t memberIndex, string key, bool mandatory, Type)( private void injectValue(string member, size_t memberIndex, string key, bool mandatory, Type)(
shared(DependencyContainer) container, Type instance) shared(DependencyContainer) container, Type instance)
{ {
alias MemberType = typeof(Type.tupleof[memberIndex]); alias MemberType = typeof(Type.tupleof[memberIndex]);
try try
@ -315,7 +315,7 @@ private void injectValue(string member, size_t memberIndex, string key, bool man
} }
private void printDebugValueInjection(TypeInfo instanceType, private void printDebugValueInjection(TypeInfo instanceType,
void* instanceAddress, string member, TypeInfo valueType, string key) void* instanceAddress, string member, TypeInfo valueType, string key)
{ {
debug debug
{ {
@ -340,16 +340,16 @@ class AutowiredRegistration(RegistrationType : Object) : Registration
private shared(DependencyContainer) container; private shared(DependencyContainer) container;
public this(TypeInfo registeredType, InstanceFactory instanceFactory, public this(TypeInfo registeredType, InstanceFactory instanceFactory,
shared(DependencyContainer) originatingContainer) shared(DependencyContainer) originatingContainer)
{ {
super(registeredType, typeid(RegistrationType), instanceFactory, originatingContainer); super(registeredType, typeid(RegistrationType), instanceFactory, originatingContainer);
} }
public override Object getInstance( public override Object getInstance(
InstantiationContext context = new AutowireInstantiationContext()) InstantiationContext context = new AutowireInstantiationContext())
{ {
enforce(!(originatingContainer is null), enforce(!(originatingContainer is null),
"The registration's originating container is null. There is no way to resolve autowire dependencies."); "The registration's originating container is null. There is no way to resolve autowire dependencies.");
RegistrationType instance = cast(RegistrationType) super.getInstance(context); RegistrationType instance = cast(RegistrationType) super.getInstance(context);

View file

@ -41,7 +41,7 @@ version (unittest)
auto componentD = new ComponentD(); auto componentD = new ComponentD();
container.autowire(componentD); container.autowire(componentD);
assert(componentD.privateComponentC is componentD.componentC, assert(componentD.privateComponentC is componentD.componentC,
"Autowire private dependency failed"); "Autowire private dependency failed");
} }
// Test autowiring will only happen once // Test autowiring will only happen once
@ -55,7 +55,7 @@ version (unittest)
container.autowire(componentD); container.autowire(componentD);
auto actualComponent = componentD.componentC; auto actualComponent = componentD.componentC;
assert(expectedComponent is actualComponent, assert(expectedComponent is actualComponent,
"Autowiring the second time wired a different instance"); "Autowiring the second time wired a different instance");
} }
// Test autowiring unregistered type // Test autowiring unregistered type
@ -64,7 +64,7 @@ version (unittest)
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto componentD = new ComponentD(); auto componentD = new ComponentD();
assertThrown!(ResolveException)(container.autowire(componentD), assertThrown!(ResolveException)(container.autowire(componentD),
"Autowiring unregistered type should throw ResolveException"); "Autowiring unregistered type should throw ResolveException");
} }
// Test autowiring member with non-autowire attribute does not autowire // Test autowiring member with non-autowire attribute does not autowire
@ -74,7 +74,7 @@ version (unittest)
auto componentE = new ComponentE(); auto componentE = new ComponentE();
container.autowire(componentE); container.autowire(componentE);
assert(componentE.componentC is null, assert(componentE.componentC is null,
"Autowiring should not occur for members with attributes other than @Autowire"); "Autowiring should not occur for members with attributes other than @Autowire");
} }
// Test autowire class with alias declaration // Test autowire class with alias declaration
@ -87,7 +87,7 @@ version (unittest)
container.autowire(componentDeclarationCocktail); container.autowire(componentDeclarationCocktail);
assert(componentDeclarationCocktail.componentA !is null, assert(componentDeclarationCocktail.componentA !is null,
"Autowiring class with non-assignable declarations failed"); "Autowiring class with non-assignable declarations failed");
} }
// Test autowire class with qualifier // Test autowire class with qualifier
@ -117,9 +117,9 @@ version (unittest)
container.autowire(bootstrapBootstrap); container.autowire(bootstrapBootstrap);
assert(bootstrapBootstrap.componentX is componentX, assert(bootstrapBootstrap.componentX is componentX,
"Autowiring class with multiple qualifiers failed"); "Autowiring class with multiple qualifiers failed");
assert(bootstrapBootstrap.componentC is componentC, assert(bootstrapBootstrap.componentC is componentC,
"Autowiring class with multiple qualifiers failed"); "Autowiring class with multiple qualifiers failed");
} }
// Test getting instance from autowired registration will autowire instance // Test getting instance from autowired registration will autowire instance
@ -129,9 +129,9 @@ version (unittest)
container.register!ComponentA; container.register!ComponentA;
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB),
new InstanceFactory(), container).initializeFactoryType().singleInstance(); new InstanceFactory(), container).initializeFactoryType().singleInstance();
auto instance = cast(ComponentB) registration.getInstance( auto instance = cast(ComponentB) registration.getInstance(
new AutowireInstantiationContext()); new AutowireInstantiationContext());
assert(instance.componentA !is null); assert(instance.componentA !is null);
} }
@ -161,7 +161,7 @@ version (unittest)
container.autowire(charlie); container.autowire(charlie);
assert(charlie.componentA !is regularComponentA, assert(charlie.componentA !is regularComponentA,
"Autowiring class with AssignNewInstance did not yield a different instance"); "Autowiring class with AssignNewInstance did not yield a different instance");
} }
// Test autowiring members from base class // Test autowiring members from base class
@ -222,4 +222,20 @@ version (unittest)
assert(componentA.instance is componentB); assert(componentA.instance is componentB);
assert(componentB.instance is componentA); assert(componentB.instance is componentA);
} }
// Test autowiring class where a method is marked with @Autowire does nothing
unittest
{
// It should also not show deprecation warning:
// Deprecation: `__traits(getAttributes)` may only be used for individual functions, not overload sets such as: `lala`
// the result of `__traits(getOverloads)` may be used to select the desired function to extract attributes from
auto container = new shared DependencyContainer();
container.register!AutowiredMethod;
auto instance = container.resolve!AutowiredMethod;
assert(instance !is null);
assert(instance.lala == 42);
assert(instance.lala(77) == 77);
}
} }