w4c-workflows-api/Services/Nodes/NodeExecutorRegistry.cs
2026-09-11 19:04:50 +03:00

38 lines
1.4 KiB
C#

using w4c_workflows.Models.Nodes;
namespace w4c_workflows.Services.Nodes;
/// <summary>
/// Maps a blueprint type id to the <see cref="INodeExecutor"/> that can run it.
/// Blueprints may exist without an executor (catalogue-only, e.g. a connector
/// that is listed but not installed); the compiler and API decide how to surface
/// that, the registry only answers whether a runner exists.
/// </summary>
public class NodeExecutorRegistry
{
private readonly IReadOnlyDictionary<string, INodeExecutor> _executors;
public NodeExecutorRegistry(IEnumerable<INodeExecutor> executors)
{
var map = new Dictionary<string, INodeExecutor>(StringComparer.Ordinal);
foreach (var executor in executors)
{
if (string.IsNullOrWhiteSpace(executor.Type))
throw new InvalidOperationException("a node executor is missing its type id");
if (!map.TryAdd(executor.Type, executor))
throw new InvalidOperationException($"duplicate node executor for type '{executor.Type}'");
}
_executors = map;
}
public IReadOnlyCollection<string> Types => (IReadOnlyCollection<string>)_executors.Keys;
public INodeExecutor? Resolve(string type)
=> _executors.TryGetValue(type, out var executor) ? executor : null;
public bool CanRun(string type) => _executors.ContainsKey(type);
public bool CanRun(NodeBlueprint blueprint) => CanRun(blueprint.Type);
}