Skip to content

Commit

Permalink
Added support for update() for better support for data backed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 22, 2023
1 parent 1380220 commit 52d7288
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
13 changes: 12 additions & 1 deletion lib/__test__/example_descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ export class Food extends DataClass {
return Food._all;
}

public _price: float;

@GenieKey
@GenieProperty("Name of the food item")
public name: string;
Expand All @@ -273,11 +275,20 @@ export class Food extends DataClass {
}) {
super({});
this.name = name;
this.price = price;
this._price = price;
this.restaurant = restaurant;
Food._all.push(this);
}

async update() {
await new Promise((resolve) => {
setTimeout(() => {
resolve(void 0);
}, 10);
});
this.price = this._price;
}

description(): {} {
return {
name: this.name,
Expand Down
7 changes: 7 additions & 0 deletions lib/dsl-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export class GenieObject {
}
}

/**
* This function is called after the object is created, and called before function calls and property access.
*/
update(): void | Promise<void> {
return
}

// placeholder, should be replaced by GenieClass decorator
static _createObject<T extends typeof GenieObject>(
this: T,
Expand Down
17 changes: 16 additions & 1 deletion lib/dsl/__test__/dsl-interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ test("find burger name", async () => {
});
});

test("find burger price", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
"Restaurant.current().menu.matching(field: .name, value: \"hamburger\")[0].price"
);
expect(funcCallResult).toEqual({
objectType: "float",
type: "object",
value: 5
});
});

test("best restaurant", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
Expand Down Expand Up @@ -195,7 +207,10 @@ test("add hamburger to the order (steps)", async () => {
const funcCallResult: any[] = await interpreter.interpretSteps(
"Restaurant.all().equals(field: .priceGrade, value: 1)[0].name"
);
expect((await interpreter.describeSteps(funcCallResult)).reverse()[1]).toEqual({
const describeSteps =(await interpreter.describeSteps(
funcCallResult[funcCallResult.length - 1].steps
)).reverse()
expect((describeSteps[1])).toEqual({
type: "object",
value: {
address: "123 Main St, Palo Alto, USA",
Expand Down
19 changes: 14 additions & 5 deletions lib/dsl/dsl-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,19 @@ export class DslInterpreter {
return lastResult;
}

public async interpretSteps(input: string): Promise<any[]> {
public async interpretSteps(input: string): Promise<{ ast: any, result: any, steps: {ast: any, result: any}[] }[]> {
const ast = parse(input) as object[];
var lastResult = null;
let allSteps = [];
for (const statement of ast) {
// console.log(JSON.stringify(ast));
this.resolveSteps = [];
this.resolveStepsEnabled = true;
lastResult = await this.resolve(statement);
this.resolveStepsEnabled = false;
return this.resolveSteps;
allSteps.push({ ast: statement, result: lastResult, steps: this.resolveSteps });
}
return lastResult;
return allSteps;
}

/**
Expand Down Expand Up @@ -383,6 +384,7 @@ export class DslInterpreter {
value: ast.value,
};
} else {
await ast.value.update()
return { type: "object", value: await ast.value.description() };
}
} else {
Expand All @@ -395,7 +397,7 @@ export class DslInterpreter {
public async describeSteps(list: any[]) {
const steps = []
for (const e of list) {
steps.push(await this.describe(e))
steps.push(await this.describe(e.result))
}
return steps;
}
Expand Down Expand Up @@ -528,6 +530,7 @@ export class DslInterpreter {
}
} else {
if (isObject) {
await this.strip(parent).update();
fieldValue = this.strip(parent)[ast.access];
} else {
fieldValue = classDescriptor.classConstructor[ast.access];
Expand Down Expand Up @@ -617,7 +620,7 @@ export class DslInterpreter {
const parameter_entries = [];
if (ast.parameters !== null) {
for (const p of ast.parameters) {
parameter_entries.push([p.parameter, await this.resolve(p.value, env)]);
parameter_entries.push([p.parameter, await this.resolve(p.value, null)]);
}
}
const parameters = new Map(parameter_entries);
Expand Down Expand Up @@ -766,6 +769,9 @@ export class DslInterpreter {
}],
};
} else {
if (env.type === "object") {
await targetImplementation.update();
}
return {
type: "array",
value: (await targetImplementation[ast.func_name](matchedParameters)).map(
Expand All @@ -787,6 +793,9 @@ export class DslInterpreter {
objectType: returnType.original_type,
};
} else {
if (env.type === "object") {
await targetImplementation.update();
}
return {
type: "object",
value: await(targetImplementation[ast.func_name](matchedParameters)),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactgenie-dsl",
"version": "0.0.44",
"version": "0.0.45",
"description": "A natural language parser based on a large language model",
"scripts": {
"prepare": "peggy lib/dsl/parser.pegjs -o lib/dsl/parser.gen.js && tsc",
Expand Down

0 comments on commit 52d7288

Please sign in to comment.