-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from narthur/codegen
Codegen
- Loading branch information
Showing
13 changed files
with
275 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import f from "../test/fixtures/fieldDefinition"; | ||
import { makeGetter } from "./makeGetter"; | ||
import { FieldDefinition } from ".."; | ||
|
||
function run(field: Partial<FieldDefinition> = {}): string { | ||
return makeGetter(f(field), []); | ||
} | ||
|
||
describe("makeGetter", () => { | ||
it.each([ | ||
[{ type: "text" }, "string"], | ||
[{ type: "number" }, "number"], | ||
[ | ||
{ | ||
type: "single_select", | ||
select_options: [ | ||
{ | ||
id: 1, | ||
value: "the_option_name", | ||
color: "red", | ||
}, | ||
], | ||
}, | ||
"the_option_name", | ||
], | ||
[{ type: "date" }, ": Date"], | ||
[ | ||
{ | ||
type: "single_select", | ||
select_options: [ | ||
{ | ||
id: 1, | ||
value: "the_option_name", | ||
color: "red", | ||
}, | ||
], | ||
}, | ||
`: "the_option_name"`, | ||
], | ||
[ | ||
{ | ||
type: "single_select", | ||
select_options: [ | ||
{ | ||
id: 1, | ||
value: "the_option_name", | ||
color: "red", | ||
}, | ||
], | ||
}, | ||
`<({ id: 1, value: "the_option_name", color: "red" })>`, | ||
], | ||
[ | ||
{ | ||
type: "single_select", | ||
select_options: [ | ||
{ | ||
id: 1, | ||
value: "the_option_name", | ||
color: "red", | ||
}, | ||
{ | ||
id: 2, | ||
value: "the_option_name_2", | ||
color: "blue", | ||
}, | ||
], | ||
}, | ||
`<({ id: 1, value: "the_option_name", color: "red" } | { id: 2, value: "the_option_name_2", color: "blue" })>`, | ||
], | ||
])("%s => `%s`", (field, expected) => { | ||
expect(run(field)).toContain(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Table } from "../codegen.js"; | ||
import { FieldDefinition, ListFieldsResponse } from "../index.js"; | ||
import { makeFieldType } from "./makeFieldType.js"; | ||
import { toCamelCase } from "./toCamelCase.js"; | ||
|
||
function getForeignTable( | ||
field: FieldDefinition, | ||
tables: Table[], | ||
): { id: number; name: string } { | ||
if (!field.link_row_table_id) { | ||
throw new Error("link_row_table_id is missing"); | ||
} | ||
const foreignTable = tables.find((t) => field.link_row_table_id === t.id); | ||
if (!foreignTable) { | ||
throw new Error("foreign table not found"); | ||
} | ||
return foreignTable; | ||
} | ||
|
||
function getReturnType(field: FieldDefinition, tables: Table[]): string { | ||
if (field.type === "date" || field.formula_type === "date") { | ||
return "Date"; | ||
} | ||
|
||
if (field.type === "link_row") { | ||
const foreignTable = getForeignTable(field, tables); | ||
return `Promise<${toCamelCase(foreignTable.name, true)}Row[]>`; | ||
} | ||
|
||
if (field.type === "single_select") { | ||
if (!field.select_options) { | ||
throw new Error( | ||
`Field ${field.name} is a single_select but has no select_options`, | ||
); | ||
} | ||
return field.select_options | ||
?.map((option) => `"${option.value}"`) | ||
.join(" | "); | ||
} | ||
|
||
return makeFieldType(field); | ||
} | ||
|
||
function getBody(field: FieldDefinition, tables: Table[]): string { | ||
const rawType = makeFieldType(field); | ||
const query = `this.getField<${rawType}>("${field.name}")`; | ||
|
||
if (field.type === "number") { | ||
return `return parseFloat(${query}.toString());`; | ||
} | ||
|
||
if (field.type === "single_select") { | ||
return `return ${query}.value;`; | ||
} | ||
|
||
if (field.type === "link_row") { | ||
const foreignTable = getForeignTable(field, tables); | ||
return `return Promise.all(${query}.map((r) => this.repository.${toCamelCase(`get one ${foreignTable.name}`)}(r.id)));`; | ||
} | ||
|
||
if (field.type === "date" || field.formula_type === "date") { | ||
return `return new Date(${query});`; | ||
} | ||
|
||
return `return ${query};`; | ||
} | ||
|
||
export function makeGetter( | ||
field: FieldDefinition, | ||
tables: { id: number; name: string; fields: ListFieldsResponse }[], | ||
): string { | ||
const fn = toCamelCase(`get ${field.name}`); | ||
const rt = getReturnType(field, tables); | ||
const bd = getBody(field, tables); | ||
|
||
return `\n public ${fn}(): ${rt} {\n ${bd}\n }`; | ||
} |
Oops, something went wrong.