Skip to content

Commit

Permalink
feat: Added trigger grouping methods to indexers (microsoft#4413)
Browse files Browse the repository at this point in the history
* Added trigger grouping methods to indexers

* Fix bug and build issue
  • Loading branch information
GeoffCoxMSFT committed Oct 19, 2020
1 parent 6957c24 commit 7648b1e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
35 changes: 35 additions & 0 deletions Composer/packages/lib/indexers/src/extractSchemaProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { DialogInfo, JsonSchemaFile } from '@bfc/shared';

import { getBaseName } from './utils/help';

export const extractSchemaProperties = (dialog: DialogInfo, jsonSchemaFiles: JsonSchemaFile[]): string[] => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const schemaRef: any = dialog.content?.schema;

if (schemaRef) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let schema: any = undefined;

if (schemaRef.$public || schemaRef.properties) {
schema = schemaRef;
} else {
schema = jsonSchemaFiles.find(
(file) => file.id === getBaseName(schemaRef as string) || file.id === dialog.content.schema
)?.content;
}

if (schema) {
if (schema?.$public && Array.isArray(schema.$public)) {
return schema.$public;
}
if (schema?.properties) {
return Object.keys(schema.properties) ?? [];
}
}
}

return [];
};
80 changes: 80 additions & 0 deletions Composer/packages/lib/indexers/src/groupTriggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { DialogInfo, ITrigger } from '@bfc/shared';
import { ExpressionParser } from 'adaptive-expressions';
import uniq from 'lodash/uniq';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getPropertyReferences = (content: any) => {
const foundProperties: string[] = [];

if (content) {
// has $designer: { "propertyGroups": ["<name1>", "<name2", ... ]
if (content.$designer?.propertyGroups && Array.isArray(content.$designer?.propertyGroups === 'array')) {
foundProperties.push(...content.$designer.propertyGroups);
}

// has "property": "<name>"
if (content.property && typeof content.property === 'string') {
foundProperties.push(content.property);
}

// has condition : "<expresssion referencing properties>"
if (content.condition) {
const expressionParser = new ExpressionParser();
const expression = expressionParser.parse(content.condition);
const references = expression.references().map((r) => (r.startsWith('$') ? r.substring(1) : r));
foundProperties.push(...references);
}
}

return uniq(foundProperties);
};

const getTriggerPropertyReferences = (trigger: ITrigger) => {
const content = trigger.content;

// inspect trigger
const foundProperties: string[] = getPropertyReferences(trigger.content);

// inspect actions
if (content.actions && Array.isArray(content.actions)) {
for (let i = 0; i < content.actions.length; i++) {
foundProperties.push(...getPropertyReferences(content.actions[i]));
}
}

return uniq(foundProperties);
};

export const groupTriggersByPropertyReference = (
dialog: DialogInfo,
options?: { allowMultiParent?: boolean; validProperties?: string[] }
): Record<string, ITrigger[]> => {
const result = {} as Record<string, ITrigger[]>;

const validProperties = options?.validProperties;
const isValidProperty = validProperties
? (x: string | undefined) => x && validProperties.findIndex((p) => x === p) !== -1
: () => true;

const addResult = (property: string, trigger: ITrigger) => {
result[property] ? result[property].push(trigger) : (result[property] = [trigger]);
};

if (dialog?.triggers) {
dialog.triggers.forEach((t) => {
const properties = getTriggerPropertyReferences(t).filter(isValidProperty);
if (properties.length > 1 && options?.allowMultiParent) {
properties.forEach((p) => {
addResult(p, t);
});
} else if (properties.length === 1) {
addResult(properties[0], t);
}
});
}

return result;
};
2 changes: 2 additions & 0 deletions Composer/packages/lib/indexers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ export * from './utils';
export * from './validations';
export * from './skillIndexer';
export * from './botProjectSpaceIndexer';
export * from './extractSchemaProperties';
export * from './groupTriggers';
2 changes: 1 addition & 1 deletion Composer/packages/types/src/indexers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export type Skill = {

export type JsonSchemaFile = {
id: string;
content: string;
content: any;
};

export type TextFile = {
Expand Down

0 comments on commit 7648b1e

Please sign in to comment.