Fixed new object request node and allowed multiple classes with dot notation
All checks were successful
Build / build (push) Successful in 39s

This commit is contained in:
Ankitkumar Satapara
2026-04-29 11:50:20 +05:30
parent 77cd472f73
commit a235558db1
7 changed files with 270 additions and 184 deletions

View File

@@ -75,40 +75,69 @@ namespace Nodify.Calculator.Execution.Handlers
}
}
internal sealed class NewObjectHandler : INodeExecutionHandler
internal sealed class NewObjectExecHandler : INodeExecutionHandler
{
public bool CanHandle(OperationViewModel node, ExecutionContext ctx)
=> node is SystemOperationViewModel sys && sys.SystemOperationType == SystemOperations.NEW_OBJECT;
public void Execute(OperationViewModel node, ExecutionContext ctx)
{
ctx.Log($"Constructing new object: {node.Title}");
var jObj = new JObject();
ctx.Log($"[NEW OBJECT] Constructing: {node.Title}");
var root = new JObject();
foreach (var inp in node.Input)
{
if (inp.Shape == ConnectorShape.Triangle || inp.Shape == ConnectorShape.Square) continue;
if (inp.Shape == ConnectorShape.Triangle) continue;
var propName = inp.Title ?? "";
var parenIdx = propName.IndexOf(" (", StringComparison.Ordinal);
if (parenIdx > 0) propName = propName.Substring(0, parenIdx);
// Extract property path from title: "Address.Street (string)" → "Address.Street"
var title = inp.Title ?? "";
var parenIdx = title.IndexOf(" (", StringComparison.Ordinal);
var propPath = parenIdx > 0 ? title.Substring(0, parenIdx).Trim() : title.Trim();
if (string.IsNullOrEmpty(propPath)) continue;
var val = ctx.ReadInput(inp);
if (val != null)
{
try { jObj[propName] = JToken.Parse(val); }
catch { jObj[propName] = val; }
}
ctx.Log($"[NEW OBJECT] {propPath} = {(val?.Length > 80 ? val.Substring(0, 80) + "..." : val ?? "null")}");
SetNestedValue(root, propPath, val);
}
var json = root.ToString(Formatting.None);
ctx.Outputs[node.NodeId] = json;
ctx.Variables[node.NodeId] = json;
ctx.Log($"[NEW OBJECT] Result: {(json.Length > 200 ? json.Substring(0, 200) + "..." : json)}");
}
/// <summary>
/// Sets a value at a dot-notation path in a JObject, creating intermediate objects as needed.
/// e.g., "Address.Street" with value "Main St" → { "Address": { "Street": "Main St" } }
/// </summary>
private static void SetNestedValue(JObject root, string path, string val)
{
var parts = path.Split('.');
var current = root;
for (int i = 0; i < parts.Length - 1; i++)
{
if (current[parts[i]] is JObject existing)
current = existing;
else
{
jObj[propName] = null;
var child = new JObject();
current[parts[i]] = child;
current = child;
}
}
var json = jObj.ToString(Formatting.None);
ctx.Outputs[node.NodeId] = json;
ctx.Variables[node.NodeId] = json;
ctx.Log($"Object constructed: {json}");
var leaf = parts[parts.Length - 1];
if (val != null)
{
try { current[leaf] = JToken.Parse(val); }
catch { current[leaf] = val; }
}
else
{
current[leaf] = null;
}
}
}