Skip to content

Commit

Permalink
improve single select types
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 11, 2024
1 parent 36d924b commit c11298c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
18 changes: 18 additions & 0 deletions src/codegen/makeFieldType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function makeFieldType(field: FieldDefinition): string {
if (field.type === "link_row") {
return '{ "id": number, "value": string }[]';
}

if (["rollup", "formula", "lookup"].includes(field.type)) {
switch (field.formula_type) {
case "array":
Expand All @@ -13,5 +14,22 @@ 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`,
);
}

const options = field.select_options
.map((option) => {
return `{ id: ${option.id}, value: "${option.value}", color: "${option.color}" }`;
})
.join(" | ");

return `(${options})`;
}

return mapPrimitive(field.type);
}
26 changes: 1 addition & 25 deletions src/codegen/makeGetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,8 @@ function getReturnType(field: FieldDefinition, tables: Table[]): string {
return makeFieldType(field);
}

function getRawType(field: FieldDefinition): string {
if (field.type === "link_row") {
return `{ "id": number, "value": string }[]`;
}

if (field.type === "single_select") {
if (!field.select_options) {
throw new Error(
`Field ${field.name} is a single_select but has no select_options`,
);
}

const options = field.select_options
.map((option) => {
return `{ id: ${option.id}, value: "${option.value}", color: "${option.color}" }`;
})
.join(" | ");

return `(${options})`;
}

return makeFieldType(field);
}

function getBody(field: FieldDefinition, tables: Table[]): string {
const rawType = getRawType(field);
const rawType = makeFieldType(field);
const query = `this.getField<${rawType}>("${field.name}")`;

if (field.type === "number") {
Expand Down

0 comments on commit c11298c

Please sign in to comment.