Skip to content

Commit

Permalink
wip Generator running in 4.0 now
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Oct 5, 2023
1 parent 73df1c7 commit b714e89
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/driver/src/reflection/analyzeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}>`;
Expand Down
1 change: 1 addition & 0 deletions packages/driver/src/reflection/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum TypeKind {
tuple = "tuple",
array = "array",
range = "range",
multirange = "multirange",
}

export enum ExpressionKind {
Expand Down
19 changes: 16 additions & 3 deletions packages/driver/src/reflection/queries/types.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -27,6 +27,7 @@ export type TypeKind =
| "array"
| "tuple"
| "range"
| "multirange"
| "unknown";

export type TypeProperties<T extends TypeKind> = {
Expand Down Expand Up @@ -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<UUID, Type>;

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
`;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion packages/generate/src/edgeql-js/generateCastMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export const generateCastMaps = (params: GeneratorParams) => {
dts`declare `,
t`type scalarLiterals =\n | ${Object.keys(literalToScalarMapping).join(
"\n | "
)}\n | edgedb.Range<any>;\n\n`,
)}\n | edgedb.Range<any> | edgedb.MultiRange<any>;\n\n`,
]);

f.writeln([
Expand Down Expand Up @@ -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<T['__element__']['__tstype__']>`]);
f.writeln([t` : T extends $.MultiRangeType`]);
f.writeln([t` ? edgedb.MultiRange<T['__element__']['__tstype__']>`]);
f.writeln([t` : never;`]);
f.writeln([
t`export `,
Expand Down Expand Up @@ -344,6 +346,9 @@ export const generateCastMaps = (params: GeneratorParams) => {
f.writeln([
t` T extends edgedb.Range<infer E> ? $.RangeType<literalToScalarType<E>> :`,
]);
f.writeln([
t` T extends edgedb.MultiRange<infer E> ? $.MultiRangeType<literalToScalarType<E>> :`,
]);
f.writeln([t` $.BaseType;\n\n`]);

f.writeln([
Expand Down
11 changes: 11 additions & 0 deletions packages/generate/src/edgeql-js/generateObjectTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/generate/src/funcoputil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__"]`;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/generate/src/genutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/generate/src/syntax/casting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
TupleType,
TypeSet,
RangeType,
MultiRangeType,
} from "./typesystem";
import type { cardutil } from "./cardinality";

Expand Down Expand Up @@ -60,6 +61,12 @@ export type assignableBy<T extends BaseType> = T extends ScalarType
? scalarAssignableBy<T["__element__"]>
: never
>
: T extends MultiRangeType
? MultiRangeType<
scalarAssignableBy<T["__element__"]> extends ScalarType
? scalarAssignableBy<T["__element__"]>
: never
>
: never;

export type pointerToAssignmentExpression<
Expand Down
10 changes: 10 additions & 0 deletions packages/generate/src/syntax/funcops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
TypeSet,
RangeType,
Expression,
MultiRangeType,
} from "./typesystem";
import { cast } from "./cast";
import { isImplicitlyCastableTo, literalToTypeSet } from "./castMaps";
Expand Down Expand Up @@ -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 };

Expand Down
9 changes: 9 additions & 0 deletions packages/generate/src/syntax/hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ export function makeType<T extends BaseType>(
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.");
}
Expand Down
30 changes: 25 additions & 5 deletions packages/generate/src/syntax/typesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -244,7 +244,8 @@ export type SomeType =
| TupleType
| ObjectType
| NamedTupleType
| RangeType;
| RangeType
| MultiRangeType;

export interface PropertyDesc<
Type extends BaseType = BaseType,
Expand Down Expand Up @@ -405,7 +406,8 @@ export type PrimitiveType =
| TupleType
| NamedTupleType
| ArrayType
| RangeType;
| RangeType
| MultiRangeType;

export type PrimitiveTypeSet = TypeSet<PrimitiveType, Cardinality>;

Expand Down Expand Up @@ -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
/////////////////////
Expand All @@ -694,6 +709,8 @@ export type BaseTypeToTsType<
? typeutil.flatten<ArrayTypeToTsType<Type, isParam>>
: Type extends RangeType
? Range<Type["__element__"]["__tsconsttype__"]>
: Type extends MultiRangeType
? MultiRange<Type["__element__"]["__tsconsttype__"]>
: Type extends TupleType
? TupleItemsToTsType<Type["__items__"], isParam>
: Type extends NamedTupleType
Expand Down Expand Up @@ -788,7 +805,8 @@ export type NonArrayType =
| ObjectType
| TupleType
| NamedTupleType
| RangeType;
| RangeType
| MultiRangeType;

export type AnyTupleType = TupleType | NamedTupleType;

Expand All @@ -800,7 +818,9 @@ export type ParamType =
| TupleType<typeutil.tupleOf<ParamType>>
| NamedTupleType<{ [k: string]: ParamType }>
| RangeType
| MultiRangeType
>
| TupleType<typeutil.tupleOf<ParamType>>
| NamedTupleType<{ [k: string]: ParamType }>
| RangeType;
| RangeType
| MultiRangeType;

0 comments on commit b714e89

Please sign in to comment.