Skip to content

Commit 8078204

Browse files
committed
Allow UnionPresentationSchema to be used in the union utility.
1 parent 97c515c commit 8078204

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/Command/ParameterParsing.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,14 @@ import {
2727
import { CompleteCommand, PartialCommand } from "./Command";
2828
import { ArgumentParseError, PromptRequiredError } from "./ParseErrors";
2929
import { TextPresentationRenderer } from "../TextReader/TextPresentationRenderer";
30+
import { PresentationTypeWithoutWrap } from "./Presentation";
3031
import {
31-
ObjectTypeFromPresentationType,
32-
PresentationTypeWithoutWrap,
33-
} from "./Presentation";
34-
import {
35-
ObjectTypeForPresentationSchema,
3632
PresentationSchema,
3733
PresentationSchemaType,
3834
checkPresentationSchema,
3935
printPresentationSchema,
4036
} from "./PresentationSchema";
37+
import { ObjectTypeFromAcceptor } from "./PresentationSchema";
4138

4239
export type ParameterParseFunction<
4340
Arguments extends unknown[] = unknown[],
@@ -191,12 +188,6 @@ export type DescribeParameter<ObjectType = unknown> = Omit<
191188
| PresentationTypeWithoutWrap<ObjectType>;
192189
};
193190

194-
export type ObjectTypeFromAcceptor<T> = T extends PresentationTypeWithoutWrap
195-
? ObjectTypeFromPresentationType<T>
196-
: T extends PresentationSchema
197-
? ObjectTypeForPresentationSchema<T>
198-
: never;
199-
200191
export type ExtractParameterObjectType<T extends DescribeParameter> =
201192
ObjectTypeFromAcceptor<T["acceptor"]>;
202193

src/Command/PresentationSchema.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
// https://github.com/the-draupnir-project/interface-manager
88
// </text>
99

10-
import { Presentation, PresentationTypeWithoutWrap } from "./Presentation";
10+
import {
11+
ObjectTypeFromPresentationType,
12+
Presentation,
13+
PresentationTypeWithoutWrap,
14+
} from "./Presentation";
1115

1216
export enum PresentationSchemaType {
1317
Single = "Single",
@@ -104,15 +108,40 @@ export function printPresentationSchema(schema: PresentationSchema): string {
104108
}
105109
}
106110

107-
type UnionOfObjectTypes<T extends PresentationTypeWithoutWrap[]> = {
108-
[P in keyof T]: T[P] extends PresentationTypeWithoutWrap<infer U> ? U : never;
111+
type Acceptor<ObjectType = unknown> =
112+
| PresentationSchema<ObjectType>
113+
| PresentationTypeWithoutWrap<ObjectType>;
114+
export type ObjectTypeFromAcceptor<T> = T extends PresentationTypeWithoutWrap
115+
? ObjectTypeFromPresentationType<T>
116+
: T extends PresentationSchema
117+
? ObjectTypeForPresentationSchema<T>
118+
: never;
119+
120+
type UnionOfObjectTypes<T extends Acceptor[]> = {
121+
[P in keyof T]: T[P] extends Acceptor<infer U> ? U : never;
109122
}[number];
110123

111-
export function union<TPresentationTypes extends PresentationTypeWithoutWrap[]>(
112-
...presentationTypes: TPresentationTypes
113-
): UnionPresentationSchema<UnionOfObjectTypes<TPresentationTypes>> {
124+
export function union<
125+
TAcceptor extends (PresentationTypeWithoutWrap | UnionPresentationSchema)[],
126+
>(
127+
...acceptors: TAcceptor
128+
): UnionPresentationSchema<UnionOfObjectTypes<TAcceptor>> {
129+
type PresentationTypeForUnion = PresentationTypeWithoutWrap<
130+
UnionOfObjectTypes<TAcceptor>
131+
>;
132+
const presentationTypes = acceptors.reduce<PresentationTypeForUnion[]>(
133+
(acc, acceptor) => {
134+
if ("schemaType" in acceptor) {
135+
acc.push(...(acceptor.variants as PresentationTypeForUnion[]));
136+
} else {
137+
acc.push(acceptor as PresentationTypeForUnion);
138+
}
139+
return acc;
140+
},
141+
[]
142+
);
114143
return {
115144
schemaType: PresentationSchemaType.Union,
116145
variants: presentationTypes,
117-
} as UnionPresentationSchema<UnionOfObjectTypes<TPresentationTypes>>;
146+
} as UnionPresentationSchema<UnionOfObjectTypes<TAcceptor>>;
118147
}

0 commit comments

Comments
 (0)