Skip to content

Commit

Permalink
Strongly type __values__ on EnumType (#442)
Browse files Browse the repository at this point in the history
* Strongly type __values__ on EnumType

* Fix collection test
  • Loading branch information
colinhacks authored Sep 7, 2022
1 parent 1e5ad72 commit feb867f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 33 deletions.
8 changes: 3 additions & 5 deletions qb/playground.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// tslint:disable:no-console
import {setupTests} from "./test/setupTeardown";
import e, {Genre} from "./dbschema/edgeql-js";
import e from "./dbschema/edgeql-js";
import {createClient, Duration, IsolationLevel} from "edgedb";
import type {typeutil} from "edgedb/dist/reflection";

async function run() {
await setupTests();
Expand Down Expand Up @@ -30,10 +31,7 @@ async function run() {
readonly: false,
});

const query = e.select(e.Movie, m => ({
release_year: true,
filter: e.op(m.title, "=", "The Avengers"),
}));
const query = e.datetime(new Date());

console.log(query.toEdgeQL());
const result = await query.run(client);
Expand Down
8 changes: 4 additions & 4 deletions qb/test/collections.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Client, $} from "edgedb";
import e, {$infer} from "../dbschema/edgeql-js";
import type {$VersionStageλEnum} from "../dbschema/edgeql-js/modules/sys";
import e, {$infer, sys} from "../dbschema/edgeql-js";
// import type {$VersionStageλEnum} from "../dbschema/edgeql-js/modules/sys";
import {tc} from "./setupTeardown";

import {setupTests, teardownTests, TestData} from "./setupTeardown";
Expand Down Expand Up @@ -390,12 +390,12 @@ test("non literal tuples", async () => {
ver: {
major: number;
minor: number;
stage: `${$VersionStageλEnum}`;
stage: `${sys.VersionStage}`;
stage_no: number;
local: string[];
};
verMajor: number;
verStage: `${$VersionStageλEnum}`;
verStage: `${sys.VersionStage}`;
verLocal: string[];
verLocal0: string;
}
Expand Down
22 changes: 4 additions & 18 deletions src/reflection/generators/generateScalars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
toTSScalarType,
scalarToLiteralMapping,
} from "../util/genutil";
import {dts, js, r, t, ts} from "../builders";
import {dts, r, t, ts} from "../builders";
import type {GeneratorParams} from "../generate";
import {typeMapping} from "../queries/getTypes";

Expand Down Expand Up @@ -88,22 +88,6 @@ export const generateScalars = (params: GeneratorParams) => {

// generate enum
if (type.enum_values && type.enum_values.length) {
sc.writeln([
dts`declare `,
t`enum `,
js`const `,
...frag`${ref}λEnum `,
js`= `,
`{`,
]);
sc.indented(() => {
for (const val of type.enum_values!) {
sc.writeln([toIdent(val), t` = `, js`: `, quote(val), `,`]);
}
});
sc.writeln([`}`]);
sc.addExport(frag`${ref}λEnum`);

sc.writeln([
t`export `,
dts`declare `,
Expand All @@ -112,7 +96,9 @@ export const generateScalars = (params: GeneratorParams) => {
val => t` ${toIdent(val)}: $.$expr_Literal<${ref}>;\n`
),
t`} & `,
t`$.EnumType<${quote(type.name)}, \`\${${ref}λEnum}\`>;`,
t`$.EnumType<${quote(type.name)}, [${type.enum_values
.map(val => quote(val))
.join(", ")}]>;`,
]);
sc.writeln([
dts`declare `,
Expand Down
11 changes: 5 additions & 6 deletions src/reflection/typesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,12 @@ export type ExpressionMethods<Set extends TypeSet> = {
//////////////////
export interface EnumType<
Name extends string = string,
TsType extends any = any
Values extends [string, ...string[]] = [string, ...string[]]
> extends BaseType {
__kind__: TypeKind.enum;
__tstype__: TsType;
__tstype__: Values[number];
__name__: Name;
__values__: string[];
// (val: TsType | Vals): $expr_Literal<this>;
__values__: Values;
}

//////////////////
Expand Down Expand Up @@ -663,13 +662,13 @@ export type BaseTypeToTsType<Type extends BaseType> = Type extends ScalarType
: Type extends EnumType
? Type["__tstype__"]
: Type extends ArrayType<any>
? ArrayTypeToTsType<Type>
? typeutil.flatten<ArrayTypeToTsType<Type>>
: Type extends RangeType
? Range<Type["__element__"]["__tsconsttype__"]>
: Type extends TupleType
? TupleItemsToTsType<Type["__items__"]>
: Type extends NamedTupleType
? NamedTupleTypeToTsType<Type>
? typeutil.flatten<NamedTupleTypeToTsType<Type>>
: Type extends ObjectType
? typeutil.flatten<
computeObjectShape<Type["__pointers__"], Type["__shape__"]>
Expand Down

0 comments on commit feb867f

Please sign in to comment.