When Clause processing added to getokbutton and getprovider (#12886)

* processwhenclause added

* processWhenClause explanation added

* changed comment to be more generic.

* changed expected comparison

* test and space fix added

* fixed tests

* resourceTypeService now uses forloop
This commit is contained in:
Alex Ma
2020-10-16 09:38:07 -07:00
committed by GitHub
parent f4c7ab29f0
commit f6949d834b
2 changed files with 71 additions and 25 deletions

View File

@@ -225,28 +225,8 @@ export class ResourceTypeService implements IResourceTypeService {
private getProvider(resourceType: ResourceType, selectedOptions: { option: string, value: string }[]): DeploymentProvider | undefined {
for (let i = 0; i < resourceType.providers.length; i++) {
const provider = resourceType.providers[i];
if (provider.when === undefined || provider.when.toString().toLowerCase() === 'true') {
if (processWhenClause(provider.when, selectedOptions)) {
return provider;
} else {
const expected = provider.when.replace(' ', '').split('&&').sort();
let actual: string[] = [];
selectedOptions.forEach(option => {
actual.push(`${option.option}=${option.value}`);
});
actual = actual.sort();
if (actual.length === expected.length) {
let matches = true;
for (let j = 0; j < actual.length; j++) {
if (actual[j] !== expected[j]) {
matches = false;
break;
}
}
if (matches) {
return provider;
}
}
}
}
return undefined;
@@ -256,10 +236,9 @@ export class ResourceTypeService implements IResourceTypeService {
* Get the ok button text based on the selected options
*/
private getOkButtonText(resourceType: ResourceType, selectedOptions: { option: string, value: string }[]): string | undefined {
if (resourceType.okButtonText && selectedOptions.length === 1) {
const optionGiven = `${selectedOptions[0].option}=${selectedOptions[0].value}`;
if (resourceType.okButtonText) {
for (const possibleOption of resourceType.okButtonText) {
if (possibleOption.when === optionGiven || possibleOption.when === undefined || possibleOption.when.toString().toLowerCase() === 'true') {
if (processWhenClause(possibleOption.when, selectedOptions)) {
return possibleOption.value;
}
}
@@ -267,6 +246,7 @@ export class ResourceTypeService implements IResourceTypeService {
return loc.select;
}
public startDeployment(provider: DeploymentProvider): void {
const self = this;
if (instanceOfWizardDeploymentProvider(provider)) {
@@ -370,3 +350,26 @@ async function exists(path: string): Promise<boolean> {
return false;
}
}
/**
* processWhenClause takes in a when clause (either the word 'true' or a series of clauses in the format:
* '<type_name>=<value_name>' joined by '&&').
* If the when clause is true or undefined, return true as there is no clause to check.
* It evaluates each individual when clause by comparing the equivalent selected options (sorted in alphabetical order and formatted to match).
* If there is any selected option that doesn't match, return false.
* Return true if all clauses match.
*/
export function processWhenClause(when: string | undefined, selectedOptions: { option: string, value: string }[]): boolean {
if (when === undefined || when.toString().toLowerCase() === 'true') {
return true;
} else {
const expected = when.replace(/\s/g, '').split('&&').sort();
const actual = selectedOptions.map(option => `${option.option}=${option.value}`);
for (let whenClause of expected) {
if (actual.indexOf(whenClause) === -1) {
return false;
}
}
return true;
}
}