Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added trigger grouping methods to indexers #4413

Merged
merged 5 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Composer/packages/lib/indexers/src/extractSchemaProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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[] => {
if (dialog.content?.schema) {
const schema = jsonSchemaFiles.find(
(file) => file.id === getBaseName(dialog.content.schema) || file.id === dialog.content.schema
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a cast here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build caught that too :) Fortunately, it helped me find another missing case to handle.

)?.content;
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