Skip to content

Commit 0b09fd2

Browse files
committed
Added basic array support for UndefinedGenericType and fix related unit tests
1 parent 6e18d00 commit 0b09fd2

File tree

14 files changed

+359
-234
lines changed

14 files changed

+359
-234
lines changed

src/NodeDev.Blazor/Components/GraphCanvas.razor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ private void OnNewNodeTypeSelected(NodeProvider.NodeSearchResult searchResult)
413413
if (PopupNodeConnection.Type is UndefinedGenericType) // can connect to anything except exec
414414
destination = destinations.FirstOrDefault(x => !x.Type.IsExec);
415415
else // can connect to anything that is assignable to the type
416-
destination = destinations.FirstOrDefault(x => PopupNodeConnection.Type.IsAssignableTo(x.Type, out _) || (x.Type is UndefinedGenericType && !PopupNodeConnection.Type.IsExec));
416+
destination = destinations.FirstOrDefault(x => PopupNodeConnection.Type.IsAssignableTo(x.Type, out _, out _) || (x.Type is UndefinedGenericType && !PopupNodeConnection.Type.IsExec));
417417

418418
// if we found a connection, connect them together
419419
if (destination != null)
@@ -459,9 +459,9 @@ private void OnNewOverloadSelected(Node.AlternateOverload overload)
459459
#region OnGenericTypeSelectionMenuAsked
460460

461461
private bool IsShowingGenericTypeSelection = false;
462-
private UndefinedGenericType? GenericTypeSelectionMenuGeneric;
462+
private string? GenericTypeSelectionMenuGeneric;
463463

464-
public void OnGenericTypeSelectionMenuAsked(GraphNodeModel nodeModel, UndefinedGenericType undefinedGenericType)
464+
public void OnGenericTypeSelectionMenuAsked(GraphNodeModel nodeModel, string undefinedGenericType)
465465
{
466466
PopupNode = nodeModel.Node;
467467
var p = Diagram.GetScreenPoint(nodeModel.Position.X, nodeModel.Position.Y) - Diagram.Container!.NorthWest;
@@ -478,7 +478,7 @@ private void OnGenericTypeSelected(TypeBase type)
478478
if (PopupNode == null || GenericTypeSelectionMenuGeneric == null)
479479
return;
480480

481-
GraphManagerService.PropagateNewGeneric(PopupNode, new Dictionary<UndefinedGenericType, TypeBase>() { [GenericTypeSelectionMenuGeneric] = type }, false, null, overrideInitialTypes: true);
481+
GraphManagerService.PropagateNewGeneric(PopupNode, new Dictionary<string, TypeBase>() { [GenericTypeSelectionMenuGeneric] = type }, false, null, overrideInitialTypes: true);
482482

483483
// Prefer updating the nodes directly instead of calling Graph.RaiseGraphChanged(true) to be sure it is called as soon as possible
484484
UpdateNodes(Graph.Nodes.Values.ToList());

src/NodeDev.Blazor/DiagramsModels/GraphNodeWidget.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<span class="ml-1">&lt;</span>
1717
@foreach (var undefinedGeneric in undefinedGenerics)
1818
{
19-
<span class="ml-1 undefined-generic" @onclick="() => GraphCanvas.OnGenericTypeSelectionMenuAsked(Node, undefinedGeneric)">@(undefinedGeneric.Name + (undefinedGeneric == undefinedGenerics[^1] ? "" : ", "))</span>
19+
<span class="ml-1 undefined-generic" @onclick="() => GraphCanvas.OnGenericTypeSelectionMenuAsked(Node, undefinedGeneric)">@(undefinedGeneric + (undefinedGeneric == undefinedGenerics[^1] ? "" : ", "))</span>
2020
}
2121
<span class="ml-1">&gt;</span>
2222
}

src/NodeDev.Blazor/DiagramsModels/GraphPortModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public override bool CanAttachTo(ILinkable other)
2929
if(Alignment == otherPort.Alignment) // can't plug input to input or output to output
3030
return false;
3131

32-
return Connection.Type.IsAssignableTo(otherPort.Connection.Type, out _);
32+
return Connection.Type.IsAssignableTo(otherPort.Connection.Type, out _, out _);
3333
}
3434
}

src/NodeDev.Blazor/Services/GraphManager/GraphManagerService.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ public void AddNewConnectionBetween(Connection source, Connection destination)
2424
Graph.Connect(source, destination, false);
2525

