Skip to content

Commit

Permalink
Accept readonly arrays on generated query file args (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Mar 4, 2024
1 parent 20fe059 commit cd1d76f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Expand Up @@ -11,5 +11,5 @@ select Movie {
version := sys::get_version(),
range := range(123, 456),
local_date := <cal::local_date>'2022-09-08',
} filter .characters.name = <optional str>$name;

} filter .characters.name = <optional str>$name
and .release_year in array_unpack(<array<year>>$years);
6 changes: 5 additions & 1 deletion integration-tests/lts/queries.test.ts
Expand Up @@ -22,7 +22,10 @@ describe("queries", () => {
});

test("basic select", async () => {
const result = await getMoviesStarring(client, { name: "Iron Man" });
const result = await getMoviesStarring(client, {
name: "Iron Man",
years: [2012, 2016] as const, // readonly arrays accepted
});

type result = typeof result;
tc.assert<
Expand Down Expand Up @@ -58,6 +61,7 @@ describe("queries", () => {
GetMoviesStarringArgs,
{
name?: string | null;
years: readonly number[];
}
>
>(true);
Expand Down
19 changes: 15 additions & 4 deletions packages/driver/src/reflection/analyzeQuery.ts
Expand Up @@ -31,13 +31,15 @@ export async function analyzeQuery(
const args = walkCodec(inCodec, {
indent: "",
optionalNulls: true,
readonly: true,
imports,
});

const result = applyCardinalityToTsType(
walkCodec(outCodec, {
indent: "",
optionalNulls: false,
readonly: false,
imports,
}),
cardinality
Expand Down Expand Up @@ -91,7 +93,12 @@ export function applyCardinalityToTsType(
export { walkCodec as walkCodecToTsType };
function walkCodec(
codec: ICodec,
ctx: { indent: string; optionalNulls: boolean; imports: Set<string> }
ctx: {
indent: string;
optionalNulls: boolean;
readonly: boolean;
imports: Set<string>;
}
): string {
if (codec instanceof NullCodec) {
return "null";
Expand All @@ -111,7 +118,7 @@ function walkCodec(
? codec.getFields()
: codec.getNames().map((name) => ({ name, cardinality: ONE }));
const subCodecs = codec.getSubcodecs();
return `{\n${fields
const objectShape = `{\n${fields
.map((field, i) => {
let subCodec = subCodecs[i];
if (subCodec instanceof SetCodec) {
Expand All @@ -130,12 +137,16 @@ function walkCodec(
)};`;
})
.join("\n")}\n${ctx.indent}}`;
return ctx.readonly ? `Readonly<${objectShape}>` : objectShape;
}
if (codec instanceof ArrayCodec) {
return `${walkCodec(codec.getSubcodecs()[0], ctx)}[]`;
return `${ctx.readonly ? "readonly " : ""}${walkCodec(
codec.getSubcodecs()[0],
ctx
)}[]`;
}
if (codec instanceof TupleCodec) {
return `[${codec
return `${ctx.readonly ? "readonly " : ""}[${codec
.getSubcodecs()
.map((subCodec) => walkCodec(subCodec, ctx))
.join(", ")}]`;
Expand Down

0 comments on commit cd1d76f

Please sign in to comment.