Skip to content

Commit

Permalink
Adds a protected access modifier for roots
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 6, 2024
1 parent 4c4c332 commit 0d1cee5
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion readme/auto-scoped.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ classDiagram
class Composition {
<<partial>>
+Program ProgramRoot
+Service SessionRoot
-Service SessionRoot
}
class Dependency {
+Dependency()
Expand Down
4 changes: 2 additions & 2 deletions readme/composition-root-kinds.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ classDiagram
namespace Pure.DI.UsageTests.Advanced.CompositionRootKindsScenario {
class Composition {
<<partial>>
+IDependency Dependency
~IDependency Dependency
+IService GetOtherService()
+IService GetRoot()
-IService GetRoot()
+ T ResolveᐸTᐳ()
+ T ResolveᐸTᐳ(object? tag)
+ object Resolve(Type type)
Expand Down
2 changes: 1 addition & 1 deletion readme/partial-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ classDiagram
namespace Pure.DI.UsageTests.Advanced.PartialClassScenario {
class Composition {
<<partial>>
+Service Root
~Service Root
+ T ResolveᐸTᐳ()
+ T ResolveᐸTᐳ(object? tag)
+ object Resolve(Type type)
Expand Down
19 changes: 12 additions & 7 deletions src/Pure.DI.Core/Components/Api.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,37 +1093,42 @@ internal enum RootKinds
/// <summary>
/// Specifies to use a <c>internal</c> access modifier for the root of the composition.
/// </summary>
Internal = 2,
Internal = 1 << 1,

/// <summary>
/// Specifies to use a <c>private</c> access modifier for the root of the composition.
/// </summary>
Private = 4,
Private = 1 << 2,

/// <summary>
/// Specifies to create a composition root as a property.
/// </summary>
Property = 8,
Property = 1 << 3,

/// <summary>
/// Specifies to create a composition root as a method.
/// </summary>
Method = 16,
Method = 1 << 4,

/// <summary>
/// Specifies to create a static root of the composition.
/// </summary>
Static = 32,
Static = 1 << 5,

/// <summary>
/// Specifies to create a partial root of the composition.
/// </summary>
Partial = 64,
Partial = 1 << 6,

/// <summary>
/// Specifies to create a exposed root of the composition.
/// </summary>
Exposed = 128
Exposed = 1 << 7,

/// <summary>
/// Specifies to use a <c>protected</c> access modifier for the root of the composition.
/// </summary>
Protected = 1 << 8,
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/ClassDiagramBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal sealed class ClassDiagramBuilder(
IBuilder<ContractsBuildContext, ISet<Injection>> injectionsBuilder,
IMarker marker,
ITypeResolver typeResolver,
IRootAccessModifierResolver rootAccessModifierResolver,
CancellationToken cancellationToken)
: IBuilder<CompositionCode, LinesBuilder>
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public LinesBuilder Build(CompositionCode composition)
compositionLines.AppendLine("<<partial>>");
foreach (var root in composition.Roots.OrderByDescending(i => i.IsPublic).ThenBy(i => i.Name))
{
compositionLines.AppendLine($"{(root.IsPublic ? "+" : "-")}{FormatRoot(setup, root)}");
compositionLines.AppendLine($"{Format(rootAccessModifierResolver.Resolve(root))}{FormatRoot(setup, root)}");
}

if (hasResolveMethods)
Expand Down Expand Up @@ -320,7 +321,6 @@ private string ResolveTypeName(MdSetup setup, ITypeSymbol typeSymbol)
private static string Format(Accessibility accessibility) =>
accessibility switch
{
Accessibility.NotApplicable => "",
Accessibility.Private => "-",
Accessibility.ProtectedAndInternal => "#",
Accessibility.Protected => "#",
Expand Down
22 changes: 14 additions & 8 deletions src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal sealed class RootMethodsBuilder(
ITypeResolver typeResolver,
[Tag(typeof(RootMethodsCommenter))] ICommenter<Root> rootCommenter,
IMarker marker,
IRootAccessModifierResolver rootAccessModifierResolver,
CancellationToken cancellationToken)
: IBuilder<CompositionCode, CompositionCode>
{
Expand Down Expand Up @@ -51,15 +52,20 @@ private void BuildRoot(CompositionCode composition, Root root)
rootArgsStr = $"({string.Join(", ", root.Args.Select(arg => $"{typeResolver.Resolve(composition.Source.Source, arg.InstanceType)} {arg.VariableDeclarationName}"))})";
buildTools.AddPureHeader(code);
}

var modifier = !root.IsPublic || (root.Kind & RootKinds.Private) == RootKinds.Private
? "private"
: (root.Kind & RootKinds.Internal) == RootKinds.Internal
? "internal"
: "public";


var name = new StringBuilder();
name.Append(modifier);
var accessModifier = rootAccessModifierResolver.Resolve(root) switch
{
Accessibility.Private => "private",
Accessibility.ProtectedAndInternal => "protected",
Accessibility.Protected => "protected",
Accessibility.Internal => "internal",
Accessibility.ProtectedOrInternal => "internal",
Accessibility.Public => "public",
_ => ""
};

name.Append(accessModifier);
if ((root.Kind & RootKinds.Static) == RootKinds.Static)
{
name.Append(" static");
Expand Down
6 changes: 6 additions & 0 deletions src/Pure.DI.Core/Core/IRootAccessModifierResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Pure.DI.Core;

internal interface IRootAccessModifierResolver
{
Accessibility Resolve(Root root);
}
15 changes: 15 additions & 0 deletions src/Pure.DI.Core/Core/RootAccessModifierResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Pure.DI.Core;

internal class RootAccessModifierResolver : IRootAccessModifierResolver
{
public Accessibility Resolve(Root root) =>
root.IsPublic switch
{
true when (root.Kind & RootKinds.Public) != 0 => Accessibility.Public,
true when (root.Kind & RootKinds.Internal) != 0 => Accessibility.Internal,
true when (root.Kind & RootKinds.Protected) != 0 => Accessibility.Protected,
true when (root.Kind & RootKinds.Private) != 0 => Accessibility.Private,
false => Accessibility.Private,
_ => Accessibility.Public
};
}
3 changes: 2 additions & 1 deletion src/Pure.DI.Core/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,6 @@ private void Setup() => DI.Setup()
.Bind("Injection").To<IdGenerator>()
.Bind().To<IdGenerator>()
.Bind().To<Registry<TT>>()
.Bind().To<Locks>();
.Bind().To<Locks>()
.Bind().To<RootAccessModifierResolver>();
}

0 comments on commit 0d1cee5

Please sign in to comment.