import { Conversions, ConvertibleInputPathToValue, ConvertTo, InputPaths, InputPathToArray, InputPathToObject, InputPathToValue, ObjectInputPaths, ObjectInputPathsAndPathToObject, } from "@octopusdeploy/step-inputs"; export function createPathToArrayInput, ArrayItem>( array: ReadonlyArray>, pathToInput: PathToInput ): ReadonlyArray> & InputPathToArray { const partialRuntimePathToArray: Partial> = [...array]; partialRuntimePathToArray.__pathToInput = pathToInput; partialRuntimePathToArray.__type = "array"; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const runtimePathToArray = partialRuntimePathToArray as RuntimeInputPathToArray; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return runtimePathToArray as unknown as ReadonlyArray> & InputPathToArray; } export function getPathToArrayInput(pathToInput: InputPathToArray): PathToInput { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const runtimePathToArray = pathToInput as unknown as RuntimeInputPathToArray; return runtimePathToArray.__pathToInput; } export function getArrayFromArrayInputPath(pathToInput: InputPathToArray): ReadonlyArray> { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return pathToInput as unknown as RuntimeInputPathToArray; } // The runtime implementation of InputPathToArray type RuntimeInputPathToArray = ReadonlyArray> & { __pathToInput: PathToInput; __type: "array"; }; export function createPathToInput(pathToInput: PathToInput): ConvertibleInputPathToValue { return createPathToInputWithConversions(pathToInput, { convertFromOriginalType: (x) => { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return x as InputValue; }, convertToOriginalType: (x) => x, }); } export function createPathToInputWithConversions( pathToInput: PathToInput, conversions: RequiredConversions ): ConvertibleInputPathToValue { const runtimePathToValue: RuntimeInputPathToValue = { __pathToInput: pathToInput, __conversions: conversions, __type: "value", convertTo: (newConversions: Conversions): ConvertibleInputPathToValue => { return createPathToInputWithConversions(pathToInput, { convertToOriginalType: (newInput) => { const newConverter = newConversions.toOriginalType ? newConversions.toOriginalType : (x: NewValue) => { // If it wasn't provided, we assume the type system allowed it to be omitted because the types were assignable // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return x as unknown as InputValue; }; return conversions.convertToOriginalType(newConverter(newInput)); }, convertFromOriginalType: (original) => { const toType = newConversions.toNewType ? newConversions.toNewType : (x: InputValue) => { // If it wasn't provided, we assume the type system allowed it to be omitted because the types were assignable // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return x as unknown as NewValue; }; return toType(conversions.convertFromOriginalType(original)); }, }); }, }; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return runtimePathToValue as unknown as ConvertibleInputPathToValue; } export function getPathToInput(pathToInput: InputPathToValue): PathToInput { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const runtimePathToValue = pathToInput as unknown as RuntimeInputPathToValue; return runtimePathToValue.__pathToInput; } export function convertFromOriginalType(pathToInput: InputPathToValue, originalValue: OriginalValue): NewValue { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const runtimePathToValue = pathToInput as unknown as RuntimeInputPathToValue; return runtimePathToValue.__conversions.convertFromOriginalType(originalValue); } export function convertToOriginalType(pathToInput: InputPathToValue, value: NewValue): OriginalValue { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const runtimePathToValue = pathToInput as unknown as RuntimeInputPathToValue; const originalValue = runtimePathToValue.__conversions.convertToOriginalType(value); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return originalValue as OriginalValue; } type RequiredConversions = { convertFromOriginalType: (originalValue: unknown) => NewInput; convertToOriginalType: (value: NewInput) => unknown; }; // The runtime implementation of InputPathToValue type RuntimeInputPathToValue = { __type: "value"; __pathToInput: PathToInput; __conversions: RequiredConversions; convertTo: ConvertTo; }; export function getPathToInputObject(pathToObject: InputPathToObject): PathToInput { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const runtimeObjectWithInputPaths = pathToObject as unknown as RuntimeInputPathToObject; return runtimeObjectWithInputPaths.__pathToInput; } export function createPathToInputObject( pathToObject: PathToInput, objectWithInputPaths: ObjectInputPaths ): ObjectInputPathsAndPathToObject { const runtimeObjectWithInputPaths: RuntimeInputPathToObject = { ...objectWithInputPaths, __type: "object", __pathToInput: pathToObject, }; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return runtimeObjectWithInputPaths as unknown as ObjectInputPathsAndPathToObject; } type RuntimeInputPathToObject = ObjectInputPaths & { __type: "object"; __pathToInput: PathToInput; }; export function isInputPathToObject( path: InputPathToObject | InputPathToArray | InputPathToValue ): path is InputPathToObject { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const castedPath = path as unknown as RuntimeInputPathToObject | RuntimeInputPathToArray | RuntimeInputPathToValue; return castedPath.__type === "object"; } export function isInputPathToArray(path: InputPathToObject | InputPathToArray | InputPathToValue): path is InputPathToArray { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const castedPath = path as unknown as RuntimeInputPathToObject | RuntimeInputPathToArray | RuntimeInputPathToValue; return castedPath.__type === "array"; } export function isInputPathToValue(path: InputPathToObject | InputPathToArray | InputPathToValue): path is InputPathToValue { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const castedPath = path as unknown as RuntimeInputPathToObject | RuntimeInputPathToArray | RuntimeInputPathToValue; return castedPath.__type === "value"; } export type PathToInput = ReadonlyArray; export type PathSegment = string | number | symbol;