Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept readonly arrays on generated query file args #887

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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