Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 20, 2024
1 parent 76a3b0e commit e27f794
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/CompositionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CompositionCode Build(DependencyGraph graph)
ctx.Code.AppendLine($"return {buildTools.OnInjected(ctx, rootBlock.Current)};");
ctx.Code.AppendLines(ctx.LocalFunctionsCode.Lines);

var args = GetRootArgs(map.Values).ToImmutableArray();
var args = GetRootArgs(map.Values).ToList();
var processedRoot = root with
{
Lines = ctx.Code.Lines.ToImmutableArray(),
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/IInjections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ internal interface IInjections

void PropertyInjection(string targetName, BuildContext ctx, DpProperty property, Variable propertyVariable);

void MethodInjection(string targetName, BuildContext ctx, DpMethod method, ImmutableArray<Variable> methodArgs);
void MethodInjection(string targetName, BuildContext ctx, DpMethod method, IReadOnlyList<Variable> methodArgs);
}
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Core/Code/ImplementationCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public void Build(BuildContext ctx, in DpImplementation implementation)

private string CreateInstantiation(
BuildContext ctx,
ImmutableArray<Variable> constructorArgs,
ImmutableArray<(Variable RequiredVariable, DpField RequiredField)>.Builder requiredFields,
ImmutableArray<(Variable RequiredVariable, DpProperty RequiredProperty)>.Builder requiredProperties)
IReadOnlyCollection<Variable> constructorArgs,
IReadOnlyCollection<(Variable RequiredVariable, DpField RequiredField)> requiredFields,
IReadOnlyCollection<(Variable RequiredVariable, DpProperty RequiredProperty)> requiredProperties)
{
var code = new StringBuilder();
var variable = ctx.Variable;
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/Injections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public void PropertyInjection(string targetName, BuildContext ctx, DpProperty p
ctx.Code.AppendLine($"{targetName}.{property.Property.Name} = {ctx.BuildTools.OnInjected(ctx, propertyVariable)};");
}

public void MethodInjection(string targetName, BuildContext ctx, DpMethod method, ImmutableArray<Variable> methodArgs)
public void MethodInjection(string targetName, BuildContext ctx, DpMethod method, IReadOnlyList<Variable> methodArgs)
{
var args = new List<string>();
for (var index = 0; index < methodArgs.Length; index++)
for (var index = 0; index < methodArgs.Count; index++)
{
var variable = methodArgs[index];
if (index < method.Parameters.Length)
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/ResolverInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Pure.DI.Core.Code;
internal record ResolverInfo(
int Id,
ITypeSymbol Type,
ImmutableArray<Root> Roots)
IReadOnlyCollection<Root> Roots)
{
public string ClassName => $"{Names.ResolverClassName}_{Id:0000}";
}
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/ResolversBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public IEnumerable<ResolverInfo> Build(RootContext ctx) =>
.Where(i => !ReferenceEquals(i.Injection.Tag, MdTag.ContextTag))
.Where(i => typeResolver.Resolve(ctx.Setup, i.Injection.Type).TypeArgs.Count == 0)
.GroupBy(i => i.Injection.Type, SymbolEqualityComparer.Default)
.Select((i, id) => new ResolverInfo(id, (ITypeSymbol)i.Key!, i.ToImmutableArray()));
.Select((i, id) => new ResolverInfo(id, (ITypeSymbol)i.Key!, i.ToList()));
}
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/TypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private TypeDescription Resolve(MdSetup setup, ITypeSymbol type, ITypeParameterS
args.AddRange(item.description.TypeArgs);
}

description = new TypeDescription($"({string.Join(", ", elements)})", args.Distinct().ToImmutableArray(), typeParam);
description = new TypeDescription($"({string.Join(", ", elements)})", args.Distinct().ToList(), typeParam);
}
break;

Expand All @@ -60,7 +60,7 @@ private TypeDescription Resolve(MdSetup setup, ITypeSymbol type, ITypeParameterS
}

var name = string.Join("", namedTypeSymbol.ToDisplayParts().TakeWhile(i => i.ToString() != "<"));
description = new TypeDescription($"{name}<{string.Join(", ", types)}>", args.Distinct().ToImmutableArray(), typeParam);
description = new TypeDescription($"{name}<{string.Join(", ", types)}>", args.Distinct().ToList(), typeParam);
}
break;

Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/DependenciesToVariablesWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public DependenciesToVariablesWalker(ICollection<Variable> variables)
.ToDictionary(i => i.Key, i => new LinkedList<Variable>(i));
}

