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

Infer expression cardinality for Object UUID casts #912

Merged
merged 1 commit into from Mar 19, 2024
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
21 changes: 20 additions & 1 deletion integration-tests/lts/casts.test.ts
Expand Up @@ -3,7 +3,7 @@ import e from "./dbschema/edgeql-js";
import type { $Movie } from "./dbschema/edgeql-js/modules/default";

import { setupTests, tc, teardownTests } from "./setupTeardown";
import type { Client } from "edgedb";
import type { Client, $ } from "edgedb";

describe("casts", () => {
let client: Client;
Expand Down Expand Up @@ -47,6 +47,7 @@ describe("casts", () => {
assert.equal(expr.toEdgeQL(), `<default::Movie>{}`);

tc.assert<tc.IsExact<(typeof expr)["__element__"], $Movie>>(true);
tc.assert<tc.IsExact<(typeof expr)["__cardinality__"], $.Cardinality.Empty>>(true);
});

test("UUID to object cast", () => {
Expand All @@ -61,8 +62,26 @@ describe("casts", () => {
);

tc.assert<tc.IsExact<(typeof expr)["__element__"], $Movie>>(true);
tc.assert<tc.IsExact<(typeof expr)["__cardinality__"], $.Cardinality.One>>(true);

// @ts-expect-error: does not allow assignment of non UUID
e.cast(e.Movie, 42);
});

test("multiple UUIDs to object cast", () => {
const ids = e.cast(e.uuid, e.set(
"00000000-0000-0000-0000-000000000000",
"00000000-0000-0000-0000-000000000001",
"00000000-0000-0000-0000-000000000002",
));
const expr = e.cast(e.Movie, ids);

assert.equal(
expr.toEdgeQL(),
`<default::Movie>(<std::uuid>({ "00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000002" }))`
);

tc.assert<tc.IsExact<(typeof expr)["__element__"], $Movie>>(true);
tc.assert<tc.IsExact<(typeof expr)["__cardinality__"], $.Cardinality.AtLeastOne>>(true);
});
});
9 changes: 6 additions & 3 deletions packages/generate/src/syntax/cast.ts
Expand Up @@ -21,10 +21,13 @@ export function cast<Target extends BaseType | ObjectTypeExpression>(
: never,
Cardinality.Empty
>;
export function cast<Target extends ObjectTypeExpression>(
export function cast<
Target extends ObjectTypeExpression,
Card extends Cardinality
>(
target: Target,
arg: TypeSet<ScalarType<"std::uuid">>
): $expr_Cast<Target["__element__"], Cardinality.One>;
arg: TypeSet<ScalarType<"std::uuid">, Card>
): $expr_Cast<Target["__element__"], Card>;
export function cast<Target extends BaseType, Expr extends TypeSet>(
target: Target,
expr: orScalarLiteral<Expr>
Expand Down