Skip to content

Commit

Permalink
Infer expression cardinality for Object UUID casts (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Mar 19, 2024
1 parent f9f39c9 commit 3676ed2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
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

0 comments on commit 3676ed2

Please sign in to comment.