private TemplateStepInputCreate GetTemplateStepInput(string inputReference, TemplateStepCreate step) { if (string.IsNullOrEmpty(inputReference)) { throw new ArgumentException(nameof(inputReference)); } var inputReferencePath = inputReference.Split(Constants.TemplateComponentReferenceSeparator); var stepParentReferencePath = step.ParentReference?.Split(Constants.TemplateComponentReferenceSeparator) ?? Enumerable.Empty <string>(); if (!stepParentReferencePath.SequenceEqual(inputReferencePath.Take(stepParentReferencePath.Count()))) { throw new Exception("Step's parent reference did not match input's reference"); } var inputReferenceFromParentStep = inputReferencePath.Skip(stepParentReferencePath.Count()); var stepId = inputReferenceFromParentStep.First(); if (stepId != step.Id) { throw new Exception("Step id did not match id resolved from input reference"); } if (inputReferenceFromParentStep.Count() == 1) // Reference to step { if (step.Inputs.Count() == 1) { var input = step.Inputs.First(); if (string.IsNullOrEmpty(input.Key)) { return(input); } else { throw new Exception("Could not find default input from step reference (expected input's id to be empty)"); } } else { throw new Exception("Input reference did not contain an input id, expected one step"); } } else if (inputReferenceFromParentStep.Count() == 2) // Reference to input { var inputId = inputReferenceFromParentStep.Skip(1).Single(); return(step.Inputs.SingleOrDefault(i => i.Key == inputId)); } else { throw new ArgumentException(nameof(inputReference)); } }
private void ValidateTemplateStepInputs(TemplateStepCreate step, ModelErrorDictionary stepErrors, IEnumerable <object> stepErrorPath) { var stepInputsErrorPath = stepErrorPath.Concat(nameof(TemplateStepCreate.Inputs)); step.Inputs .ToIndexedLookup(i => i.Key) .Where(g => g.Count() > 1) .SelectMany(g => g.AsEnumerable()) .ForEach(indexedStepInput => { stepErrors.Add( $"An input with the id {indexedStepInput.Element.Key} already exists in this step (case-insensitive)", stepInputsErrorPath.Concat(new object[] { indexedStepInput.Index })); }); var isOnlyInput = step.Inputs.Count() == 1; step.Inputs.ForEach((stepInput, stepInputIndex) => ValidateTemplateStepInput( stepInput, stepErrors, stepInputsErrorPath.Concat(stepInputIndex), isOnlyInput)); }