From b714e899efd9db2ecd98549c3d2b1421bae91897 Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Thu, 5 Oct 2023 09:31:36 -0400 Subject: [PATCH] wip Generator running in 4.0 now --- .../driver/src/reflection/analyzeQuery.ts | 2 +- packages/driver/src/reflection/enums.ts | 1 + .../driver/src/reflection/queries/types.ts | 19 ++++++++++-- .../src/edgeql-js/generateCastMaps.ts | 7 ++++- .../src/edgeql-js/generateObjectTypes.ts | 11 +++++++ packages/generate/src/funcoputil.ts | 2 +- packages/generate/src/genutil.ts | 9 ++++++ packages/generate/src/syntax/casting.ts | 7 +++++ packages/generate/src/syntax/funcops.ts | 10 +++++++ packages/generate/src/syntax/hydrate.ts | 9 ++++++ packages/generate/src/syntax/typesystem.ts | 30 +++++++++++++++---- 11 files changed, 96 insertions(+), 11 deletions(-) diff --git a/packages/driver/src/reflection/analyzeQuery.ts b/packages/driver/src/reflection/analyzeQuery.ts index aa72d75e4..63029015f 100644 --- a/packages/driver/src/reflection/analyzeQuery.ts +++ b/packages/driver/src/reflection/analyzeQuery.ts @@ -147,7 +147,7 @@ function walkCodec( if (codec instanceof MultiRangeCodec) { const subCodec = codec.getSubcodecs()[0]; if (!(subCodec instanceof ScalarCodec)) { - throw Error("expected range subtype to be scalar type"); + throw Error("expected multirange subtype to be scalar type"); } ctx.imports.add("MultiRange"); return `MultiRange<${subCodec.tsType}>`; diff --git a/packages/driver/src/reflection/enums.ts b/packages/driver/src/reflection/enums.ts index 271ddaf1c..4697f60c0 100644 --- a/packages/driver/src/reflection/enums.ts +++ b/packages/driver/src/reflection/enums.ts @@ -15,6 +15,7 @@ export enum TypeKind { tuple = "tuple", array = "array", range = "range", + multirange = "multirange", } export enum ExpressionKind { diff --git a/packages/driver/src/reflection/queries/types.ts b/packages/driver/src/reflection/queries/types.ts index 2dc31dad1..4037ebd22 100644 --- a/packages/driver/src/reflection/queries/types.ts +++ b/packages/driver/src/reflection/queries/types.ts @@ -1,5 +1,5 @@ -import { Executor } from "../../ifaces"; -import { Cardinality } from "../enums"; +import type { Executor } from "../../ifaces"; +import type { Cardinality } from "../enums"; import type { UUID } from "./queryTypes"; import { StrictMap } from "../strictMap"; @@ -27,6 +27,7 @@ export type TypeKind = | "array" | "tuple" | "range" + | "multirange" | "unknown"; export type TypeProperties = { @@ -73,7 +74,12 @@ export type RangeType = TypeProperties<"range"> & { is_abstract: boolean; }; -export type PrimitiveType = ScalarType | ArrayType | TupleType | RangeType; +export type MultiRangeType = TypeProperties<"multirange"> & { + multirange_element_id: UUID; + is_abstract: boolean; +}; + +export type PrimitiveType = ScalarType | ArrayType | TupleType | RangeType | MultiRangeType; export type Type = PrimitiveType | ObjectType; export type Types = StrictMap; @@ -120,6 +126,7 @@ export async function getTypes( `select sys::get_version().major;` ); const v2Plus = version >= 2; + const v4Plus = version >= 4; const QUERY = ` WITH MODULE schema, @@ -144,6 +151,7 @@ export async function getTypes( 'array' IF Type IS Array ELSE 'tuple' IF Type IS Tuple ELSE ${v2Plus ? `'range' IF Type IS Range ELSE` : ``} + ${v4Plus ? `'multirange' IF Type IS MultiRange ELSE` : ``} 'unknown', [IS ScalarType].enum_values, @@ -224,6 +232,7 @@ export async function getTypes( name } ORDER BY @index ASC), ${v2Plus ? `range_element_id := [IS Range].element_type.id,` : ``} + ${v4Plus ? `multirange_element_id := [IS MultiRange].element_type.id,` : ``} } ORDER BY .name; `; @@ -261,6 +270,10 @@ export async function getTypes( typeMapping.get(type.material_id)?.id ?? type.material_id; } break; + case "multirange": + type.multirange_element_id = + typeMapping.get(type.multirange_element_id)?.id ?? type.multirange_element_id; + break; case "range": type.range_element_id = typeMapping.get(type.range_element_id)?.id ?? type.range_element_id; diff --git a/packages/generate/src/edgeql-js/generateCastMaps.ts b/packages/generate/src/edgeql-js/generateCastMaps.ts index 2b32cd94b..7f36880cc 100644 --- a/packages/generate/src/edgeql-js/generateCastMaps.ts +++ b/packages/generate/src/edgeql-js/generateCastMaps.ts @@ -267,7 +267,7 @@ export const generateCastMaps = (params: GeneratorParams) => { dts`declare `, t`type scalarLiterals =\n | ${Object.keys(literalToScalarMapping).join( "\n | " - )}\n | edgedb.Range;\n\n`, + )}\n | edgedb.Range | edgedb.MultiRange;\n\n`, ]); f.writeln([ @@ -296,6 +296,8 @@ export const generateCastMaps = (params: GeneratorParams) => { f.writeln([t` : T["__tstype__"]`]); f.writeln([t` : T extends $.RangeType`]); f.writeln([t` ? edgedb.Range`]); + f.writeln([t` : T extends $.MultiRangeType`]); + f.writeln([t` ? edgedb.MultiRange`]); f.writeln([t` : never;`]); f.writeln([ t`export `, @@ -344,6 +346,9 @@ export const generateCastMaps = (params: GeneratorParams) => { f.writeln([ t` T extends edgedb.Range ? $.RangeType> :`, ]); + f.writeln([ + t` T extends edgedb.MultiRange ? $.MultiRangeType> :`, + ]); f.writeln([t` $.BaseType;\n\n`]); f.writeln([ diff --git a/packages/generate/src/edgeql-js/generateObjectTypes.ts b/packages/generate/src/edgeql-js/generateObjectTypes.ts index 0a1b16a5c..a910f078f 100644 --- a/packages/generate/src/edgeql-js/generateObjectTypes.ts +++ b/packages/generate/src/edgeql-js/generateObjectTypes.ts @@ -150,6 +150,17 @@ export const getStringRepresentation: ( .runtimeType })`, }; + } else if (type.kind === "multirange") { + return { + staticType: frag`$.MultiRangeType<${ + getStringRepresentation(types.get(type.multirange_element_id), params) + .staticType + }>`, + runtimeType: frag`$.MultiRangeType(${ + getStringRepresentation(types.get(type.multirange_element_id), params) + .runtimeType + })`, + }; } else { throw new Error("Invalid type"); } diff --git a/packages/generate/src/funcoputil.ts b/packages/generate/src/funcoputil.ts index 6c33ba3e3..06b58f074 100644 --- a/packages/generate/src/funcoputil.ts +++ b/packages/generate/src/funcoputil.ts @@ -220,7 +220,7 @@ function _findPathOfAnytype( return `[${isNamed ? quote(name) : name}]${elPath}`; } } - } else if (type.kind === "range") { + } else if (type.kind === "range" || type.kind === "multirange") { return `["__element__"]["__element__"]`; } diff --git a/packages/generate/src/genutil.ts b/packages/generate/src/genutil.ts index bb49b19cc..b5e4ed715 100644 --- a/packages/generate/src/genutil.ts +++ b/packages/generate/src/genutil.ts @@ -213,6 +213,15 @@ export function toTSScalarType( return frag`${opts.edgedbDatatypePrefix}edgedb.Range<${tn}>`; } + case "multirange": { + const tn = toTSScalarType( + types.get(type.multirange_element_id) as introspect.PrimitiveType, + types, + opts + ); + return frag`${opts.edgedbDatatypePrefix}edgedb.MultiRange<${tn}>`; + } + default: util.assertNever(type); } diff --git a/packages/generate/src/syntax/casting.ts b/packages/generate/src/syntax/casting.ts index a6b9c6ad6..a29e167e3 100644 --- a/packages/generate/src/syntax/casting.ts +++ b/packages/generate/src/syntax/casting.ts @@ -15,6 +15,7 @@ import type { TupleType, TypeSet, RangeType, + MultiRangeType, } from "./typesystem"; import type { cardutil } from "./cardinality"; @@ -60,6 +61,12 @@ export type assignableBy = T extends ScalarType ? scalarAssignableBy : never > + : T extends MultiRangeType + ? MultiRangeType< + scalarAssignableBy extends ScalarType + ? scalarAssignableBy + : never + > : never; export type pointerToAssignmentExpression< diff --git a/packages/generate/src/syntax/funcops.ts b/packages/generate/src/syntax/funcops.ts index bcf5829cb..211eb06f3 100644 --- a/packages/generate/src/syntax/funcops.ts +++ b/packages/generate/src/syntax/funcops.ts @@ -13,6 +13,7 @@ import type { TypeSet, RangeType, Expression, + MultiRangeType, } from "./typesystem"; import { cast } from "./cast"; import { isImplicitlyCastableTo, literalToTypeSet } from "./castMaps"; @@ -405,6 +406,15 @@ function compareType( ); } } + if (type.kind === "multirange") { + if (arg.__kind__ === TypeKind.multirange) { + return compareType( + typeSpec, + type.multirange_element_id, + (arg as any as MultiRangeType).__element__ as BaseType + ); + } + } if (type.kind === "object") { if (arg.__kind__ !== TypeKind.object) return { match: false }; diff --git a/packages/generate/src/syntax/hydrate.ts b/packages/generate/src/syntax/hydrate.ts index 1caf236c7..4b90b1415 100644 --- a/packages/generate/src/syntax/hydrate.ts +++ b/packages/generate/src/syntax/hydrate.ts @@ -217,6 +217,15 @@ export function makeType( return `range<${obj.__element__.__name__}>`; }); return obj; + } else if (type.kind === "multirange") { + obj.__kind__ = TypeKind.multirange; + util.defineGetter(obj, "__element__", () => { + return makeType(spec, type.multirange_element_id, literal, anytype); + }); + util.defineGetter(obj, "__name__", () => { + return `multirange<${obj.__element__.__name__}>`; + }); + return obj; } else { throw new Error("Invalid type."); } diff --git a/packages/generate/src/syntax/typesystem.ts b/packages/generate/src/syntax/typesystem.ts index ec482d916..ec777308b 100644 --- a/packages/generate/src/syntax/typesystem.ts +++ b/packages/generate/src/syntax/typesystem.ts @@ -9,7 +9,7 @@ import type { } from "edgedb/dist/reflection/index"; import { TypeKind } from "edgedb/dist/reflection/index"; import type { cardutil } from "./cardinality"; -import type { Range } from "edgedb"; +import type { Range, MultiRange } from "edgedb"; ////////////////// // BASETYPE @@ -244,7 +244,8 @@ export type SomeType = | TupleType | ObjectType | NamedTupleType - | RangeType; + | RangeType + | MultiRangeType; export interface PropertyDesc< Type extends BaseType = BaseType, @@ -405,7 +406,8 @@ export type PrimitiveType = | TupleType | NamedTupleType | ArrayType - | RangeType; + | RangeType + | MultiRangeType; export type PrimitiveTypeSet = TypeSet; @@ -672,6 +674,19 @@ export interface RangeType< __element__: Element; } +///////////////////////// +/// MULTIRANGE TYPE +///////////////////////// + +export interface MultiRangeType< + Element extends ScalarType = ScalarType, + Name extends string = `multirange<${Element["__name__"]}>` +> extends BaseType { + __name__: Name; + __kind__: TypeKind.multirange; + __element__: Element; +} + ///////////////////// /// TSTYPE COMPUTATION ///////////////////// @@ -694,6 +709,8 @@ export type BaseTypeToTsType< ? typeutil.flatten> : Type extends RangeType ? Range + : Type extends MultiRangeType + ? MultiRange : Type extends TupleType ? TupleItemsToTsType : Type extends NamedTupleType @@ -788,7 +805,8 @@ export type NonArrayType = | ObjectType | TupleType | NamedTupleType - | RangeType; + | RangeType + | MultiRangeType; export type AnyTupleType = TupleType | NamedTupleType; @@ -800,7 +818,9 @@ export type ParamType = | TupleType> | NamedTupleType<{ [k: string]: ParamType }> | RangeType + | MultiRangeType > | TupleType> | NamedTupleType<{ [k: string]: ParamType }> - | RangeType; + | RangeType + | MultiRangeType;