Skip to content

Commit

Permalink
Added support for nested types
Browse files Browse the repository at this point in the history
  • Loading branch information
hugener committed Mar 29, 2024
1 parent 5a31160 commit fdde1ff
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static string GetUniqueName(string baseName, IInjectionNode? parentCreati

public static string GetVariableNameForType(DefiniteType type)
{
const string Empty = "";
const string Dot = ".";

var name = type.Name.AsSpan();
if (IsInterfaceName(name))
{
Expand All @@ -51,14 +54,14 @@ public static string GetVariableNameForType(DefiniteType type)
name = name.Slice(0, name.Length - arraySign.Length);
}

return name.ToString().Uncapitalize() + arrayName;
return name.ToString().Replace(Dot, Empty).Uncapitalize() + arrayName;
case DefiniteBoundGenericType genericType:
break;
case NamedType namedType:
break;
}

return name.ToString().Uncapitalize();
return name.ToString().Replace(Dot, Empty).Uncapitalize();
}

public static string GetFactoryMethodName(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,14 @@ private static (string Name, string Namespace, bool IsShortNameAlias) GetName(IT
return (typeSymbol.ToDisplayString(NameQualifiedTypeFormat), string.Empty, true);
}

return (typeSymbol.Name, TypeHelper.GetNamespace(typeSymbol.ContainingNamespace), false);
var format = new SymbolDisplayFormat(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypes,
genericsOptions: SymbolDisplayGenericsOptions.None,
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers | SymbolDisplayMiscellaneousOptions.UseSpecialTypes);

var name = typeSymbol.ToDisplayString(format);
return (name, TypeHelper.GetNamespace(typeSymbol.ContainingNamespace), false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ public ResolveRootFactory(
this.interfaceSingleInstancePerFactory,
overrideableNewImplementationForResolveRoot,
constructedChildForResolveRoot,
CreateMultipleImplementationForEnumerable());
CreateMultipleImplementationForEnumerable(),
new global::AllFeaturesSuccess.NestingTypes.NestedConsumer(new global::AllFeaturesSuccess.NestingTypes.Nestee.Nested()));
this.lifecycleHandler.TryAdd(resolveRootResult, childLifecycleHandler);
return new global::Sundew.Injection.Constructed<global::AllFeaturesSuccess.IResolveRoot>(resolveRootResult, childLifecycleHandler);
}
Expand Down
1 change: 1 addition & 0 deletions Source/Sundew.Injection.Testing/TestProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public static class TestProjects
{
public static Project AllFeatureSuccess = new Project(@"TestProjects/AllFeaturesSuccess");
public static Project TestPlayground = new Project(@"TestProjects/TestPlayground");

public class Project
{
Expand Down
30 changes: 30 additions & 0 deletions Source/Sundew.Injection.Tests/Playground/NestedTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Sundew.Injection.Tests.Playground;

extern alias sbt;
using FluentAssertions.Execution;
using sbt::Sundew.Base.Text;
using Sundew.Injection.Generator.Stages.InjectionDefinitionStage;
using Sundew.Injection.Testing;

[TestFixture]
public class NestedTests
{
[Test]
public void T()
{
var compilation = TestProjects.TestPlayground.FromCurrentDirectory.Value;
var demoModuleDeclaration = compilation.GetTypeByMetadataName("TestPlayground.FactoryDeclaration");
if (demoModuleDeclaration == null)
{
Assert.Fail($"Could not find FactoryDeclaration. Compilation had: {compilation.GetDiagnostics().Length} diagnostics");
throw new NotImplementedException("Assert.Fail is marked as throws.");
}

var injectionDefinitionSemanticModel = compilation.GetSemanticModel(demoModuleDeclaration.DeclaringSyntaxReferences.First().SyntaxTree, true);
var injectionDefinition = InjectionDefinitionProvider.GetInjectionDefinition(injectionDefinitionSemanticModel, CancellationToken.None);
if (!injectionDefinition.IsSuccess)
{
throw new AssertionFailedException($"InjectionDefinition should have been successful, but failed with errors: {injectionDefinition.Error.JoinToString((builder, item) => builder.Append(item), ", ")}");
}
}
}
22 changes: 13 additions & 9 deletions Source/Sundew.Injection.Tests/Sundew.Injection.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces"/>
<PackageReference Include="Disposal.Interfaces"/>
<PackageReference Include="Initialization.Interfaces"/>
<PackageReference Include="FluentAssertions"/>
<PackageReference Include="Microsoft.CodeAnalysis"/>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="NUnit"/>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Disposal.Interfaces" />
<PackageReference Include="Initialization.Interfaces" />
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.CodeAnalysis" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="NUnit.Analyzers">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -30,11 +30,11 @@
<PackageReference Include="Sundew.Base.Collections">
<Aliases>sbc</Aliases>
</PackageReference>
<PackageReference Include="Sundew.Base.Primitives"/>
<PackageReference Include="Sundew.Base.Primitives" />
<PackageReference Include="Sundew.Base.Text">
<Aliases>sbt</Aliases>
</PackageReference>
<PackageReference Include="Sundew.Testing.CodeAnalysis"/>
<PackageReference Include="Sundew.Testing.CodeAnalysis" />
</ItemGroup>

<ItemGroup>
Expand All @@ -44,4 +44,8 @@
<ProjectReference Include="..\TestProjects\AllFeaturesSuccessDependency\AllFeaturesSuccessDependency.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Playground\" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions Source/Sundew.Injection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AllFeaturesSuccessDependenc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sundew.Injection.PerformanceTests", "Sundew.Injection.PerformanceTests\Sundew.Injection.PerformanceTests.csproj", "{EA549FB4-6F3C-479A-B37F-0C2DC8D4EFD7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestPlayground", "TestProjects\TestPlayground\TestPlayground.csproj", "{3F7E2D84-D29C-47A4-B8B0-A31344CDB685}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -157,6 +159,12 @@ Global
{EA549FB4-6F3C-479A-B37F-0C2DC8D4EFD7}.DebugTests|Any CPU.Build.0 = DebugTests|Any CPU
{EA549FB4-6F3C-479A-B37F-0C2DC8D4EFD7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA549FB4-6F3C-479A-B37F-0C2DC8D4EFD7}.Release|Any CPU.Build.0 = Release|Any CPU
{3F7E2D84-D29C-47A4-B8B0-A31344CDB685}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F7E2D84-D29C-47A4-B8B0-A31344CDB685}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F7E2D84-D29C-47A4-B8B0-A31344CDB685}.DebugTests|Any CPU.ActiveCfg = Debug|Any CPU
{3F7E2D84-D29C-47A4-B8B0-A31344CDB685}.DebugTests|Any CPU.Build.0 = Debug|Any CPU
{3F7E2D84-D29C-47A4-B8B0-A31344CDB685}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F7E2D84-D29C-47A4-B8B0-A31344CDB685}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -173,6 +181,7 @@ Global
{02472129-7EAE-4A4B-B98B-E04026D6DD30} = {2D0EABCE-E506-436E-8483-F598967B61AC}
{3A168BE4-80C2-49C2-89A2-3870CEABE9DB} = {9E41F070-F91D-4526-A6C9-AB3F454D3A8A}
{EA549FB4-6F3C-479A-B37F-0C2DC8D4EFD7} = {88F08C53-01F8-428F-BA00-B28B1DFEA880}
{3F7E2D84-D29C-47A4-B8B0-A31344CDB685} = {9E41F070-F91D-4526-A6C9-AB3F454D3A8A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BC13742C-AB82-49EE-988E-1FF59BAF1BB2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="System.Collections.Immutable" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Disposal.Interfaces" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Disposal.Interfaces" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Initialization.Interfaces" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace AllFeaturesSuccess.NestingTypes;

using System;

public class NestedConsumer : IPrint
{
public NestedConsumer(Nestee.Nested nested)
{

}

public void PrintMe(int indent)
{
Console.WriteLine(new string(' ', indent) + this.GetType().Name);
}
}
8 changes: 8 additions & 0 deletions Source/TestProjects/AllFeaturesSuccess/NestingTypes/Nestee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AllFeaturesSuccess.NestingTypes;

public class Nestee
{
public class Nested
{
}
}
12 changes: 9 additions & 3 deletions Source/TestProjects/AllFeaturesSuccess/ResolveRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
using System.Collections.Generic;
using AllFeaturesSuccess.ChildFactory;
using AllFeaturesSuccess.MultipleImplementations;
using AllFeaturesSuccess.NestingTypes;
using AllFeaturesSuccess.OverridableNew;

public class ResolveRoot : global::AllFeaturesSuccess.IResolveRoot
{
private readonly OverrideableNewImplementation overrideableNewImplementation;
private readonly ConstructedChild constructedChild;
private readonly IEnumerable<IMultipleImplementationForEnumerable> multipleImplementationForEnumerables;
private readonly NestedConsumer nestingConsumer;

public ResolveRoot(
global::AllFeaturesSuccess.InterfaceImplementationBindings.IIntercepted intercepted,
global::AllFeaturesSuccess.SingleInstancePerFactory.IInterfaceSingleInstancePerFactory interfaceSingleInstancePerFactory,
OverrideableNewImplementation overrideableNewImplementation,
ConstructedChild constructedChild,
IEnumerable<IMultipleImplementationForEnumerable> multipleImplementationForEnumerables)
IEnumerable<IMultipleImplementationForEnumerable> multipleImplementationForEnumerables,
NestedConsumer nestingConsumer)
{
this.Intercepted = intercepted;
this.InterfaceSingleInstancePerFactory = interfaceSingleInstancePerFactory;
this.overrideableNewImplementation = overrideableNewImplementation;
this.constructedChild = constructedChild;
this.multipleImplementationForEnumerables = multipleImplementationForEnumerables;
this.Intercepted = intercepted;
this.InterfaceSingleInstancePerFactory = interfaceSingleInstancePerFactory;
this.nestingConsumer = nestingConsumer;
}

public global::AllFeaturesSuccess.InterfaceImplementationBindings.IIntercepted Intercepted { get; }
Expand All @@ -41,5 +45,7 @@ public void PrintMe(int indent)
{
multipleImplementationForEnumerable.PrintMe(indent + 2);
}

this.nestingConsumer.PrintMe(indent + 2);
}
}
22 changes: 10 additions & 12 deletions Source/TestProjects/NetStandardLibrarySuccess/FactoryDeclaration.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@

