From d52d2d963eea603d23359cf1839e6da007ea91a9 Mon Sep 17 00:00:00 2001 From: Carlin St Pierre Date: Sat, 14 Apr 2018 13:48:25 +1000 Subject: [PATCH] Support types that have recursive template arguments with createImportsString --- source/poodinis/imports.d | 7 ++++--- test/poodinis/autowiretest.d | 17 +++++++++++++++++ test/poodinis/testclasses.d | 13 +++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/source/poodinis/imports.d b/source/poodinis/imports.d index 107b278..7ad5691 100644 --- a/source/poodinis/imports.d +++ b/source/poodinis/imports.d @@ -11,14 +11,15 @@ module poodinis.imports; +import std.meta; import std.traits; -public static string createImportsString(Type)() { +public static string createImportsString(Type, ParentTypeList...)() { string imports = `import ` ~ moduleName!Type ~ `;`; static if (__traits(compiles, TemplateArgsOf!Type)) { foreach(TemplateArgType; TemplateArgsOf!Type) { - static if (!isBuiltinType!TemplateArgType) { - imports ~= createImportsString!TemplateArgType; + static if (!isBuiltinType!TemplateArgType && staticIndexOf!(TemplateArgType, ParentTypeList) == -1) { + imports ~= createImportsString!(TemplateArgType, ParentTypeList, Type); } } } diff --git a/test/poodinis/autowiretest.d b/test/poodinis/autowiretest.d index 50450c3..0499912 100644 --- a/test/poodinis/autowiretest.d +++ b/test/poodinis/autowiretest.d @@ -9,6 +9,7 @@ import poodinis; import poodinis.test.testClasses; import std.exception; +import std.meta; version(unittest) { @@ -179,4 +180,20 @@ version(unittest) { assert(instance.intValue == 8); assert(instance.unrelated !is null); } + + // Test autowiring classes with recursive template parameters + unittest { + auto container = new shared DependencyContainer(); + container.register!CircularTemplateComponentA; + container.register!CircularTemplateComponentB; + + auto componentA = container.resolve!CircularTemplateComponentA; + auto componentB = container.resolve!CircularTemplateComponentB; + + assert(componentA !is null); + assert(componentB !is null); + + assert(componentA.instance is componentB); + assert(componentB.instance is componentA); + } } diff --git a/test/poodinis/testclasses.d b/test/poodinis/testclasses.d index 10956e6..8c7966b 100644 --- a/test/poodinis/testclasses.d +++ b/test/poodinis/testclasses.d @@ -630,4 +630,17 @@ version(unittest) { return 0; } } + + class TemplatedComponent(T) { + @Autowire + T instance; + } + + class CircularTemplateComponentA : TemplatedComponent!CircularTemplateComponentB { + + } + + class CircularTemplateComponentB : TemplatedComponent!CircularTemplateComponentA { + + } }