import { ArrayTypeDefinition, DiscriminatorProperty, NonDiscriminatorTypeDefinition, PlainObjectTypeDefinition } from "../Schema/InputSchema"; import { ObjectRuntimeInputs } from "../RuntimeInputs"; import { exhaustiveCheck } from "../exhaustiveCheck"; import { createPathToArrayInput, createPathToInput, createPathToInputObject, PathSegment, PathToInput } from "./RuntimeInputPath"; import { ConvertibleInputPathToValue, Discriminator, InputPaths, ObjectInputPaths, ObjectInputPathsAndPathToObject } from "@octopusdeploy/step-inputs"; export function createObjectInputPaths(schema: PlainObjectTypeDefinition): ObjectInputPathsAndPathToObject { return createInputPathsForObject(schema, []); } function createInputPathsForObject( objectTypeDefinition: PlainObjectTypeDefinition, pathsToObject: PathToInput ): ObjectInputPathsAndPathToObject { const objectWithNonDiscriminatorProperties = objectTypeDefinition.nonDiscriminatorProperties.reduce>>((prev, p) => { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const inputKey = p.name as keyof StepInputs; return { ...prev, [inputKey]: createInputPathForProperty(inputKey, p.type, pathsToObject), }; }, {}); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const objectWithInputPaths = { ...objectWithNonDiscriminatorProperties, ...getDiscriminatorProperties(objectTypeDefinition.discriminatorProperties), } as ObjectInputPaths; return createPathToInputObject(pathsToObject, objectWithInputPaths); } function getDiscriminatorProperties(discriminatorProperties: DiscriminatorProperty[]) { type DiscriminatorProperties = DiscriminatorPropertiesOf>; return discriminatorProperties.reduce>((prev, discriminatorProperty) => { return { ...prev, [discriminatorProperty.discriminatorName]: discriminatorProperty.type.const, }; }, {}); } type DiscriminatorPropertiesOf = { [K in keyof T]: T[K] extends Discriminator ? T[K] : never; }; export function createInputPathForProperty( inputKey: PropertyKey, type: NonDiscriminatorTypeDefinition, pathsToObject: PathToInput ): InputPaths { const pathsToProperty = [...pathsToObject, inputKey]; switch (type.type) { case "package": case "container-image": case "sensitive": case "account": case "string": case "primitive": return getPrimitivePath(pathsToProperty); case "object": return getObjectPath(type, pathsToProperty); case "array": return getArrayPath(type, pathsToProperty); } exhaustiveCheck(type, "Possible type not handled in createInputPathForProperty"); } function getPrimitivePath( pathsToProperty: (PathSegment | PropertyKey)[] ): InputPaths { const pathToProperty: ConvertibleInputPathToValue = createPathToInput(pathsToProperty); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return pathToProperty as InputPaths; } function getObjectPath( type: PlainObjectTypeDefinition, pathsToProperty: (PathSegment | PropertyKey)[] ): InputPaths { const inputPathsForObject = createInputPathsForObject(type, pathsToProperty); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return inputPathsForObject as InputPaths; } function getArrayPath( type: ArrayTypeDefinition, pathsToProperty: (PathSegment | PropertyKey)[] ): InputPaths { type TypesInArray = ArrayType[PropertyKey]>; const arrayWithItemsThatHavePaths: Array> = type.itemTypes.map((itemType, i) => addPathToArrayItem(i, itemType, pathsToProperty)); const pathToInputArray = createPathToArrayInput(arrayWithItemsThatHavePaths, pathsToProperty); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return pathToInputArray as InputPaths; } function addPathToArrayItem( i: number, type: NonDiscriminatorTypeDefinition, pathsToProperty: (PathSegment | PropertyKey)[] ): InputPaths { return createInputPathForProperty, typeof i>(i, type, pathsToProperty); } type ArrayType = T extends ReadonlyArray ? TValue : T extends Array ? TValue : never;