Skip to content

Commit

Permalink
fix: templated data parser infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismclarke committed Sep 25, 2024
1 parent 2678f1c commit f6bb227
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("App Data Converter", () => {
it("Populates output to folder by data type", async () => {
await converter.run();
const outputFolders = readdirSync(paths.outputFolder);
expect(outputFolders).toEqual(["data_list", "data_pipe", "template"]);
expect(outputFolders).toEqual(["data_list", "template"]);
});
it("Supports input from multiple source folders", async () => {
const multipleSourceConverter = new AppDataConverter({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class RowProcessor {
if (this.parent.flow.flow_type === "data_pipe") return this.row;
// Handle replacements
const context = { row: this.row };
return new TemplatedData({ context, initialValue: this.row }).parse();
return new TemplatedData({ context }).parse(this.row);
}

private removeMetaFields() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ describe("Templated Data Parsing", () => {
function execTest(testData: ITestData) {
const { input, output } = testData;
it(JSON.stringify(input), () => {
const parser = new TemplatedData({ context: context.input, initialValue: input });
const parsedValue = parser.parse();
const parser = new TemplatedData({ context: context.input });
const parsedValue = parser.parse(input);
expect(parsedValue).toEqual(output);
// process.nextTick(() => console.log(` ${JSON.stringify(parsedValue)}\n`));
});
Expand Down
24 changes: 10 additions & 14 deletions packages/shared/src/models/templatedData/templatedData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ITemplatedStringVariable } from "../../types";
import { addStringDelimiters, extractDelimitedTemplateString } from "../../utils/delimiters";
import { isObjectLiteral } from "../../utils";

type ITemplatedDataContext = { [prefix: string]: any };

Expand Down Expand Up @@ -27,9 +28,6 @@ export interface ITemplatedDataContextList {
* E.g. {row:{id:'example_1'}} will replace `@row.id` with 'example_1`
*/
export class TemplatedData {
/** Value containing templated data, e.g. `"Hello @row.id"` */
private initialValue: any;

/** Value returned after parsing templated data, e.g. `"Hello example_1"` */
public parsedValue: any;

Expand All @@ -46,14 +44,12 @@ export class TemplatedData {
/** A list of all variable replacements carried out during parse (for tracking dependencies list) */
public replacedVariablesList: { [key: string]: string } = {};

constructor(options?: { initialValue?: any; context?: ITemplatedDataContext }) {
this.updateValue(options?.initialValue ?? "");
constructor(options?: { context?: ITemplatedDataContext }) {
this.updateContext(options?.context ?? {});
}

/** Change the initial value whilst keeping existing context same */
public updateValue(value: any) {
this.initialValue = value;
this.replacedVariablesList = {};
return this;
}
Expand All @@ -77,18 +73,18 @@ export class TemplatedData {
* Main data conversion method
* Iterate over data, parse string values and nested objects and arrays recursively
*/
public parse<T>(value: T = this.initialValue) {
public parse<T>(value: T) {
if (value) {
if (typeof value === "string") {
value = this.parseTemplatedString(value);
}
// recurssively convert array and json-like objects
if (typeof value === "object") {
if (Array.isArray(value)) {
value = value.map((v) => this.parse(v)) as any;
}
if (value.constructor === {}.constructor) {
Object.keys(value).forEach((key) => (value[key] = this.parse(value[key])));
// recursively convert array and json-like objects
if (Array.isArray(value)) {
value = value.map((v) => this.parse(v)) as any;
}
if (isObjectLiteral(value)) {
for (const [key, nestedValue] of Object.entries(value)) {
value[key] = this.parse(nestedValue);
}
}
}
Expand Down

0 comments on commit f6bb227

Please sign in to comment.