namespace NetStandardLibrarySuccess
{
using System.Threading.Tasks;
using Sundew.Injection;
namespace NetStandardLibrarySuccess;

public class FactoryDeclaration : IInjectionDeclaration
{
public void Configure(IInjectionBuilder injectionBuilder)
{
injectionBuilder.CreateFactory<T>();
}
}
using Sundew.Injection;

public class T
public class FactoryDeclaration : IInjectionDeclaration
{
public void Configure(IInjectionBuilder injectionBuilder)
{
injectionBuilder.CreateFactory<T>();
}
}

public class T
{
}
11 changes: 11 additions & 0 deletions Source/TestProjects/TestPlayground/FactoryDeclaration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace TestPlayground;

using Sundew.Injection;

public class FactoryDeclaration : IInjectionDeclaration
{
public void Configure(IInjectionBuilder injectionBuilder)
{
}
}
24 changes: 24 additions & 0 deletions Source/TestProjects/TestPlayground/TestPlayground.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="System.Collections.Immutable" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Disposal.Interfaces" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Initialization.Interfaces" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\..\Sundew.Injection\Sundew.Injection.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\AllFeaturesSuccessDependency\AllFeaturesSuccessDependency.csproj" />
</ItemGroup>

</Project>

0 comments on commit fdde1ff

Please sign in to comment.