Skip to content

Commit

Permalink
Update std libs imports & literalToScalarMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
diksipav committed Oct 17, 2024
1 parent 8cea556 commit f538f5f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 51 deletions.
66 changes: 45 additions & 21 deletions integration-tests/lts/literals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import * as edgedb from "edgedb";
import { TypeKind } from "edgedb/dist/reflection";
import e from "./dbschema/edgeql-js";
import { setupTests } from "./setupTeardown";
import { setupTests, versionGTE } from "./setupTeardown";

describe("literals", () => {
test("literals", () => {
Expand Down Expand Up @@ -68,26 +68,50 @@ describe("literals", () => {
e.std.uuid(uuid).toEdgeQL(),
`<std::uuid>"317fee4c-0da5-45aa-9980-fedac211bfb6"`,
);
assert.equal(
e.cal.local_date(localdate).toEdgeQL(),
`<cal::local_date>'2021-10-31'`,
);
assert.equal(
e.cal.local_datetime(localdatetime).toEdgeQL(),
`<cal::local_datetime>'2021-10-31T21:45:30'`,
);
assert.equal(
e.cal.local_time(localtime).toEdgeQL(),
`<cal::local_time>'15:15:00'`,
);
assert.equal(
e.cal.relative_duration(relduration).toEdgeQL(),
`<cal::relative_duration>'P1Y2M21D'`,
);
assert.equal(
e.cal.date_duration(dateduration).toEdgeQL(),
`<cal::date_duration>'P1Y2M25D'`,
);

if (versionGTE(6)) {
assert.equal(
e.cal.local_date(localdate).toEdgeQL(),
`<std::cal::local_date>'2021-10-31'`,
);
assert.equal(
e.cal.local_datetime(localdatetime).toEdgeQL(),
`<std::cal::local_datetime>'2021-10-31T21:45:30'`,
);
assert.equal(
e.cal.local_time(localtime).toEdgeQL(),
`<std::cal::local_time>'15:15:00'`,
);
assert.equal(
e.cal.relative_duration(relduration).toEdgeQL(),
`<std::cal::relative_duration>'P1Y2M21D'`,
);
assert.equal(
e.cal.date_duration(dateduration).toEdgeQL(),
`<std::cal::date_duration>'P1Y2M25D'`,
);
} else {
assert.equal(
e.cal.local_date(localdate).toEdgeQL(),
`<cal::local_date>'2021-10-31'`,
);
assert.equal(
e.cal.local_datetime(localdatetime).toEdgeQL(),
`<cal::local_datetime>'2021-10-31T21:45:30'`,
);
assert.equal(
e.cal.local_time(localtime).toEdgeQL(),
`<cal::local_time>'15:15:00'`,
);
assert.equal(
e.cal.relative_duration(relduration).toEdgeQL(),
`<cal::relative_duration>'P1Y2M21D'`,
);
assert.equal(
e.cal.date_duration(dateduration).toEdgeQL(),
`<cal::date_duration>'P1Y2M25D'`,
);
}
});

test("collection type literals", () => {
Expand Down
8 changes: 5 additions & 3 deletions integration-tests/lts/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ SELECT (SELECT {
tuple: readonly [string, number, readonly boolean[]];
namedTuple: Readonly<{ a: number; b: readonly bigint[]; c: string }>;
jsonTuple: readonly [unknown];
people: Readonly<
{ name: string; age: number; tags: readonly string[] }[]
>;
people: readonly {
name: string;
age: number;
tags: readonly string[];
}[];
}
>
>(true);
Expand Down
28 changes: 26 additions & 2 deletions packages/generate/src/edgeql-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ export async function generateQueryBuilder(params: {
throw new Error(`Error: no syntax files found for target "${target}"`);
}

// libs that existed in modules/[lib] and in server v6 moved to modules/std/[lib]
const stdLibs = ["cal", "fts", "math", "pg"];

// instead of hardcoding we can check generated files inside modules/std
// if (version.major > 5) {
// const stdPath = path.join(prettyOutputDir, "modules", "std");
// const filenames = await fs.readdir(stdPath);

// for (const fname of filenames) {
// const fullPath = path.join(stdPath, fname);
// const fileStat = await fs.stat(fullPath);

// if (fileStat.isFile()) {
// const libName = path.parse(fname).name;
// stdLibs.push(libName);
// }
// }
// }

for (const f of syntaxFiles) {
const outputPath = path.join(syntaxOutDir, f.path);
written.add(outputPath);
Expand All @@ -190,8 +209,13 @@ export async function generateQueryBuilder(params: {
let newContents = headerComment + f.content;

// in server versions >=6 cal, fts, math and pg are moved inside std module
if (f.path === "range.ts" && version.major > 5) {
newContents = newContents.replace("modules/cal", "modules/std/cal");
if (version.major > 5) {
stdLibs.forEach((lib) => {
newContents = newContents.replace(
`modules/${lib}`,
`modules/std/${lib}`,
);
});
}

if (oldContents !== newContents) {
Expand Down
7 changes: 5 additions & 2 deletions packages/generate/src/edgeql-js/generateCastMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { GeneratorParams } from "../genutil";
import {
getRef,
joinFrags,
literalToScalarMapping,
getLiteralToScalarMapping,
quote,
scalarToLiteralMapping,
} from "../genutil";
Expand All @@ -13,9 +13,11 @@ import { getStringRepresentation } from "./generateObjectTypes";
const getRuntimeRef = (name: string) => getRef(name, { prefix: "" });

export const generateCastMaps = (params: GeneratorParams) => {
const { dir, types, casts, typesByName } = params;
const { dir, types, casts, typesByName, edgedbVersion } = params;
const { implicitCastMap } = casts;

const literalToScalarMapping = getLiteralToScalarMapping(edgedbVersion);

const f = dir.getPath("castMaps");
f.addImportStar("edgedb", "edgedb");
f.addImportStar("$", "./reflection", {
Expand Down Expand Up @@ -349,6 +351,7 @@ export const generateCastMaps = (params: GeneratorParams) => {
f.writeln([
t` T extends edgedb.MultiRange<infer E> ? $.MultiRangeType<literalToScalarType<E>> :`,
]);
// todo probably should be ScalarType or never
f.writeln([t` $.BaseType;\n\n`]);

f.writeln([
Expand Down
75 changes: 52 additions & 23 deletions packages/generate/src/genutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,58 +73,87 @@ export const scalarToLiteralMapping: {
literalKind: "instanceof",
extraTypes: ["string"],
},
"cal::local_datetime": {
"cfg::memory": {
type: "edgedb.ConfigMemory",
literalKind: "instanceof",
extraTypes: ["string"],
},
"ext::pgvector::vector": {
type: "Float32Array",
literalKind: "instanceof",
extraTypes: ["number[]"],
argTypes: ["number[]"],
},
// server version >=6
"std::cal::local_datetime": {
type: "edgedb.LocalDateTime",
literalKind: "instanceof",
extraTypes: ["string"],
},
"cal::local_date": {
"std::cal::local_date": {
type: "edgedb.LocalDate",
literalKind: "instanceof",
extraTypes: ["string"],
},
"cal::local_time": {
"std::cal::local_time": {
type: "edgedb.LocalTime",
literalKind: "instanceof",
extraTypes: ["string"],
},
"cal::relative_duration": {
"std::cal::relative_duration": {
type: "edgedb.RelativeDuration",
literalKind: "instanceof",
extraTypes: ["string"],
},
"cal::date_duration": {
"std::cal::date_duration": {
type: "edgedb.DateDuration",
literalKind: "instanceof",
extraTypes: ["string"],
},
"cfg::memory": {
type: "edgedb.ConfigMemory",
// server version < 6
"cal::local_datetime": {
type: "edgedb.LocalDateTime",
literalKind: "instanceof",
extraTypes: ["string"],
},
"ext::pgvector::vector": {
type: "Float32Array",
"cal::local_date": {
type: "edgedb.LocalDate",
literalKind: "instanceof",
extraTypes: ["number[]"],
argTypes: ["number[]"],
extraTypes: ["string"],
},
"cal::local_time": {
type: "edgedb.LocalTime",
literalKind: "instanceof",
extraTypes: ["string"],
},
"cal::relative_duration": {
type: "edgedb.RelativeDuration",
literalKind: "instanceof",
extraTypes: ["string"],
},
"cal::date_duration": {
type: "edgedb.DateDuration",
literalKind: "instanceof",
extraTypes: ["string"],
},
};

export const literalToScalarMapping: {
[key: string]: { type: string; literalKind: "typeof" | "instanceof" };
} = {};
for (const [scalarType, { type, literalKind }] of Object.entries(
scalarToLiteralMapping,
)) {
if (literalKind) {
if (literalToScalarMapping[type]) {
throw new Error(
`literal type '${type}' cannot be mapped to multiple scalar types`,
);
export function getLiteralToScalarMapping(version: Version) {
const literalToScalarMapping: {
[key: string]: { type: string; literalKind: "typeof" | "instanceof" };
} = {};

for (const [scalarType, { type, literalKind }] of Object.entries(
scalarToLiteralMapping,
)) {
if (literalKind) {
if (literalToScalarMapping[type] && version.major > 5) {
continue;
}
literalToScalarMapping[type] = { type: scalarType, literalKind };
}
literalToScalarMapping[type] = { type: scalarType, literalKind };
}
return literalToScalarMapping;
}

export function toTSScalarType(
Expand Down

0 comments on commit f538f5f

Please sign in to comment.