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

Refactor: extract dynamic fields #2294

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 27 additions & 25 deletions packages/data-models/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,36 @@ const DYNAMIC_STRING_REGEX = /[`!]?@([a-z]+)\.([0-9a-z_]+)([0-9a-z_.]*)/gi;
*
* Store these references in a separate object so they can be evaluated at runtime
*/
export function extractDynamicFields(data: any) {
export function extractDynamicFields(data: any): FlowTypes.IDynamicField | undefined {
// only extract from string, array or nested json object types
if (!data || typeof data !== "object") return;

// extract fields
let dynamicFields: FlowTypes.IDynamicField = {};
switch (typeof data) {
case "object":
// simply convert array to object to handle in next case
// ie. ["a","b"] => {0: "a", 1: "b"}
if (Array.isArray(data)) {
data = _arrayToObject(data);
}
if (data !== null) {
// data is a json-like object
Object.entries(data).forEach(([key, value]) => {
// skip processing some columns (remember these can be nested in other objects like parameter_list)
if (!["comments", "_dynamicFields"].includes(key)) {
const nestedDynamic = extractDynamicFields(value);
if (nestedDynamic) {
dynamicFields[key] = nestedDynamic;
}
}
});
// simply convert array to object to handle in next case
// ie. ["a","b"] => {0: "a", 1: "b"}
if (Array.isArray(data)) {
data = _arrayToObject(data);
}
// data is a json-like object
Object.entries(data).forEach(([key, value]) => {
// skip processing some columns (remember these can be nested in other objects like parameter_list)
if (!["comments", "_dynamicFields"].includes(key)) {
let nestedDynamic: FlowTypes.IDynamicField | FlowTypes.TemplateRowDynamicEvaluator[] =
undefined;
if (typeof value === "string") {
// extract evaluators from string expression
nestedDynamic = extractDynamicEvaluators(value);
} else {
// extract evaluators from deeper nested objects or arrays
nestedDynamic = extractDynamicFields(value);
}
break;
case "string":
const dynamicEvaluators = extractDynamicEvaluators(data as string);
if (dynamicEvaluators) {
return dynamicEvaluators;
if (nestedDynamic) {
dynamicFields[key] = nestedDynamic;
}
}
}
});

// nested dynamic fields are managed in the row themselves
if (dynamicFields.hasOwnProperty("rows")) {
delete dynamicFields["rows"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IConverterPaths, IFlowHashmapByType, IParsedWorkbookData } from "../../
import { arrayToHashmap, groupJsonByKey, IContentsEntry } from "../../utils";
import BaseProcessor from "../base";

const cacheVersion = 20240412.0;
const cacheVersion = 20240412.3;

export class FlowParserProcessor extends BaseProcessor<FlowTypes.FlowTypeWithData> {
public parsers: { [flowType in FlowTypes.FlowType]: Parsers.DefaultParser } = {
Expand Down
2 changes: 1 addition & 1 deletion src/app/feature/campaign/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class CampaignService extends AsyncServiceBase {
// process translations first as these are made with dynamic content in place (e.g. "hello @name")
const translatedRow = this.templateTranslateService.translateRow(row as any);
// Continue processing full row
translatedRow._dynamicFields = extractDynamicFields(translatedRow) as FlowTypes.IDynamicField;
translatedRow._dynamicFields = extractDynamicFields(translatedRow);
const parsedRow = await this.templateVariablesService.evaluatePLHData(translatedRow, {
row: translatedRow,
templateRowMap: {},
Expand Down
Loading