2626
// we're plugging something something with a generic into something without a generic
27-
if (source.IsAssignableTo(destination, true, true, out var newTypes, out var usedInitialTypes) && newTypes.Count != 0)
27+
if (source.IsAssignableTo(destination, true, true, out var newTypesLeft, out var newTypesRight, out var usedInitialTypes))
2828
{
29-
PropagateNewGeneric(source.Parent, newTypes, usedInitialTypes, destination, false);
30-
PropagateNewGeneric(destination.Parent, newTypes, usedInitialTypes, source, false);
29+
if (newTypesLeft.Count != 0)
30+
PropagateNewGeneric(source.Parent, newTypesLeft, usedInitialTypes, destination, false);
31+
if (newTypesRight.Count != 0)
32+
PropagateNewGeneric(destination.Parent, newTypesRight, usedInitialTypes, source, false);
3133
}
3234

3335
GraphCanvas.UpdatePortColor(source);
@@ -55,7 +57,7 @@ public void DisconnectConnectionBetween(Connection source, Connection destinatio
5557
/// Propagate the new generic type to all the connections of the node and recursively to the connected nodes.
5658
/// </summary>
5759
/// <param name="initiatingConnection">The connection that initiated the propagation. This is used to avoid reupdating back and forth, sometimes erasing information in the process.</param>
58-
public void PropagateNewGeneric(Node node, IReadOnlyDictionary<UndefinedGenericType, TypeBase> changedGenerics, bool useInitialTypes, Connection? initiatingConnection, bool overrideInitialTypes)
60+
public void PropagateNewGeneric(Node node, IReadOnlyDictionary<string, TypeBase> changedGenerics, bool useInitialTypes, Connection? initiatingConnection, bool overrideInitialTypes)
5961
{
6062
bool hadAnyChanges = false;
6163
foreach (var port in node.InputsAndOutputs) // check if any of the ports have the generic we just solved
@@ -74,14 +76,19 @@ public void PropagateNewGeneric(Node node, IReadOnlyDictionary<UndefinedGenericT
7476
// check if other connections had their own generics and if we just solved them
7577
foreach (var other in port.Connections.ToList())
7678
{
77-
if(other == initiatingConnection)
79+
if (other == initiatingConnection)
7880
continue;
7981

8082
var source = isPortInput ? other : port;
8183
var target = isPortInput ? port : other;
82-
if (source.IsAssignableTo(target, isPortInput, !isPortInput, out var changedGenerics2, out var usedInitialTypes) && changedGenerics2.Count != 0)
83-
PropagateNewGeneric(other.Parent, changedGenerics2, usedInitialTypes, port, false);
84-
else if ((changedGenerics2?.Count ?? 0) != 0)// looks like changing the generic made it so we can't link to this connection anymore
84+
if (source.IsAssignableTo(target, isPortInput, !isPortInput, out var changedGenericsLeft2, out var changedGenericsRight2, out var usedInitialTypes) && (changedGenericsLeft2.Count != 0 || changedGenericsRight2.Count != 0))
85+
{
86+
if (changedGenericsLeft2.Count != 0)
87+
PropagateNewGeneric(port.Parent, changedGenericsLeft2, usedInitialTypes, other, false);
88+
if (changedGenericsRight2.Count != 0)
89+
PropagateNewGeneric(other.Parent, changedGenericsRight2, usedInitialTypes, port, false);
90+
}
91+
else if ((changedGenericsLeft2?.Count ?? 0) != 0)// looks like changing the generic made it so we can't link to this connection anymore
8592
DisconnectConnectionBetween(port, other);
8693
}
8794
}

src/NodeDev.Core/Connections/Connection.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,33 @@ internal static Connection Deserialize(Node parent, SerializedConnection seriali
113113

114114
#endregion
115115

116-
public bool IsAssignableTo(Connection other, bool alsoValidateInitialTypeSource, bool alsoValidateInitialTypeDestination, [MaybeNullWhen(false)] out Dictionary<UndefinedGenericType, TypeBase> changedGenerics, out bool usedInitialTypes)
116+
public bool IsAssignableTo(Connection other, bool alsoValidateInitialTypeSource, bool alsoValidateInitialTypeDestination, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsLeft, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsRight, out bool usedInitialTypes)
117117
{
118-
if (Type.IsAssignableTo(other.Type, out var changedGenerics1, out var depth1))
118+
if (Type.IsAssignableTo(other.Type, out var changedGenericsLeft1, out var changedGenericsRight1, out var depth1))
119119
{
120120
if (alsoValidateInitialTypeSource || alsoValidateInitialTypeDestination)
121121
{
122122
var initialType = alsoValidateInitialTypeSource ? InitialType : Type;
123123
var otherInitialType = alsoValidateInitialTypeDestination ? other.InitialType : other.Type;
124-
if ((initialType != Type || otherInitialType != other.Type) && initialType.IsAssignableTo(otherInitialType, out var changedGenerics2, out var depth2))
124+
if ((initialType != Type || otherInitialType != other.Type) && initialType.IsAssignableTo(otherInitialType, out var changedGenericsLeft2, out var changedGenericsRight2, out var depth2))
125125
{
126-
if (changedGenerics2.Count != 0 && depth2 < depth1)
126+
if ((changedGenericsLeft2.Count != 0 || changedGenericsRight2.Count != 0) && depth2 < depth1)
127127
{
128-
changedGenerics = changedGenerics2;
128+
changedGenericsLeft = changedGenericsLeft2;
129+
changedGenericsRight = changedGenericsRight2;
129130
usedInitialTypes = true;
130131
return true;
131132
}
132133
}
133134
}
134135

135-
changedGenerics = changedGenerics1;
136+
changedGenericsLeft = changedGenericsLeft1;
137+
changedGenericsRight = changedGenericsRight1;
136138
usedInitialTypes = false;
137139
return true;
138140
}
139141

140-
changedGenerics = null;
142+
changedGenericsLeft = changedGenericsRight = null;
141143
usedInitialTypes = false;
142144
return false;
143145
}

src/NodeDev.Core/NodeProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ IEnumerable<NodeSearchResult> GetPropertiesAndFields(TypeBase type, string text)
6868

6969
// only keep the methods that are using the startConnection type, if provided
7070
if (startConnection?.Type?.IsExec == false)
71-
methods = methods.Where(x => x.GetParameters().Any(y => startConnection.Type.IsAssignableTo(y.ParameterType, out _)));
71+
methods = methods.Where(x => x.GetParameters().Any(y => startConnection.Type.IsAssignableTo(y.ParameterType, out _, out _)));
7272

7373
results = results.Concat(methods.Select(x => new MethodCallNode(typeof(MethodCall), x)));
7474

@@ -123,7 +123,7 @@ private static IEnumerable<IMethodInfo> GetExtensionMethods(TypeBase t, TypeFact
123123
.GetTypes()
124124
.Where(type => !type.IsGenericType)
125125
.SelectMany(x => x.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
126-
.Where(method => method.IsDefined(typeof(ExtensionAttribute), false) && t.IsAssignableTo(typeFactory.Get(method.GetParameters()[0].ParameterType, null), out _))
126+
.Where(method => method.IsDefined(typeof(ExtensionAttribute), false) && t.IsAssignableTo(typeFactory.Get(method.GetParameters()[0].ParameterType, null), out _, out _))
127127
.Select(x => new RealMethodInfo(typeFactory, x, typeFactory.Get(x.DeclaringType!, null)))
128128
.ToList();
129129
});

src/NodeDev.Core/Nodes/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Node(Graph graph, string? id = null)
6161
/// </summary>
6262
public int GraphIndex { get; set; } = -1;
6363

64-
public IEnumerable<UndefinedGenericType> GetUndefinedGenericTypes() => InputsAndOutputs.SelectMany(x => x.Type.GetUndefinedGenericTypes()).Distinct();
64+
public IEnumerable<string> GetUndefinedGenericTypes() => InputsAndOutputs.SelectMany(x => x.Type.GetUndefinedGenericTypes()).Distinct();
6565

6666
public record class AlternateOverload(TypeBase ReturnType, List<IMethodParameterInfo> Parameters);
6767
public virtual IEnumerable<AlternateOverload> AlternatesOverloads => [];

src/NodeDev.Core/Types/NodeClassArrayType.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ protected internal override string Serialize()
9595

9696
public static string GetArrayString(int nbArrayLevels)
9797
{
98+
if (nbArrayLevels == 0)
99+
return string.Empty;
100+
98101
var str = string.Create(nbArrayLevels * 2, nbArrayLevels, static (span, nbLevels) =>
99102
{
100103
for (int i = 0; i < span.Length; i += 2)

0 commit comments

Comments
 (0)