|
| 1 | +using System.Text.Json.Nodes; |
| 2 | +using PythonScriptRunner.Core.Execution; |
| 3 | +using PythonScriptRunner.Core.Values; |
| 4 | + |
| 5 | +namespace PythonScriptRunner.Core.Pipelines; |
| 6 | + |
| 7 | +/// <summary>Inputs of one pipeline step after resolving its bindings.</summary> |
| 8 | +internal sealed record StepInputs(JsonObject Wire, IReadOnlyList<InputFileCopy> Files, IReadOnlyList<InputError> Errors); |
| 9 | + |
| 10 | +/// <summary>Resolves the inputs of a step from pipeline inputs, earlier step outputs, constants and implicit bindings.</summary> |
| 11 | +internal static class StepInputResolver |
| 12 | +{ |
| 13 | + public static StepInputs Resolve( |
| 14 | + StageDefinition stage, |
| 15 | + PipelineDefinition pipeline, |
| 16 | + int index, |
| 17 | + JsonObject pipelineInputs, |
| 18 | + IReadOnlyDictionary<string, IReadOnlyList<PsrFile>> pipelineFiles, |
| 19 | + IReadOnlyDictionary<string, StepResult> completed) |
| 20 | + { |
| 21 | + var step = pipeline.Steps[index]; |
| 22 | + var wire = new JsonObject(); |
| 23 | + var files = new List<InputFileCopy>(); |
| 24 | + var errors = new List<InputError>(); |
| 25 | + foreach (var target in stage.Inputs) |
| 26 | + { |
| 27 | + var source = step.Bindings.FirstOrDefault(b => b.Input == target.Name)?.Source ?? Implicit(target, index, pipeline); |
| 28 | + var (value, sourceFiles) = Read(source, pipelineInputs, pipelineFiles, completed); |
| 29 | + if (target.Type.IsFile) |
| 30 | + { |
| 31 | + if (sourceFiles is { Count: > 0 }) |
| 32 | + { |
| 33 | + files.AddRange(sourceFiles.Select((file, i) => |
| 34 | + new InputFileCopy(target.Name, i, file.LocalPath!, file.FileName, file.ContentType, file.Length ?? 0, file.Sha256!))); |
| 35 | + } |
| 36 | + else if (target.Required) |
| 37 | + { |
| 38 | + errors.Add(Missing(target, source)); |
| 39 | + } |
| 40 | + |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + if (value is null) |
| 45 | + { |
| 46 | + if (target.Default is not null) |
| 47 | + { |
| 48 | + wire[target.Name] = target.Default.DeepClone(); |
| 49 | + } |
| 50 | + else if (target.Required) |
| 51 | + { |
| 52 | + errors.Add(Missing(target, source)); |
| 53 | + } |
| 54 | + |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + var coerced = Coercion.Coerce(value, target.Type); |
| 59 | + var wireErrors = new List<WireError>(); |
| 60 | + WireValidator.Validate(coerced, target, target.Name, wireErrors); |
| 61 | + errors.AddRange(wireErrors.Select(e => new InputError(e.Path, e.Code, e.Message))); |
| 62 | + wire[target.Name] = coerced; |
| 63 | + } |
| 64 | + |
| 65 | + return new StepInputs(wire, files, errors); |
| 66 | + } |
| 67 | + |
| 68 | + private static BindingSource? Implicit(FieldDefinition target, int index, PipelineDefinition pipeline) |
| 69 | + { |
| 70 | + if (index > 0) |
| 71 | + { |
| 72 | + return new StepOutputSource(pipeline.Steps[index - 1].Key, target.Name); |
| 73 | + } |
| 74 | + |
| 75 | + return pipeline.Inputs.Any(f => f.Name == target.Name) ? new PipelineInputSource(target.Name) : null; |
| 76 | + } |
| 77 | + |
| 78 | + private static (JsonNode? Value, IReadOnlyList<PsrFile>? Files) Read( |
| 79 | + BindingSource? source, |
| 80 | + JsonObject pipelineInputs, |
| 81 | + IReadOnlyDictionary<string, IReadOnlyList<PsrFile>> pipelineFiles, |
| 82 | + IReadOnlyDictionary<string, StepResult> completed) => source switch |
| 83 | + { |
| 84 | + PipelineInputSource input => (pipelineInputs[input.Field], pipelineFiles.GetValueOrDefault(input.Field)), |
| 85 | + StepOutputSource output when completed.TryGetValue(output.Step, out var result) => |
| 86 | + (result.Outputs?[output.Field], result.Files.GetValueOrDefault(output.Field)), |
| 87 | + ConstantSource constant => (constant.Value, null), |
| 88 | + _ => (null, null), |
| 89 | + }; |
| 90 | + |
| 91 | + private static InputError Missing(FieldDefinition target, BindingSource? source) => |
| 92 | + new(target.Name, "MISSING_REQUIRED_INPUT", source is null |
| 93 | + ? "required input has no binding and no default" |
| 94 | + : $"required input received no value from {source}"); |
| 95 | +} |
0 commit comments