import { BoundValue, isBoundValue } from "../BoundValue"; import { RuntimeInputs, ObjectRuntimeInputs } from "../RuntimeInputs"; import { InputPathToArray, InputPathToObject, InputPathToValue } from "@octopusdeploy/step-inputs"; import { convertFromOriginalType, convertToOriginalType, getPathToArrayInput, getPathToInput, getPathToInputObject, PathSegment, PathToInput, } from "./RuntimeInputPath"; export type InputObjectAccessor = InputAccessor>; export type InputValueAccessor = InputAccessor; export type InputValueAccessorNoBoundValues = InputAccessor; export type InputArrayAccessor = InputAccessor>>; type InputAccessor = { getInputValue: (inputs: ObjectRuntimeInputs) => Input; changeInputValue: (inputs: ObjectRuntimeInputs, newValue: Input) => ObjectRuntimeInputs; }; export function createInputValueAccessor(pathToInput: InputPathToValue): InputValueAccessor { return createInputAccessor>( getPathToInput(pathToInput), (existingInput) => { if (isBoundValue(existingInput)) { return existingInput; } return convertFromOriginalType(pathToInput, existingInput); }, (newInput) => { if (isBoundValue(newInput)) { // Not all inputs support bound values, but if we see a bound value here then we can expect it was allowed for this input type // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return newInput as RuntimeInputs; } else { return convertToOriginalType(pathToInput, newInput); } } ); } export function createInputValueAccessorForDeploymentTarget( pathToInput: InputPathToValue ): InputValueAccessorNoBoundValues { return createInputAccessor>( getPathToInput(pathToInput), (existingInput) => { return convertFromOriginalType(pathToInput, existingInput); }, (newInput) => { return convertToOriginalType(pathToInput, newInput); } ); } export function createObjectValueAccessor(pathToObject: InputPathToObject): InputObjectAccessor { return createInputAccessor, ObjectRuntimeInputs>( getPathToInputObject(pathToObject), (existingInput) => existingInput, (newInput) => newInput ); } export function createInputArrayAccessor(pathToArray: InputPathToArray): InputArrayAccessor { return createInputAccessor>, Array>>( getPathToArrayInput(pathToArray), (existingArray) => existingArray, (newArray) => newArray ); } function createInputAccessor( pathToInput: PathToInput, convertToInput: (valueAtPath: RuntimeInputType) => Input, convertFromInput: (input: Input) => RuntimeInputType ): InputAccessor { return { getInputValue: (inputs) => { const result = pathToInput.reduce>((prev, path) => { if (typeof path === "symbol") { throw new Error("symbol paths are not supported"); } // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return prev[path] as Record; }, inputs); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const runtimeInputType = result as RuntimeInputType; return convertToInput(runtimeInputType); }, changeInputValue: (inputs, newValue) => { const convertedNewValue = convertFromInput(newValue); const result = changeValueAtPath(pathToInput, inputs, convertedNewValue); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return result as ObjectRuntimeInputs; }, }; } function changeValueAtPath(partialPaths: readonly PathSegment[], inputAtPath: Record | Array, newValue: unknown): unknown { const [partialPath, ...rest] = partialPaths; if (partialPath === undefined) { return newValue; } if (typeof partialPath === "symbol") { throw new Error("symbol paths are not supported"); } if (Array.isArray(inputAtPath)) { return changeValueInArray(partialPath, inputAtPath, rest, newValue); } // Otherwise it must be an object return changeValueInObject(inputAtPath, partialPath, rest, newValue); } function changeValueInArray(partialPath: string | number, inputAtPath: Array, rest: readonly PathSegment[], newValue: unknown): unknown[] { if (typeof partialPath !== "number") { throw new Error("Only number keys can be used for arrays"); } return inputAtPath.map((inputItem, index) => { if (index === partialPath) { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const indexableItem = inputItem as Record | Array; return changeValueAtPath(rest, indexableItem, newValue); } return inputItem; }); } function changeValueInObject( inputAtPath: Record, partialPath: string | number, rest: readonly PathSegment[], newValue: unknown ) { const inputAtPathElement = inputAtPath[partialPath]; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const indexableInput = inputAtPathElement as Record | Array; return { ...inputAtPath, [partialPath]: changeValueAtPath(rest, indexableInput, newValue), }; }