Added nodes related to the list and array operations
Some checks failed
Build / build (push) Has been cancelled
Some checks failed
Build / build (push) Has been cancelled
This commit is contained in:
@@ -205,4 +205,88 @@ namespace Nodify.Calculator.Execution.Resolvers
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// LIST GET: pick an element by index from a JSON array (non-flow value node).
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
internal sealed class ListGetValueResolver : INodeValueResolver
|
||||
{
|
||||
public bool CanResolve(OperationViewModel node, ConnectorViewModel c)
|
||||
=> node is SystemOperationViewModel sys && sys.SystemOperationType == SystemOperations.LIST_GET;
|
||||
|
||||
public string Resolve(OperationViewModel node, ConnectorViewModel outputConnector, IValueResolutionContext ctx)
|
||||
{
|
||||
var listInput = node.Input.FirstOrDefault(i => i.Shape == ConnectorShape.Grid);
|
||||
var indexInput = node.Input.FirstOrDefault(i => (i.Title ?? "").StartsWith("Index", StringComparison.OrdinalIgnoreCase));
|
||||
if (listInput == null) return null;
|
||||
|
||||
var json = ctx.Read(listInput);
|
||||
var indexStr = indexInput != null ? ctx.Read(indexInput) : "0";
|
||||
int index = int.TryParse(indexStr, out var i) ? i : 0;
|
||||
|
||||
try
|
||||
{
|
||||
var arr = JArray.Parse(json ?? "[]");
|
||||
if (index >= 0 && index < arr.Count)
|
||||
{
|
||||
var item = arr[index];
|
||||
return item.Type == JTokenType.String ? item.ToString() : item.ToString(Formatting.None);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// LIST COUNT: returns the length of a JSON array as a string integer.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
internal sealed class ListCountValueResolver : INodeValueResolver
|
||||
{
|
||||
public bool CanResolve(OperationViewModel node, ConnectorViewModel c)
|
||||
=> node is SystemOperationViewModel sys && sys.SystemOperationType == SystemOperations.LIST_COUNT;
|
||||
|
||||
public string Resolve(OperationViewModel node, ConnectorViewModel outputConnector, IValueResolutionContext ctx)
|
||||
{
|
||||
var listInput = node.Input.FirstOrDefault(i => i.Shape == ConnectorShape.Grid);
|
||||
if (listInput == null) return "0";
|
||||
|
||||
var json = ctx.Read(listInput);
|
||||
try
|
||||
{
|
||||
var arr = JArray.Parse(json ?? "[]");
|
||||
return arr.Count.ToString();
|
||||
}
|
||||
catch { return "0"; }
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// LIST CONTAINS: checks if item exists in array, returns "true"/"false".
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
internal sealed class ListContainsValueResolver : INodeValueResolver
|
||||
{
|
||||
public bool CanResolve(OperationViewModel node, ConnectorViewModel c)
|
||||
=> node is SystemOperationViewModel sys && sys.SystemOperationType == SystemOperations.LIST_CONTAINS;
|
||||
|
||||
public string Resolve(OperationViewModel node, ConnectorViewModel outputConnector, IValueResolutionContext ctx)
|
||||
{
|
||||
var listInput = node.Input.FirstOrDefault(i => i.Shape == ConnectorShape.Grid);
|
||||
var itemInput = node.Input.FirstOrDefault(i => i.IsCopyConnector);
|
||||
if (listInput == null) return "false";
|
||||
|
||||
var json = ctx.Read(listInput);
|
||||
var itemJson = itemInput != null ? ctx.Read(itemInput) : null;
|
||||
|
||||
try
|
||||
{
|
||||
var arr = JArray.Parse(json ?? "[]");
|
||||
if (string.IsNullOrEmpty(itemJson)) return "false";
|
||||
var searchToken = JToken.Parse(itemJson);
|
||||
bool found = arr.Any(t => JToken.DeepEquals(t, searchToken));
|
||||
return found.ToString().ToLower();
|
||||
}
|
||||
catch { return "false"; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user