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

Replace imports of modules/cal with modules/std/cal #1120

Merged
merged 5 commits into from
Oct 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 31 additions & 1 deletion 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 @@ -187,7 +206,18 @@ export async function generateQueryBuilder(params: {
.then((content) => content)
.catch(() => "");

const newContents = headerComment + f.content;
let newContents = headerComment + f.content;

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

if (oldContents !== newContents) {
await fs.writeFile(outputPath, 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
79 changes: 56 additions & 23 deletions packages/generate/src/genutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,58 +73,91 @@ 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
// keep this order of mapping, adding firstly mapping as it is in v6 and greater
// then also add mappings as they are in < v6
"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) {
// there's for example edgedb.LocalTime that maps to the std::cal::local_time
// if the server version > 5 continue, otherwise overwrite this mapping with cal::local_time
continue;
}
literalToScalarMapping[type] = { type: scalarType, literalKind };
}
literalToScalarMapping[type] = { type: scalarType, literalKind };
}
return literalToScalarMapping;
}

export function toTSScalarType(
Expand Down
10 changes: 3 additions & 7 deletions packages/generate/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"outDir": "dist"
},
"include": [
"src"
],
"exclude": [
"dist"
]
"include": ["src"],
"exclude": ["dist"]
}