public ImmutableArray<Variable> GetResult()
public IReadOnlyList<Variable> GetResult()
{
var result = _resultBuilder.ToImmutable();
var result = _resultBuilder.ToList();
_resultBuilder.Clear();
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/DependencyGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Pure.DI.Core;

using Injection = Models.Injection;
using Injection = Injection;

internal sealed class DependencyGraphBuilder(
IEnumerable<IBuilder<MdSetup, IEnumerable<DependencyNode>>> dependencyNodeBuilders,
Expand Down Expand Up @@ -341,7 +341,7 @@ public IEnumerable<DependencyNode> TryBuild(
edges.Add(dependency);
}

entries.Add(new GraphEntry<DependencyNode, Dependency>(node.Node, edges.ToImmutableArray()));
entries.Add(new GraphEntry<DependencyNode, Dependency>(node.Node, edges));
}

var graph = new Graph<DependencyNode, Dependency>(entries);
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/DependencyGraphLocationsWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal sealed class DependencyGraphLocationsWalker(in Injection injection)
private readonly Injection _injection = injection;
private readonly ImmutableArray<Location>.Builder _locationsBuilder = ImmutableArray.CreateBuilder<Location>();

public ImmutableArray<Location> Locations => _locationsBuilder.ToImmutableArray();
public IReadOnlyCollection<Location> Locations => _locationsBuilder.ToList();

public override void VisitInjection(
in Unit ctx,
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.Core/Core/Disposables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ internal static class Disposables
public static IDisposable Create(Action action) => new DisposableAction(action);

public static IDisposable Create(params IDisposable[] disposables) =>
Create(disposables.ToImmutableArray());
Create(disposables.ToList());

public static IDisposable Create(in ImmutableArray<IDisposable> disposables) =>
public static IDisposable Create(in IReadOnlyCollection<IDisposable> disposables) =>
new CompositeDisposable(disposables);

public static IDisposable Create(IEnumerable<IDisposable> disposables) =>
Expand Down Expand Up @@ -64,9 +64,9 @@ public override int GetHashCode() =>
_key != null ? _key.GetHashCode() : 0;
}

private class CompositeDisposable(in ImmutableArray<IDisposable> disposables) : IDisposable
private class CompositeDisposable(in IReadOnlyCollection<IDisposable> disposables) : IDisposable
{
private readonly ImmutableArray<IDisposable> _disposables = disposables;
private readonly IReadOnlyCollection<IDisposable> _disposables = disposables;
private int _counter;

public void Dispose()
Expand Down
12 changes: 3 additions & 9 deletions src/Pure.DI.Core/Core/Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ internal sealed class Graph<TVertex, TEdge> : IGraph<TVertex, TEdge>
where TEdge : IEdge<TVertex>
where TVertex : notnull
{
private readonly List<GraphEntry<TVertex, TEdge>> _entries = [];
private readonly Dictionary<TVertex, GraphEntry<TVertex, TEdge>> _inOutEdges;
private readonly Dictionary<TVertex, GraphEntry<TVertex, TEdge>> _outInEdges;
private readonly List<TEdge> _edges = [];
Expand All @@ -20,7 +19,6 @@ public Graph(
var outInEdges = new Dictionary<TVertex, List<TEdge>>(comparer);
foreach (var entry in entries)
{
_entries.Add(entry);
_inOutEdges.Add(entry.Target, entry);
foreach (var edge in entry.Edges)
{
Expand All @@ -35,7 +33,9 @@ public Graph(
}
}

_outInEdges = outInEdges.ToDictionary(i => i.Key, i => new GraphEntry<TVertex, TEdge>(i.Key, i.Value));
_outInEdges = outInEdges.ToDictionary(
i => i.Key,
i => new GraphEntry<TVertex, TEdge>(i.Key, i.Value));
}

public IEnumerable<TVertex> Vertices => _inOutEdges.Keys;
Expand Down Expand Up @@ -65,10 +65,4 @@ public bool TryGetOutEdges(in TVertex source, out IReadOnlyCollection<TEdge> edg
edges = ImmutableArray<TEdge>.Empty;
return false;
}

public IGraph<TVertex, TEdge> Consolidate(ISet<TVertex> vertices) =>
new Graph<TVertex, TEdge>(
_entries.Where(i =>
vertices.Contains(i.Target)
|| vertices.Intersect(i.Edges.Select(j => j.Source)).Any()));
}
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/InitializersWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void VisitMethod(in BuildContext ctx, in DpMethod method)
base.VisitMethod(in ctx, in method);
var curCtx = ctx;
var curMethod = method;
var curVariables = _variables.ToImmutableArray();
var curVariables = _variables.ToList();
_actions.Add(new (() => injections.MethodInjection(variableName, curCtx, curMethod, curVariables), curMethod.Ordinal));
_variables.Clear();
}
Expand Down
1 change: 0 additions & 1 deletion src/Pure.DI.Core/Core/InstanceDpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public InstanceDp Get(
setupAttributesBuilder.AddRange(setup.TagAttributes);
setupAttributesBuilder.AddRange(setup.TypeAttributes);
var setupAttributes = setupAttributesBuilder.MoveToImmutable();

var methods = new List<DpMethod>();
var fields = new List<DpField>();
var properties = new List<DpProperty>();
Expand Down
6 changes: 3 additions & 3 deletions tests/Pure.DI.IntegrationTests/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal static async Task<Result> RunAsync(this string setupCode, Options? opti
logs,
errors,
warnings,
dependencyGraphObserver.Values.ToImmutableArray(),
dependencyGraphObserver.Values,
string.Empty);
}

Expand Down Expand Up @@ -129,7 +129,7 @@ internal static async Task<Result> RunAsync(this string setupCode, Options? opti
logs,
errors,
warnings,
dependencyGraphObserver.Values.ToImmutableArray(),
dependencyGraphObserver.Values,
generatedCode);
}

Expand Down Expand Up @@ -166,7 +166,7 @@ internal static async Task<Result> RunAsync(this string setupCode, Options? opti
logs,
errors,
warnings,
dependencyGraphObserver.Values.ToImmutableArray(),
dependencyGraphObserver.Values,
generatedCode);

void StdOutReceived(object sender, DataReceivedEventArgs args)
Expand Down
4 changes: 2 additions & 2 deletions tests/Pure.DI.IntegrationTests/TuplesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public static void Main()

// Then
result.Success.ShouldBeFalse(result);
var errors = result.Logs.Where(i => i.Id == LogId.ErrorUnableToResolve).ToImmutableArray();
errors.Length.ShouldBe(1, result);
var errors = result.Logs.Where(i => i.Id == LogId.ErrorUnableToResolve).ToList();
errors.Count.ShouldBe(1, result);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace Pure.DI.UsageTests.Hints.OnNewInstanceHintScenario;

using System.Collections.Immutable;
using Shouldly;
using Xunit;

Expand Down

0 comments on commit e27f794

Please sign in to comment.