From 34213af21bc6652deaa9b659f56beafc62dda961 Mon Sep 17 00:00:00 2001 From: chrismclarke Date: Thu, 2 May 2024 16:13:39 -0700 Subject: [PATCH 1/2] feat: data-items local context (alt) --- .../components/data-items/data-items.component.ts | 9 ++++++--- .../services/instance/template-row.service.ts | 13 +++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/app/shared/components/template/components/data-items/data-items.component.ts b/src/app/shared/components/template/components/data-items/data-items.component.ts index b32c9275e..6c8fb7d3b 100644 --- a/src/app/shared/components/template/components/data-items/data-items.component.ts +++ b/src/app/shared/components/template/components/data-items/data-items.component.ts @@ -147,6 +147,10 @@ export class TmplDataItemsComponent extends TemplateBaseComponent implements OnD rows: [], }, } as any); + // HACK - still want to be able to use localContext from parent rows so copy to child processor + processor.templateRowMap = JSON.parse( + JSON.stringify(this.parent.templateRowService.templateRowMap) + ); await processor.processContainerTemplateRows(); return processor.renderedRows; } @@ -185,9 +189,8 @@ export class TmplDataItemsComponent extends TemplateBaseComponent implements OnD parsed[listKey] = listValue; for (const [itemKey, itemValue] of Object.entries(listValue)) { if (typeof itemValue === "string") { - parsed[listKey][itemKey] = await this.templateVariablesService.evaluateConditionString( - itemValue - ); + parsed[listKey][itemKey] = + await this.templateVariablesService.evaluateConditionString(itemValue); } } } diff --git a/src/app/shared/components/template/services/instance/template-row.service.ts b/src/app/shared/components/template/services/instance/template-row.service.ts index 5746226fb..5eeef086a 100644 --- a/src/app/shared/components/template/services/instance/template-row.service.ts +++ b/src/app/shared/components/template/services/instance/template-row.service.ts @@ -36,7 +36,10 @@ export class TemplateRowService extends SyncServiceBase { public templateRowMap: ITemplateRowMap = {}; public renderedRows: FlowTypes.TemplateRow[]; // rows processed and filtered by condition - constructor(private injector: Injector, public container: TemplateContainerComponent) { + constructor( + private injector: Injector, + public container: TemplateContainerComponent + ) { super("TemplateRow"); /** * Avoid initialisation logic and prefer to ensure services ready @@ -278,6 +281,9 @@ export class TemplateRowService extends SyncServiceBase { if (type === "template") isNestedTemplate = true; + // data_items still need to process on render so avoid populating child rows to templateRowMap + if (type === "data_items") isNestedTemplate = true; + // Instead of returning themselves items looped child rows if (type === "items") { // extract raw parameter list @@ -358,9 +364,8 @@ export class TemplateRowService extends SyncServiceBase { parsed[listKey] = listValue; for (const [itemKey, itemValue] of Object.entries(listValue)) { if (typeof itemValue === "string") { - parsed[listKey][itemKey] = await this.templateVariablesService.evaluateConditionString( - itemValue - ); + parsed[listKey][itemKey] = + await this.templateVariablesService.evaluateConditionString(itemValue); } } } From 79323f0cb99c87e6ed9d81b61d68536414cabe72 Mon Sep 17 00:00:00 2001 From: Johnny McQuade Date: Fri, 3 May 2024 11:27:45 +0100 Subject: [PATCH 2/2] test: template variables service spec --- .../template-variables.service.spec.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/app/shared/components/template/services/template-variables.service.spec.ts b/src/app/shared/components/template/services/template-variables.service.spec.ts index 259296d75..3a493d0ae 100644 --- a/src/app/shared/components/template/services/template-variables.service.spec.ts +++ b/src/app/shared/components/template/services/template-variables.service.spec.ts @@ -88,6 +88,33 @@ const TEST_ITEM_CONTEXT: IVariableContext = { }, }; +const TEST_LOCAL_CONTEXT: IVariableContext = { + ...MOCK_CONTEXT_BASE, + templateRowMap: { + // Mock row setting a local variable + string_local: { + name: "string_local", + value: "Jasper", + type: "set_variable", + _nested_name: "string_local", + }, + }, + row: { + ...MOCK_CONTEXT_BASE.row, + value: "Hello @local.string_local", + _dynamicFields: { + value: [ + { + fullExpression: "Hello @local.string_local", + matchedExpression: "@local.string_local", + type: "local", + fieldName: "string_local", + }, + ], + }, + }, +}; + /** * Call standalone tests via: * yarn ng test --include src/app/shared/components/template/services/template-variables.service.spec.ts @@ -178,4 +205,13 @@ describe("TemplateVariablesService", () => { ); expect(resWithoutItemContext).toEqual(MOCK_ITEM_STRING); }); + + it("Evaluates string containing local variable", async () => { + const MOCK_LOCAL_STRING = "Hello @local.string_local"; + const resWithLocalContext = await service.evaluatePLHData( + MOCK_LOCAL_STRING, + TEST_LOCAL_CONTEXT + ); + expect(resWithLocalContext).toEqual("Hello Jasper"); + }); });