Skip to content

Commit

Permalink
avoid unnecessary undefined default value
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 13, 2023
1 parent cc71547 commit 774b599
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/__test__/dsl-descriptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ test("Restaurant Description", async () => {
"\t// The current restaurant\n" +
"\tstatic Restaurant current();\n" +
"\t// Book a table for a given date time\n" +
"\tvoid book(dateTime: DateTime? = DateTime.today());\n" +
"\tvoid book(dateTime: DateTime? = `DateTime.today()`);\n" +
"\t// Book numbers tables for a given date time\n" +
"\tvoid bookTable(dateTime: DateTime?, number: int?);\n" +
"}"
);
});
22 changes: 22 additions & 0 deletions lib/__test__/example_descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,18 @@ export class Restaurant extends DataClass {
recentBooking = `${this.name} is booking for ${dateTime.toString()}`;
}

@GenieFunction("Book numbers tables for a given date time")
bookTable({ dateTime = undefined, number }: { dateTime?: DateTime, number?: int }): void {
if (dateTime === undefined) {
dateTime = DateTime.today();
}
if (number === undefined) {
number = 1;
}
console.log(`${this.name} is booking ${number} tables for ${dateTime.toString()}`);
recentBooking = `${this.name} is booking ${number} tables for ${dateTime.toString()}`;
}

@GenieKey
@GenieProperty("Name of the restaurant")
public name: string;
Expand Down Expand Up @@ -661,6 +673,16 @@ export class Restaurant extends DataClass {
false,
"Book a table for a given date time"
),
new FuncDescriptor(
"bookTable",
[
new ParamDescriptor("dateTime", "DateTime", false, null),
new ParamDescriptor("number", "int", false, null)
],
"void",
false,
"Book numbers tables for a given date time"
),
],
[
new FieldDescriptor("name", "string", false),
Expand Down
5 changes: 4 additions & 1 deletion lib/decorators/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,10 @@ export function GenieFunction(comment: string = "") {
const paramValueObj = destructingParamValues[0];
parameters = Object.keys(paramTypeObj).map((paramName) => {
const paramType = classToName(paramTypeObj[paramName].type);
const paramValue = paramValueObj[paramName];
let paramValue = paramValueObj[paramName];
if (paramValue === undefined || paramValue === "undefined") {
paramValue = null;
}
const paramOptional = paramTypeObj[paramName].optional;
return new ParamDescriptor(paramName, paramType, !paramOptional, paramValue);
});
Expand Down
4 changes: 2 additions & 2 deletions lib/dsl-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ export class ParamDescriptor {
public name: string,
public type: string,
public required: boolean = true,
public defaultValue?: any
public defaultValue: any = null
) {}
description(): string {
return (
`${this.name}: ${this.type}` + (this.required ? "" : "?") +
(this.defaultValue ? ` = ${this.defaultValue}` : "")
(this.defaultValue ? ` = \`${this.defaultValue}\`` : "")
);
}
}
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.40",
"version": "0.0.41",
"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 774b599

Please sign in to comment.