@shaclmate/compiler 4.0.77 → 4.0.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generators/ts/AbstractType.d.ts +5 -1
- package/dist/generators/ts/AbstractType.js +5 -0
- package/dist/generators/ts/DiscriminatedUnionType.d.ts +3 -2
- package/dist/generators/ts/DiscriminatedUnionType.js +67 -63
- package/dist/generators/ts/LazyOptionType.d.ts +1 -0
- package/dist/generators/ts/ObjectType.js +10 -2
- package/dist/generators/ts/_AbstractType/AbstractType_ConversionFunction.d.ts +9 -3
- package/dist/generators/ts/_AbstractType/AbstractType_ConversionFunction.js +18 -1
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_conversionFunctionExpression.d.ts +6 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_conversionFunctionExpression.js +86 -0
- package/dist/input/input.shaclmate.d.ts +116 -100
- package/dist/input/input.shaclmate.js +144 -130
- package/package.json +10 -8
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
2
|
import { Maybe } from "purify-ts";
|
|
3
3
|
import type { Logger } from "ts-log";
|
|
4
|
-
import
|
|
4
|
+
import { AbstractType_ConversionFunction } from "./_AbstractType/AbstractType_ConversionFunction.js";
|
|
5
5
|
import type { AbstractType_DiscriminantProperty } from "./_AbstractType/AbstractType_DiscriminantProperty.js";
|
|
6
6
|
import { AbstractType_GraphqlType } from "./_AbstractType/AbstractType_GraphqlType.js";
|
|
7
7
|
import { AbstractType_JsonType } from "./_AbstractType/AbstractType_JsonType.js";
|
|
@@ -273,6 +273,10 @@ export declare abstract class AbstractType {
|
|
|
273
273
|
}
|
|
274
274
|
export declare namespace AbstractType {
|
|
275
275
|
type ConversionFunction = AbstractType_ConversionFunction;
|
|
276
|
+
namespace ConversionFunction {
|
|
277
|
+
const SourceType: typeof AbstractType_ConversionFunction.SourceType;
|
|
278
|
+
type SourceType = AbstractType_ConversionFunction.SourceType;
|
|
279
|
+
}
|
|
276
280
|
type DiscriminantProperty = AbstractType_DiscriminantProperty;
|
|
277
281
|
namespace DiscriminantProperty {
|
|
278
282
|
type Value = AbstractType_DiscriminantProperty.Value;
|
|
@@ -6,6 +6,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
6
6
|
};
|
|
7
7
|
import { Maybe } from "purify-ts";
|
|
8
8
|
import { Memoize } from "typescript-memoize";
|
|
9
|
+
import { AbstractType_ConversionFunction } from "./_AbstractType/AbstractType_ConversionFunction.js";
|
|
9
10
|
import { AbstractType_GraphqlType } from "./_AbstractType/AbstractType_GraphqlType.js";
|
|
10
11
|
import { AbstractType_JsonType } from "./_AbstractType/AbstractType_JsonType.js";
|
|
11
12
|
import { AbstractType_JsType } from "./_AbstractType/AbstractType_JsType.js";
|
|
@@ -120,6 +121,10 @@ __decorate([
|
|
|
120
121
|
Memoize()
|
|
121
122
|
], AbstractType.prototype, "schemaExpression", null);
|
|
122
123
|
(function (AbstractType) {
|
|
124
|
+
let ConversionFunction;
|
|
125
|
+
(function (ConversionFunction) {
|
|
126
|
+
ConversionFunction.SourceType = AbstractType_ConversionFunction.SourceType;
|
|
127
|
+
})(ConversionFunction = AbstractType.ConversionFunction || (AbstractType.ConversionFunction = {}));
|
|
123
128
|
AbstractType.GraphqlType = AbstractType_GraphqlType;
|
|
124
129
|
AbstractType.JsType = AbstractType_JsType;
|
|
125
130
|
AbstractType.JsonType = AbstractType_JsonType;
|
|
@@ -24,7 +24,6 @@ export declare class DiscriminatedUnionType<MemberTypeT extends Type> extends Ab
|
|
|
24
24
|
get conversionFunction(): Maybe<AbstractType.ConversionFunction>;
|
|
25
25
|
get discriminantProperty(): Maybe<AbstractType.DiscriminantProperty>;
|
|
26
26
|
get equalsFunction(): Code;
|
|
27
|
-
protected get inlineExpression(): Code;
|
|
28
27
|
get filterFunction(): Code;
|
|
29
28
|
get filterType(): Code;
|
|
30
29
|
get fromRdfResourceValuesFunction(): Code;
|
|
@@ -40,8 +39,9 @@ export declare class DiscriminatedUnionType<MemberTypeT extends Type> extends Ab
|
|
|
40
39
|
get toRdfResourceValueTypes(): AbstractType["toRdfResourceValueTypes"];
|
|
41
40
|
get valueSparqlConstructTriplesFunction(): Code;
|
|
42
41
|
get valueSparqlWherePatternsFunction(): Code;
|
|
42
|
+
protected get inlineExpression(): Code;
|
|
43
43
|
protected get schemaInitializers(): readonly Code[];
|
|
44
|
-
|
|
44
|
+
private get conversionFunctionExpression();
|
|
45
45
|
fromJsonExpression({ variables, }: Parameters<AbstractType["fromJsonExpression"]>[0]): Code;
|
|
46
46
|
graphqlResolveExpression({ variables, }: {
|
|
47
47
|
variables: {
|
|
@@ -54,6 +54,7 @@ export declare class DiscriminatedUnionType<MemberTypeT extends Type> extends Ab
|
|
|
54
54
|
toJsonExpression({ variables, }: Parameters<AbstractType["toJsonExpression"]>[0]): Code;
|
|
55
55
|
toRdfResourceValuesExpression({ variables, }: Parameters<AbstractType["toRdfResourceValuesExpression"]>[0]): Code;
|
|
56
56
|
toStringExpression({ variables, }: Parameters<AbstractType["toStringExpression"]>[0]): Code;
|
|
57
|
+
protected staticModuleDeclarations(name: string): Record<string, Code>;
|
|
57
58
|
private readonly lazyMembers;
|
|
58
59
|
}
|
|
59
60
|
export declare namespace DiscriminatedUnionType {
|
|
@@ -7,6 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
import { Maybe } from "purify-ts";
|
|
8
8
|
import { invariant } from "ts-invariant";
|
|
9
9
|
import { Memoize } from "typescript-memoize";
|
|
10
|
+
import { DiscriminatedUnionType_conversionFunctionExpression } from "./_DiscriminatedUnionType/DiscriminatedUnionType_conversionFunctionExpression.js";
|
|
10
11
|
import { DiscriminatedUnionType_equalsFunctionExpression } from "./_DiscriminatedUnionType/DiscriminatedUnionType_equalsFunctionExpression.js";
|
|
11
12
|
import { DiscriminatedUnionType_filterFunctionExpression } from "./_DiscriminatedUnionType/DiscriminatedUnionType_filterFunctionExpression.js";
|
|
12
13
|
import { DiscriminatedUnionType_filterTypeExpression } from "./_DiscriminatedUnionType/DiscriminatedUnionType_filterTypeExpression.js";
|
|
@@ -64,13 +65,14 @@ export class DiscriminatedUnionType extends AbstractType {
|
|
|
64
65
|
if (discriminant.kind === "Typeof") {
|
|
65
66
|
return code `(${joinCode(discriminantValues.map((discriminantValue) => code `typeof ${instance} === ${literalOf(discriminantValue)}`), { on: " || " })})`;
|
|
66
67
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
68
|
+
// Causes problems in mixed Object | other dicriminated unions
|
|
69
|
+
// if (discriminant.kind === "Intrinsic" && !json) {
|
|
70
|
+
// switch (member.type.kind) {
|
|
71
|
+
// case "Object":
|
|
72
|
+
// case "ObjectDiscriminatedUnion":
|
|
73
|
+
// return code`${member.type.name.unsafeCoerce()}.is${member.type.name.unsafeCoerce()}(${instance})`;
|
|
74
|
+
// }
|
|
75
|
+
// }
|
|
74
76
|
const discriminantName = json
|
|
75
77
|
? discriminant.jsonName
|
|
76
78
|
: discriminant.name;
|
|
@@ -125,23 +127,15 @@ export class DiscriminatedUnionType extends AbstractType {
|
|
|
125
127
|
});
|
|
126
128
|
}
|
|
127
129
|
get conversionFunction() {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
expression: this.expression,
|
|
138
|
-
jsType: {
|
|
139
|
-
instanceof: "Object",
|
|
140
|
-
typeof: "object",
|
|
141
|
-
},
|
|
142
|
-
},
|
|
143
|
-
],
|
|
144
|
-
});
|
|
130
|
+
if (this.conversionFunctionExpression.isNothing()) {
|
|
131
|
+
return this.conversionFunctionExpression;
|
|
132
|
+
}
|
|
133
|
+
return this.name
|
|
134
|
+
.map((name) => Maybe.of({
|
|
135
|
+
code: code `${name}.convert`,
|
|
136
|
+
sourceTypes: this.conversionFunctionExpression.extract().sourceTypes,
|
|
137
|
+
}))
|
|
138
|
+
.orDefault(this.conversionFunctionExpression);
|
|
145
139
|
}
|
|
146
140
|
get discriminantProperty() {
|
|
147
141
|
switch (this.discriminant.kind) {
|
|
@@ -174,9 +168,6 @@ export class DiscriminatedUnionType extends AbstractType {
|
|
|
174
168
|
.map((name) => code `${name}.equals`)
|
|
175
169
|
.orDefault(DiscriminatedUnionType_equalsFunctionExpression.call(this));
|
|
176
170
|
}
|
|
177
|
-
get inlineExpression() {
|
|
178
|
-
return DiscriminatedUnionType_inlineExpression.call(this);
|
|
179
|
-
}
|
|
180
171
|
get filterFunction() {
|
|
181
172
|
return this.name
|
|
182
173
|
.map((name) => code `${name}.filter`)
|
|
@@ -269,16 +260,59 @@ export class DiscriminatedUnionType extends AbstractType {
|
|
|
269
260
|
.map((name) => code `${name}.valueSparqlWherePatterns`)
|
|
270
261
|
.orDefault(DiscriminatedUnionType_valueSparqlWherePatternsFunctionExpression.call(this));
|
|
271
262
|
}
|
|
263
|
+
get inlineExpression() {
|
|
264
|
+
return DiscriminatedUnionType_inlineExpression.call(this);
|
|
265
|
+
}
|
|
272
266
|
get schemaInitializers() {
|
|
273
267
|
return super.schemaInitializers.concat(code `members: { ${joinCode(this.members.map(({ discriminantValues, type, primaryDiscriminantValue }) => code `${literalOf(primaryDiscriminantValue)}: ${{
|
|
274
268
|
discriminantValues: discriminantValues,
|
|
275
269
|
type: type.schema,
|
|
276
270
|
}}`), { on: "," })} }`);
|
|
277
271
|
}
|
|
272
|
+
get conversionFunctionExpression() {
|
|
273
|
+
return DiscriminatedUnionType_conversionFunctionExpression.call(this);
|
|
274
|
+
}
|
|
275
|
+
fromJsonExpression({ variables, }) {
|
|
276
|
+
return code `${this.name.map((name) => code `${name}.fromJson`).orDefault(DiscriminatedUnionType_fromJsonFunctionExpression.call(this))}(${variables.value})`;
|
|
277
|
+
}
|
|
278
|
+
graphqlResolveExpression({ variables, }) {
|
|
279
|
+
return variables.value;
|
|
280
|
+
}
|
|
281
|
+
jsonSchema({ context, }) {
|
|
282
|
+
const expression = this.name
|
|
283
|
+
.map((name) => code `${name}.Json.schema()`)
|
|
284
|
+
.orDefault(DiscriminatedUnionType_jsonSchemaExpression.call(this));
|
|
285
|
+
if (context === "property" && this.recursive) {
|
|
286
|
+
return code `${this.reusables.imports.z}.lazy((): ${this.reusables.imports.z}.ZodType<${this.jsonType().expression}> => ${expression})`;
|
|
287
|
+
}
|
|
288
|
+
return expression;
|
|
289
|
+
}
|
|
290
|
+
jsonType() {
|
|
291
|
+
return this.name
|
|
292
|
+
.map((name) => new AbstractType.JsonType(code `${name}.Json`))
|
|
293
|
+
.orDefault(DiscriminatedUnionType_jsonTypeLiteral.call(this));
|
|
294
|
+
}
|
|
295
|
+
jsonUiSchemaElement() {
|
|
296
|
+
return Maybe.empty();
|
|
297
|
+
}
|
|
298
|
+
toJsonExpression({ variables, }) {
|
|
299
|
+
return code `${this.name.map((name) => code `${name}.toJson`).orDefault(DiscriminatedUnionType_toJsonFunctionExpression.call(this))}(${variables.value})`;
|
|
300
|
+
}
|
|
301
|
+
toRdfResourceValuesExpression({ variables, }) {
|
|
302
|
+
const { value: valueVariable, ...otherVariables } = variables;
|
|
303
|
+
return code `${this.name.map((name) => code `${name}.toRdfResourceValues`).orDefault(DiscriminatedUnionType_toRdfResourceValuesFunctionExpression.call(this))}(${valueVariable}, ${otherVariables})`;
|
|
304
|
+
}
|
|
305
|
+
toStringExpression({ variables, }) {
|
|
306
|
+
return code `${this.name.map((name) => code `${name}.${this.configuration.syntheticNamePrefix}toString`).orDefault(DiscriminatedUnionType_toStringFunctionExpression.call(this))}(${variables.value})`;
|
|
307
|
+
}
|
|
278
308
|
staticModuleDeclarations(name) {
|
|
279
309
|
const staticModuleDeclarations = {
|
|
280
310
|
...super.staticModuleDeclarations(name),
|
|
281
311
|
};
|
|
312
|
+
this.conversionFunctionExpression.ifJust((conversionFunction) => {
|
|
313
|
+
staticModuleDeclarations["convert"] =
|
|
314
|
+
code `export const convert = ${conversionFunction.code};`;
|
|
315
|
+
});
|
|
282
316
|
if (this.configuration.features.has("Object.equals")) {
|
|
283
317
|
staticModuleDeclarations["equals"] =
|
|
284
318
|
code `export const equals = ${DiscriminatedUnionType_equalsFunctionExpression.call(this)};`;
|
|
@@ -338,39 +372,6 @@ export namespace Json {
|
|
|
338
372
|
}
|
|
339
373
|
return staticModuleDeclarations;
|
|
340
374
|
}
|
|
341
|
-
fromJsonExpression({ variables, }) {
|
|
342
|
-
return code `${this.name.map((name) => code `${name}.fromJson`).orDefault(DiscriminatedUnionType_fromJsonFunctionExpression.call(this))}(${variables.value})`;
|
|
343
|
-
}
|
|
344
|
-
graphqlResolveExpression({ variables, }) {
|
|
345
|
-
return variables.value;
|
|
346
|
-
}
|
|
347
|
-
jsonSchema({ context, }) {
|
|
348
|
-
const expression = this.name
|
|
349
|
-
.map((name) => code `${name}.Json.schema()`)
|
|
350
|
-
.orDefault(DiscriminatedUnionType_jsonSchemaExpression.call(this));
|
|
351
|
-
if (context === "property" && this.recursive) {
|
|
352
|
-
return code `${this.reusables.imports.z}.lazy((): ${this.reusables.imports.z}.ZodType<${this.jsonType().expression}> => ${expression})`;
|
|
353
|
-
}
|
|
354
|
-
return expression;
|
|
355
|
-
}
|
|
356
|
-
jsonType() {
|
|
357
|
-
return this.name
|
|
358
|
-
.map((name) => new AbstractType.JsonType(code `${name}.Json`))
|
|
359
|
-
.orDefault(DiscriminatedUnionType_jsonTypeLiteral.call(this));
|
|
360
|
-
}
|
|
361
|
-
jsonUiSchemaElement() {
|
|
362
|
-
return Maybe.empty();
|
|
363
|
-
}
|
|
364
|
-
toJsonExpression({ variables, }) {
|
|
365
|
-
return code `${this.name.map((name) => code `${name}.toJson`).orDefault(DiscriminatedUnionType_toJsonFunctionExpression.call(this))}(${variables.value})`;
|
|
366
|
-
}
|
|
367
|
-
toRdfResourceValuesExpression({ variables, }) {
|
|
368
|
-
const { value: valueVariable, ...otherVariables } = variables;
|
|
369
|
-
return code `${this.name.map((name) => code `${name}.toRdfResourceValues`).orDefault(DiscriminatedUnionType_toRdfResourceValuesFunctionExpression.call(this))}(${valueVariable}, ${otherVariables})`;
|
|
370
|
-
}
|
|
371
|
-
toStringExpression({ variables, }) {
|
|
372
|
-
return code `${this.name.map((name) => code `${name}.${this.configuration.syntheticNamePrefix}toString`).orDefault(DiscriminatedUnionType_toStringFunctionExpression.call(this))}(${variables.value})`;
|
|
373
|
-
}
|
|
374
375
|
lazyMembers;
|
|
375
376
|
}
|
|
376
377
|
__decorate([
|
|
@@ -382,9 +383,6 @@ __decorate([
|
|
|
382
383
|
__decorate([
|
|
383
384
|
Memoize()
|
|
384
385
|
], DiscriminatedUnionType.prototype, "equalsFunction", null);
|
|
385
|
-
__decorate([
|
|
386
|
-
Memoize()
|
|
387
|
-
], DiscriminatedUnionType.prototype, "inlineExpression", null);
|
|
388
386
|
__decorate([
|
|
389
387
|
Memoize()
|
|
390
388
|
], DiscriminatedUnionType.prototype, "filterFunction", null);
|
|
@@ -421,6 +419,12 @@ __decorate([
|
|
|
421
419
|
__decorate([
|
|
422
420
|
Memoize()
|
|
423
421
|
], DiscriminatedUnionType.prototype, "valueSparqlWherePatternsFunction", null);
|
|
422
|
+
__decorate([
|
|
423
|
+
Memoize()
|
|
424
|
+
], DiscriminatedUnionType.prototype, "inlineExpression", null);
|
|
425
|
+
__decorate([
|
|
426
|
+
Memoize()
|
|
427
|
+
], DiscriminatedUnionType.prototype, "conversionFunctionExpression", null);
|
|
424
428
|
__decorate([
|
|
425
429
|
Memoize()
|
|
426
430
|
], DiscriminatedUnionType.prototype, "jsonType", null);
|
|
@@ -17,6 +17,7 @@ declare const Super: (abstract new ({ partialType, resolveType, ...superParamete
|
|
|
17
17
|
}) => AbstractLazyType<OptionType<AbstractLazyType.ItemTypeConstraint>, OptionType<AbstractLazyType.ItemTypeConstraint>>) & {
|
|
18
18
|
readonly GraphqlType: typeof import("./_AbstractType/AbstractType_GraphqlType.js").AbstractType_GraphqlType;
|
|
19
19
|
readonly JsonType: typeof import("./_AbstractType/AbstractType_JsonType.js").AbstractType_JsonType;
|
|
20
|
+
ConversionFunction: typeof import("./AbstractType.js").AbstractType.ConversionFunction;
|
|
20
21
|
readonly JsType: typeof import("./_AbstractType/AbstractType_JsType.js").AbstractType_JsType;
|
|
21
22
|
};
|
|
22
23
|
export declare class LazyOptionType extends Super {
|
|
@@ -71,11 +71,19 @@ export class ObjectType extends AbstractType {
|
|
|
71
71
|
if (this.name.isJust()) {
|
|
72
72
|
return Maybe.empty();
|
|
73
73
|
}
|
|
74
|
+
const sourceTypePropertySignatures = [];
|
|
75
|
+
for (const property of this.properties) {
|
|
76
|
+
property.constructorParameter.ifJust((propertyConstructorParameter) => {
|
|
77
|
+
sourceTypePropertySignatures.push(propertyConstructorParameter.signature);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const sourceTypeExpression = code `{ ${joinCode(sourceTypePropertySignatures)} }`;
|
|
81
|
+
const syntheticNamePrefix = this.configuration.syntheticNamePrefix;
|
|
74
82
|
return Maybe.of({
|
|
75
|
-
code: this.createFunction,
|
|
83
|
+
code: code `(<${syntheticNamePrefix}DefaultNamespaceT extends ${this.reusables.snippets.NamespaceBuilder} = ${this.reusables.snippets.NamespaceBuilder}>(value: ${sourceTypeExpression}, ${syntheticNamePrefix}defaultNamespace?: ${syntheticNamePrefix}DefaultNamespaceT) => ${this.createFunction}({ ...value, ${syntheticNamePrefix}defaultNamespace }))`,
|
|
76
84
|
sourceTypes: [
|
|
77
85
|
{
|
|
78
|
-
expression:
|
|
86
|
+
expression: sourceTypeExpression,
|
|
79
87
|
jsType: {
|
|
80
88
|
instanceof: "Object",
|
|
81
89
|
typeof: "object",
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import type { Code } from "../ts-poet-wrapper.js";
|
|
2
|
-
import
|
|
2
|
+
import { AbstractType_JsType } from "./AbstractType_JsType.js";
|
|
3
3
|
export interface AbstractType_ConversionFunction {
|
|
4
4
|
readonly code: Code;
|
|
5
|
-
readonly sourceTypes:
|
|
5
|
+
readonly sourceTypes: AbstractType_ConversionFunction.SourceType[];
|
|
6
|
+
}
|
|
7
|
+
export declare namespace AbstractType_ConversionFunction {
|
|
8
|
+
interface SourceType {
|
|
6
9
|
readonly expression: Code;
|
|
7
10
|
readonly jsType: AbstractType_JsType;
|
|
8
|
-
}
|
|
11
|
+
}
|
|
12
|
+
namespace SourceType {
|
|
13
|
+
function equals(left: SourceType, right: SourceType): boolean;
|
|
14
|
+
}
|
|
9
15
|
}
|
|
10
16
|
//# sourceMappingURL=AbstractType_ConversionFunction.d.ts.map
|
|
@@ -1,2 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import { codeEquals } from "../codeEquals.js";
|
|
2
|
+
import { AbstractType_JsType } from "./AbstractType_JsType.js";
|
|
3
|
+
export var AbstractType_ConversionFunction;
|
|
4
|
+
(function (AbstractType_ConversionFunction) {
|
|
5
|
+
let SourceType;
|
|
6
|
+
(function (SourceType) {
|
|
7
|
+
function equals(left, right) {
|
|
8
|
+
if (!codeEquals(left.expression, right.expression)) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
if (!AbstractType_JsType.equals(left.jsType, right.jsType)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
SourceType.equals = equals;
|
|
17
|
+
})(SourceType = AbstractType_ConversionFunction.SourceType || (AbstractType_ConversionFunction.SourceType = {}));
|
|
18
|
+
})(AbstractType_ConversionFunction || (AbstractType_ConversionFunction = {}));
|
|
2
19
|
//# sourceMappingURL=AbstractType_ConversionFunction.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Maybe } from "purify-ts";
|
|
2
|
+
import { AbstractType } from "../AbstractType.js";
|
|
3
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
4
|
+
import type { Type } from "../Type.js";
|
|
5
|
+
export declare function DiscriminatedUnionType_conversionFunctionExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Maybe<AbstractType.ConversionFunction>;
|
|
6
|
+
//# sourceMappingURL=DiscriminatedUnionType_conversionFunctionExpression.d.ts.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Maybe } from "purify-ts";
|
|
2
|
+
import { invariant } from "ts-invariant";
|
|
3
|
+
import { AbstractType } from "../AbstractType.js";
|
|
4
|
+
import { code, joinCode } from "../ts-poet-wrapper.js";
|
|
5
|
+
export function DiscriminatedUnionType_conversionFunctionExpression() {
|
|
6
|
+
if (this.discriminant.kind === "Typeof") {
|
|
7
|
+
// If the members are discriminated by typeof, they can all be used as source types.
|
|
8
|
+
return Maybe.of({
|
|
9
|
+
code: code `${this.reusables.snippets.identityConversionFunction}`,
|
|
10
|
+
sourceTypes: this.members.flatMap(({ type }) => type.jsTypes.map((jsType) => ({
|
|
11
|
+
expression: type.expression,
|
|
12
|
+
jsType,
|
|
13
|
+
}))),
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
if (this.discriminant.kind === "Intrinsic") {
|
|
17
|
+
// Allow discriminated unions with intrinsic discriminants (e.g., a "termType" property) to accept additional
|
|
18
|
+
// source types with typeofs besides "object".
|
|
19
|
+
// @rdfx/builder relies on discriminated unions of IRI | SomeObjectType and forces SomeObjectType to use the discriminant property
|
|
20
|
+
// "termType" so the two members share it. These properties should accept (keyof DefaultNamespaceT & string) | NamedNode | SomeObjectType source types --
|
|
21
|
+
// the "string" typeof for IRI and then "object" identity typeofs for the IRI and SomeObjectType, respectively.
|
|
22
|
+
const memberIdentitySourceTypes = [];
|
|
23
|
+
const otherMemberConversionsByTypeof = new Map();
|
|
24
|
+
for (const member of this.members) {
|
|
25
|
+
const memberConversionFunction = member.type.conversionFunction.extract();
|
|
26
|
+
const memberIdentitySourceType = {
|
|
27
|
+
expression: member.type.expression,
|
|
28
|
+
jsType: {
|
|
29
|
+
instanceof: "Object",
|
|
30
|
+
typeof: "object",
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
memberIdentitySourceTypes.push(memberIdentitySourceType);
|
|
34
|
+
if (!memberConversionFunction) {
|
|
35
|
+
// Identity conversion
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const memberSourceTypes = memberConversionFunction.sourceTypes;
|
|
39
|
+
invariant(memberSourceTypes.length > 1, "member type should return Maybe.empty() instead of returning an identity conversion alone");
|
|
40
|
+
let haveMemberIdentitySourceType = false;
|
|
41
|
+
for (const memberSourceType of memberSourceTypes) {
|
|
42
|
+
if (AbstractType.ConversionFunction.SourceType.equals(memberSourceType, memberIdentitySourceType)) {
|
|
43
|
+
haveMemberIdentitySourceType = true;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (memberSourceType.jsType.typeof === "object") {
|
|
47
|
+
this.logger.debug("%s: member type %s conversion function has more than one source type with typeof === 'object'", this, member.type);
|
|
48
|
+
return Maybe.empty();
|
|
49
|
+
}
|
|
50
|
+
if (otherMemberConversionsByTypeof.has(memberSourceType.jsType.typeof)) {
|
|
51
|
+
this.logger.debug("%s: member type %s conversion function has a source type with the same non-object typeof as another member type", this, member.type);
|
|
52
|
+
return Maybe.empty();
|
|
53
|
+
}
|
|
54
|
+
otherMemberConversionsByTypeof.set(memberSourceType.jsType.typeof, [
|
|
55
|
+
memberSourceType,
|
|
56
|
+
memberConversionFunction,
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
invariant(haveMemberIdentitySourceType, "member conversion source types should include the member type itself");
|
|
60
|
+
}
|
|
61
|
+
invariant(memberIdentitySourceTypes.length === this.members.length);
|
|
62
|
+
if (otherMemberConversionsByTypeof.size === 0) {
|
|
63
|
+
return Maybe.empty();
|
|
64
|
+
}
|
|
65
|
+
const otherMemberConversions = [...otherMemberConversionsByTypeof.values()];
|
|
66
|
+
const sourceTypes = memberIdentitySourceTypes.concat([
|
|
67
|
+
...otherMemberConversions.map((_) => _[0]),
|
|
68
|
+
]);
|
|
69
|
+
const syntheticNamePrefix = this.configuration.syntheticNamePrefix;
|
|
70
|
+
return Maybe.of({
|
|
71
|
+
code: code `\
|
|
72
|
+
(<${syntheticNamePrefix}DefaultNamespaceT extends ${this.reusables.snippets.NamespaceBuilder} = ${this.reusables.snippets.NamespaceBuilder}>(value: ${joinCode(sourceTypes.map((sourceType) => sourceType.expression), { on: " | " })}, defaultNamespace?: ${syntheticNamePrefix}DefaultNamespaceT): ${this.reusables.imports.Either}<Error, ${this.expression}> => {
|
|
73
|
+
switch (typeof value) {
|
|
74
|
+
case "object": return ${this.reusables.imports.Either}.of(value);
|
|
75
|
+
${joinCode(otherMemberConversions.map(([otherMemberSourceType, otherMemberConversionFunction]) => code `case "${otherMemberSourceType.jsType.typeof}": return ${otherMemberConversionFunction.code}(value, defaultNamespace);`), { on: "\n" })}
|
|
76
|
+
default:
|
|
77
|
+
value satisfies never;
|
|
78
|
+
throw new Error("should never reach this point");
|
|
79
|
+
}
|
|
80
|
+
})`,
|
|
81
|
+
sourceTypes,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return Maybe.empty();
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=DiscriminatedUnionType_conversionFunctionExpression.js.map
|