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: data-items local context (alt) #2314

Merged
merged 5 commits into from
May 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
});
});
Loading