Skip to content

Commit

Permalink
don't create setters for read-only fields
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 9, 2024
1 parent 535dc52 commit be265a0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tsconfig.json
package.json
pnpm-lock.yaml
README.md
README.md
__generated__
11 changes: 11 additions & 0 deletions src/codegen/makeClassMethods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,15 @@ describe("makeClassMethods", () => {
]),
).toContain("parseFloat");
});

it("does not create setter for read-only field", () => {
expect(
makeClassMethods([
f({
name: "the_field_name",
read_only: true,
}),
]),
).not.toContain("setTheFieldName");
});
});
22 changes: 13 additions & 9 deletions src/codegen/makeClassMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ function makeGetter(field: FieldDefinition): string {
}`;
}

function makeSetter(field: FieldDefinition): string {
return `public ${toCamelCase(`set ${field.name}`)}(value: ${makeFieldType(
field,
)}): Promise<void> {
return this.setField("${field.name}", value);
}`;
}

export default function makeClassMethods(fields: ListFieldsResponse): string {
let classMethods = "";
const methods: string[] = [];

fields.forEach((field) => {
const t = makeFieldType(field);
classMethods += `
${makeGetter(field)}
public ${toCamelCase(`set ${field.name}`)}(value: ${t}): Promise<void> {
return this.setField("${field.name}", value);
methods.push(makeGetter(field));
if (!field.read_only) {
methods.push(makeSetter(field));
}
`;
});

return classMethods;
return methods.join("\n");
}

0 comments on commit be265a0

Please sign in to comment.