Skip to content

Commit

Permalink
Merge pull request #17 from narthur/codegen
Browse files Browse the repository at this point in the history
fix single_select bug
  • Loading branch information
narthur committed Apr 11, 2024
2 parents 7e04ccb + df9c4d4 commit d51fe43
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
scripts/
.baserowrc
__generated__/
out.txt
2 changes: 1 addition & 1 deletion src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default async function main(): Promise<void> {
);

tables.map((table) => {
console.log(table);
console.dir(table, { depth: null });
const tableName = table.name;
const fields = table.fields;
const foreignTables = fields
Expand Down
26 changes: 26 additions & 0 deletions src/codegen/makeFieldType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, it, expect } from "vitest";
import { makeFieldType } from "./makeFieldType";
import f from "../test/fixtures/fieldDefinition";

describe("makeFieldType", () => {
it("should return a string", () => {
expect(
makeFieldType(
f({
id: 4780,
table_id: 65,
name: "Status",
order: 7,
type: "single_select",
primary: false,
read_only: false,
select_options: [
{ id: 2510, value: "Proposal", color: "gray" },
{ id: 2512, value: "Execution", color: "dark-red" },
{ id: 2513, value: "Complete", color: "darker-blue" },
],
}),
),
).toBe(`"Proposal" | "Execution" | "Complete"`);
});
});
10 changes: 10 additions & 0 deletions src/codegen/makeFieldType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ export function makeFieldType(field: FieldDefinition): string {
return mapPrimitive(field.formula_type);
}
}
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 mapPrimitive(field.type);
}
10 changes: 0 additions & 10 deletions src/codegen/makeType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ describe("makeType", () => {
).toContain("number | string");
});

it("returns bool for type single select", () => {
expect(
makeType([
f({
type: "single_select",
}),
]),
).toContain("boolean");
});

it("returns string for type long text", () => {
expect(
makeType([
Expand Down
2 changes: 0 additions & 2 deletions src/codegen/mapPrimitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export function mapPrimitive(baserowPrimitive: string | undefined): string {
return "string";
case "number":
return "number | string";
case "single_select":
return "boolean";
case "long_text":
return "string";
case "boolean":
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type FieldDefinition = {
array_formula_type?: string;
formula_type?: string;
link_row_table_id?: number;
select_options?: { id: number; value: string; color: string }[];
};

export type ListFieldsResponse = Array<FieldDefinition>;
Expand Down

0 comments on commit d51fe43

Please sign in to comment.