@shaclmate/compiler 4.0.60 → 4.0.61
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/DefaultValueType.d.ts +6 -6
- package/dist/generators/ts/DiscriminatedUnionType.d.ts +3 -53
- package/dist/generators/ts/DiscriminatedUnionType.js +46 -430
- package/dist/generators/ts/ObjectDiscriminatedUnionType.d.ts +0 -8
- package/dist/generators/ts/ObjectDiscriminatedUnionType.js +17 -152
- package/dist/generators/ts/TsGenerator.d.ts +4 -0
- package/dist/generators/ts/TsGenerator.js +6 -0
- package/dist/generators/ts/TypeFactory.js +0 -1
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_Discriminant.d.ts +30 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_Discriminant.js +2 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_equalsFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_equalsFunctionExpression.js +12 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_filterFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_filterFunctionExpression.js +24 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_filterTypeExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_filterTypeExpression.js +10 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_fromJsonFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_fromJsonFunctionExpression.js +14 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_fromRdfResourceValuesFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_fromRdfResourceValuesFunctionExpression.js +22 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_hashFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_hashFunctionExpression.js +9 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_inferDiscriminant.d.ts +8 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_inferDiscriminant.js +133 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_inlineExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_inlineExpression.js +31 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_jsonSchemaExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_jsonSchemaExpression.js +32 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_jsonTypeLiteral.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_jsonTypeLiteral.js +26 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_schemaTypeExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_schemaTypeExpression.js +11 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_toJsonFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_toJsonFunctionExpression.js +10 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_toRdfResourceValuesFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_toRdfResourceValuesFunctionExpression.js +30 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_toStringFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_toStringFunctionExpression.js +12 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_valueSparqlConstructTriplesFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_valueSparqlConstructTriplesFunctionExpression.js +13 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_valueSparqlWherePatternsFunctionExpression.d.ts +5 -0
- package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_valueSparqlWherePatternsFunctionExpression.js +13 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_focusSparqlConstructTriplesFunctionDeclaration.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_focusSparqlConstructTriplesFunctionDeclaration.js +13 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_focusSparqlWherePatternsFunctionDeclaration.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_focusSparqlWherePatternsFunctionDeclaration.js +32 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_fromRdfResourceFunctionDeclaration.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_fromRdfResourceFunctionDeclaration.js +17 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_graphqlTypeVariableStatement.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_graphqlTypeVariableStatement.js +19 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_identifierTypeDeclarations.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_identifierTypeDeclarations.js +14 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_isTypeFunctionDeclaration.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_isTypeFunctionDeclaration.js +16 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_schemaVariableStatement.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_schemaVariableStatement.js +42 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_toRdfResourceFunctionDeclaration.d.ts +4 -0
- package/dist/generators/ts/_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_toRdfResourceFunctionDeclaration.js +15 -0
- package/dist/generators/ts/_ObjectType/ObjectType_DiscriminantProperty.d.ts +4 -4
- package/dist/generators/ts/_ObjectType/ObjectType_DiscriminantProperty.js +15 -3
- package/package.json +55 -4
|
@@ -4,15 +4,21 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
-
import { PropertyPath } from "@rdfx/resource";
|
|
8
|
-
import { pascalCase } from "change-case";
|
|
9
7
|
import { Memoize } from "typescript-memoize";
|
|
8
|
+
import { ObjectDiscriminatedUnionType_focusSparqlConstructTriplesFunctionDeclaration } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_focusSparqlConstructTriplesFunctionDeclaration.js";
|
|
9
|
+
import { ObjectDiscriminatedUnionType_focusSparqlWherePatternsFunctionDeclaration } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_focusSparqlWherePatternsFunctionDeclaration.js";
|
|
10
|
+
import { ObjectDiscriminatedUnionType_fromRdfResourceFunctionDeclaration } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_fromRdfResourceFunctionDeclaration.js";
|
|
11
|
+
import { ObjectDiscriminatedUnionType_graphqlTypeVariableStatement } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_graphqlTypeVariableStatement.js";
|
|
12
|
+
import { ObjectDiscriminatedUnionType_identifierTypeDeclarations } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_identifierTypeDeclarations.js";
|
|
13
|
+
import { ObjectDiscriminatedUnionType_isTypeFunctionDeclaration } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_isTypeFunctionDeclaration.js";
|
|
14
|
+
import { ObjectDiscriminatedUnionType_schemaVariableStatement } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_schemaVariableStatement.js";
|
|
15
|
+
import { ObjectDiscriminatedUnionType_toRdfResourceFunctionDeclaration } from "./_ObjectDiscriminatedUnionType/ObjectDiscriminatedUnionType_toRdfResourceFunctionDeclaration.js";
|
|
10
16
|
import { ObjectType_objectSetMethodNames } from "./_ObjectType/ObjectType_objectSetMethodNames.js";
|
|
11
17
|
import { ObjectType_sparqlConstructQueryFunctionDeclaration } from "./_ObjectType/ObjectType_sparqlConstructQueryFunctionDeclaration.js";
|
|
12
18
|
import { ObjectType_sparqlConstructQueryStringFunctionDeclaration } from "./_ObjectType/ObjectType_sparqlConstructQueryStringFunctionDeclaration.js";
|
|
13
19
|
import { DiscriminatedUnionType } from "./DiscriminatedUnionType.js";
|
|
14
20
|
import { singleEntryRecord } from "./singleEntryRecord.js";
|
|
15
|
-
import { code
|
|
21
|
+
import { code } from "./ts-poet-wrapper.js";
|
|
16
22
|
export class ObjectDiscriminatedUnionType extends DiscriminatedUnionType {
|
|
17
23
|
kind = "ObjectDiscriminatedUnion";
|
|
18
24
|
get identifierTypeAlias() {
|
|
@@ -40,13 +46,13 @@ export class ObjectDiscriminatedUnionType extends DiscriminatedUnionType {
|
|
|
40
46
|
const name = this.name.unsafeCoerce();
|
|
41
47
|
return {
|
|
42
48
|
...super.staticModuleDeclarations,
|
|
43
|
-
...this
|
|
44
|
-
...this
|
|
45
|
-
...this
|
|
46
|
-
...this
|
|
47
|
-
...this
|
|
48
|
-
...this
|
|
49
|
-
...this
|
|
49
|
+
...ObjectDiscriminatedUnionType_identifierTypeDeclarations.call(this),
|
|
50
|
+
...ObjectDiscriminatedUnionType_focusSparqlConstructTriplesFunctionDeclaration.call(this),
|
|
51
|
+
...ObjectDiscriminatedUnionType_focusSparqlWherePatternsFunctionDeclaration.call(this),
|
|
52
|
+
...ObjectDiscriminatedUnionType_fromRdfResourceFunctionDeclaration.call(this),
|
|
53
|
+
...ObjectDiscriminatedUnionType_graphqlTypeVariableStatement.call(this),
|
|
54
|
+
...ObjectDiscriminatedUnionType_isTypeFunctionDeclaration.call(this),
|
|
55
|
+
...ObjectDiscriminatedUnionType_schemaVariableStatement.call(this),
|
|
50
56
|
...(this.configuration.features.has("Object.SPARQL")
|
|
51
57
|
? {
|
|
52
58
|
...singleEntryRecord("sparqlConstructQuery", ObjectType_sparqlConstructQueryFunctionDeclaration.call({
|
|
@@ -63,150 +69,9 @@ export class ObjectDiscriminatedUnionType extends DiscriminatedUnionType {
|
|
|
63
69
|
})),
|
|
64
70
|
}
|
|
65
71
|
: {}),
|
|
66
|
-
...this
|
|
72
|
+
...ObjectDiscriminatedUnionType_toRdfResourceFunctionDeclaration.call(this),
|
|
67
73
|
};
|
|
68
74
|
}
|
|
69
|
-
get focusSparqlConstructTriplesFunctionDeclaration() {
|
|
70
|
-
if (!this.configuration.features.has("Object.SPARQL")) {
|
|
71
|
-
return {};
|
|
72
|
-
}
|
|
73
|
-
return singleEntryRecord(`focusSparqlConstructTriples`, code `\
|
|
74
|
-
export function focusSparqlConstructTriples({ filter, focusIdentifier, variablePrefix }: { filter: ${this.filterType} | undefined; focusIdentifier: ${this.reusables.imports.NamedNode} | ${this.reusables.imports.Variable}; ignoreRdfType: boolean; variablePrefix: string }): readonly ${this.reusables.imports.sparqljs}.Triple[] {
|
|
75
|
-
return [${joinCode(this.members.map((member) => code `...${member.type.name.unsafeCoerce()}.focusSparqlConstructTriples({ filter: filter?.on?.${member.type.name.unsafeCoerce()}, focusIdentifier, ignoreRdfType: false, variablePrefix: \`\${variablePrefix}${pascalCase(member.type.name.unsafeCoerce())}\` }).concat()`), { on: ", " })}];
|
|
76
|
-
}`);
|
|
77
|
-
}
|
|
78
|
-
get focusSparqlWherePatternsFunctionDeclaration() {
|
|
79
|
-
if (!this.configuration.features.has("Object.SPARQL")) {
|
|
80
|
-
return {};
|
|
81
|
-
}
|
|
82
|
-
return singleEntryRecord(`focusSparqlWherePatterns`, code `\
|
|
83
|
-
export function focusSparqlWherePatterns({ filter, focusIdentifier, preferredLanguages, variablePrefix }: { filter: ${this.filterType} | undefined; focusIdentifier: ${this.reusables.imports.NamedNode} | ${this.reusables.imports.Variable}; ignoreRdfType: boolean; preferredLanguages: readonly string[] | undefined; variablePrefix: string }): readonly ${this.reusables.snippets.SparqlPattern}[] {
|
|
84
|
-
${joinCode([
|
|
85
|
-
code `let patterns: ${this.reusables.snippets.SparqlPattern}[] = [];`,
|
|
86
|
-
code `\
|
|
87
|
-
if (focusIdentifier.termType === "Variable") {
|
|
88
|
-
patterns = patterns.concat(${this.identifierType.unsafeCoerce().valueSparqlWherePatternsFunction}({
|
|
89
|
-
filter: filter?.${this.configuration.syntheticNamePrefix}identifier,
|
|
90
|
-
ignoreRdfType: false,
|
|
91
|
-
preferredLanguages,
|
|
92
|
-
propertyPatterns: [],
|
|
93
|
-
schema: ${this.identifierType.unsafeCoerce().schema},
|
|
94
|
-
valueVariable: focusIdentifier,
|
|
95
|
-
variablePrefix,
|
|
96
|
-
}));
|
|
97
|
-
}`,
|
|
98
|
-
code `patterns.push({ patterns: [${joinCode(this.members.map((member) => code `${{
|
|
99
|
-
patterns: code `${member.type.name.unsafeCoerce()}.focusSparqlWherePatterns({ filter: filter?.on?.${member.type.name.unsafeCoerce()}, focusIdentifier, ignoreRdfType: false, preferredLanguages, variablePrefix: \`\${variablePrefix}${pascalCase(member.type.name.unsafeCoerce())}\` }).concat()`,
|
|
100
|
-
type: literalOf("group"),
|
|
101
|
-
}}`), { on: ", " })}], type: "union" });`,
|
|
102
|
-
code `return patterns;`,
|
|
103
|
-
])}
|
|
104
|
-
}`);
|
|
105
|
-
}
|
|
106
|
-
get fromRdfResourceFunctionDeclaration() {
|
|
107
|
-
if (!this.configuration.features.has("Object.fromRdf")) {
|
|
108
|
-
return {};
|
|
109
|
-
}
|
|
110
|
-
const name = this.name.unsafeCoerce();
|
|
111
|
-
return singleEntryRecord(`fromRdfResource`, code `\
|
|
112
|
-
export const fromRdfResource: ${this.reusables.snippets.FromRdfResourceFunction}<${name}> = (resource, options) =>
|
|
113
|
-
${this.members.reduce((expression, member) => {
|
|
114
|
-
const memberTypeExpression = code `(${member.type.name.unsafeCoerce()}.fromRdfResource(resource, { ...options, ignoreRdfType: false }) as ${this.reusables.imports.Either}<Error, ${name}>)`;
|
|
115
|
-
return expression !== null
|
|
116
|
-
? code `${expression}.altLazy(() => ${memberTypeExpression})`
|
|
117
|
-
: memberTypeExpression;
|
|
118
|
-
}, null)};`);
|
|
119
|
-
}
|
|
120
|
-
get graphqlTypeVariableStatement() {
|
|
121
|
-
if (!this.configuration.features.has("GraphQL")) {
|
|
122
|
-
return {};
|
|
123
|
-
}
|
|
124
|
-
if (this.synthetic) {
|
|
125
|
-
return {};
|
|
126
|
-
}
|
|
127
|
-
const name = this.name.unsafeCoerce();
|
|
128
|
-
return singleEntryRecord(`GraphQL`, code `\
|
|
129
|
-
export const GraphQL = new ${this.reusables.imports.GraphQLUnionType}(${{
|
|
130
|
-
description: this.comment.map(JSON.stringify).extract(),
|
|
131
|
-
name: name,
|
|
132
|
-
resolveType: code `(value: ${name}) => value.${this.configuration.syntheticNamePrefix}type`,
|
|
133
|
-
types: code `[${joinCode(this.members.map((member) => member.type.graphqlType.nullableExpression), { on: ", " })}]`,
|
|
134
|
-
}});`);
|
|
135
|
-
}
|
|
136
|
-
get identifierTypeDeclarations() {
|
|
137
|
-
if (!this.configuration.features.has("Object.type")) {
|
|
138
|
-
return {};
|
|
139
|
-
}
|
|
140
|
-
return singleEntryRecord(`Identifier`, code `\
|
|
141
|
-
export type Identifier = ${this.identifierType.unsafeCoerce().expression};
|
|
142
|
-
export namespace Identifier {
|
|
143
|
-
export const parse = ${this.identifierType.unsafeCoerce().parseFunction};
|
|
144
|
-
export const stringify = ${this.identifierType.unsafeCoerce().stringifyFunction};
|
|
145
|
-
}`);
|
|
146
|
-
}
|
|
147
|
-
get isTypeFunctionDeclaration() {
|
|
148
|
-
if (!this.configuration.features.has("Object.type")) {
|
|
149
|
-
return {};
|
|
150
|
-
}
|
|
151
|
-
const name = this.name.unsafeCoerce();
|
|
152
|
-
if (name === `${this.configuration.syntheticNamePrefix}Object`) {
|
|
153
|
-
return {};
|
|
154
|
-
}
|
|
155
|
-
return singleEntryRecord(`is${name}`, code `\
|
|
156
|
-
export function is${name}(object: ${this.configuration.syntheticNamePrefix}Object): object is ${name} {
|
|
157
|
-
return ${joinCode(this.members.map((member) => code `${member.type.name.unsafeCoerce()}.is${member.type.name.unsafeCoerce()}(object)`), { on: " || " })};
|
|
158
|
-
}`);
|
|
159
|
-
}
|
|
160
|
-
get schemaVariableStatement() {
|
|
161
|
-
if (!this.configuration.features.has("Object.schema")) {
|
|
162
|
-
return {};
|
|
163
|
-
}
|
|
164
|
-
const commonPropertiesByName = {};
|
|
165
|
-
this.members.forEach((member, memberI) => {
|
|
166
|
-
for (const memberTypeProperty of member.type.properties) {
|
|
167
|
-
if (memberTypeProperty.kind !== "Shacl") {
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
let commonProperty = commonPropertiesByName[memberTypeProperty.name];
|
|
171
|
-
if (commonProperty) {
|
|
172
|
-
if (PropertyPath.equals(commonProperty.property.path, memberTypeProperty.path)) {
|
|
173
|
-
commonProperty.memberTypesWithProperty[memberI] = true;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
commonPropertiesByName[memberTypeProperty.name] = commonProperty = {
|
|
178
|
-
memberTypesWithProperty: new Array(this.members.length).fill(false),
|
|
179
|
-
property: memberTypeProperty,
|
|
180
|
-
};
|
|
181
|
-
commonProperty.memberTypesWithProperty[memberI] = true;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
const propertiesObject = [];
|
|
186
|
-
for (const name of Object.keys(commonPropertiesByName).toSorted()) {
|
|
187
|
-
const { memberTypesWithProperty, property } = commonPropertiesByName[name];
|
|
188
|
-
if (!memberTypesWithProperty.every((value) => value)) {
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
property.schema.ifJust((propertySchema) => {
|
|
192
|
-
propertiesObject.push(code `${property.name}: ${propertySchema}`);
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
return singleEntryRecord(`schema`, code `\
|
|
196
|
-
export const schema = { ${joinCode(super.schemaInitializers.concat(code `properties: { ${joinCode(propertiesObject, { on: ", " })} }`), { on: ", " })} } as const;`);
|
|
197
|
-
}
|
|
198
|
-
get toRdfResourceFunctionDeclaration() {
|
|
199
|
-
if (!this.configuration.features.has("Object.toRdf")) {
|
|
200
|
-
return {};
|
|
201
|
-
}
|
|
202
|
-
const name = this.name.unsafeCoerce();
|
|
203
|
-
return singleEntryRecord(`toRdfResource`, code `\
|
|
204
|
-
export const toRdfResource: ${this.reusables.snippets.ToRdfResourceFunction}<${name}> = (object, options) => {
|
|
205
|
-
${joinCode(this.members
|
|
206
|
-
.map((member) => code `if (${member.type.name.unsafeCoerce()}.is${member.type.name.unsafeCoerce()}(object)) { return ${member.type.name.unsafeCoerce()}.toRdfResource(object, options); }`)
|
|
207
|
-
.concat(code `throw new Error("unrecognized type");`))}
|
|
208
|
-
};`);
|
|
209
|
-
}
|
|
210
75
|
}
|
|
211
76
|
__decorate([
|
|
212
77
|
Memoize()
|
|
@@ -20,6 +20,10 @@ export declare namespace TsGenerator {
|
|
|
20
20
|
interface Configuration {
|
|
21
21
|
readonly features: ReadonlySet<TsFeature>;
|
|
22
22
|
readonly finalized: true;
|
|
23
|
+
readonly objectDiscriminantProperty: {
|
|
24
|
+
readonly jsonName: string;
|
|
25
|
+
readonly name: string;
|
|
26
|
+
};
|
|
23
27
|
readonly syntheticNamePrefix: string;
|
|
24
28
|
}
|
|
25
29
|
namespace Configuration {
|
|
@@ -219,6 +219,10 @@ export class TsGenerator {
|
|
|
219
219
|
"JSON",
|
|
220
220
|
"RDF",
|
|
221
221
|
]),
|
|
222
|
+
objectDiscriminantProperty: {
|
|
223
|
+
jsonName: "$type",
|
|
224
|
+
name: "$type",
|
|
225
|
+
},
|
|
222
226
|
syntheticNamePrefix: "$",
|
|
223
227
|
};
|
|
224
228
|
const featureDependenciesStatic = {
|
|
@@ -282,6 +286,8 @@ export class TsGenerator {
|
|
|
282
286
|
return {
|
|
283
287
|
features: inferredFeatures,
|
|
284
288
|
finalized: true,
|
|
289
|
+
objectDiscriminantProperty: partialConfiguration?.objectDiscriminantProperty ??
|
|
290
|
+
Configuration.default_.objectDiscriminantProperty,
|
|
285
291
|
syntheticNamePrefix: partialConfiguration?.syntheticNamePrefix ??
|
|
286
292
|
Configuration.default_.syntheticNamePrefix,
|
|
287
293
|
};
|
|
@@ -53,7 +53,6 @@ export class TypeFactory {
|
|
|
53
53
|
const discriminantProperty = astType.name.map((name) => new ObjectType.DiscriminantProperty({
|
|
54
54
|
configuration: this.configuration,
|
|
55
55
|
logger: this.logger,
|
|
56
|
-
name: `${this.configuration.syntheticNamePrefix}type`,
|
|
57
56
|
objectType: { name: astType.name },
|
|
58
57
|
reusables: this.reusables,
|
|
59
58
|
value: name,
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { AbstractType } from "../AbstractType.js";
|
|
2
|
+
import type { Typeof } from "../Typeof.js";
|
|
3
|
+
export type DiscriminatedUnionType_Discriminant = ExtrinsicDiscriminant | HybridDiscriminant | IntrinsicDiscriminant | TypeofDiscriminant;
|
|
4
|
+
type ExtrinsicDiscriminant = {
|
|
5
|
+
readonly jsonName: string;
|
|
6
|
+
readonly kind: "Extrinsic";
|
|
7
|
+
readonly memberValues: readonly AbstractType.DiscriminantProperty.Value[];
|
|
8
|
+
readonly name: string;
|
|
9
|
+
};
|
|
10
|
+
type HybridDiscriminant = {
|
|
11
|
+
readonly jsonName: string;
|
|
12
|
+
readonly kind: "Hybrid";
|
|
13
|
+
readonly memberValues: readonly {
|
|
14
|
+
readonly kind: "Extrinsic" | "Intrinsic";
|
|
15
|
+
readonly values: readonly AbstractType.DiscriminantProperty.Value[];
|
|
16
|
+
}[];
|
|
17
|
+
readonly name: string;
|
|
18
|
+
};
|
|
19
|
+
type IntrinsicDiscriminant = {
|
|
20
|
+
readonly jsonName: string;
|
|
21
|
+
readonly kind: "Intrinsic";
|
|
22
|
+
readonly memberValues: readonly AbstractType.DiscriminantProperty.Value[];
|
|
23
|
+
readonly name: string;
|
|
24
|
+
};
|
|
25
|
+
type TypeofDiscriminant = {
|
|
26
|
+
readonly kind: "Typeof";
|
|
27
|
+
readonly memberValues: readonly Typeof[];
|
|
28
|
+
};
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=DiscriminatedUnionType_Discriminant.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_equalsFunctionExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_equalsFunctionExpression.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { code, joinCode } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_equalsFunctionExpression() {
|
|
3
|
+
return code `\
|
|
4
|
+
((left: ${this.expression}, right: ${this.expression}) => {
|
|
5
|
+
${joinCode(this.members.map(({ type, typeCheck, unwrap }) => code `if (${typeCheck(code `left`)} && ${typeCheck(code `right`)}) {
|
|
6
|
+
return ${type.equalsFunction}(${unwrap(code `left`)} as ${type.expression}, ${unwrap(code `right`)} as ${type.expression});
|
|
7
|
+
}`))}
|
|
8
|
+
|
|
9
|
+
return ${this.reusables.imports.Left}({ left, right, propertyName: "type", propertyValuesUnequal: { left: typeof left, right: typeof right, type: "boolean" as const }, type: "property" as const });
|
|
10
|
+
})`;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=DiscriminatedUnionType_equalsFunctionExpression.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_filterFunctionExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_filterFunctionExpression.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { code, joinCode, literalOf } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_filterFunctionExpression() {
|
|
3
|
+
const syntheticNamePrefix = this.configuration.syntheticNamePrefix;
|
|
4
|
+
return code `\
|
|
5
|
+
((filter: ${this.filterType}, value: ${this.expression}) => {
|
|
6
|
+
${joinCode([
|
|
7
|
+
...this.identifierType
|
|
8
|
+
.map((identifierType) => code `\
|
|
9
|
+
if (filter.${syntheticNamePrefix}identifier !== undefined && !${identifierType.filterFunction}(filter.${syntheticNamePrefix}identifier, value.${syntheticNamePrefix}identifier())) {
|
|
10
|
+
return false;
|
|
11
|
+
}`)
|
|
12
|
+
.toList(),
|
|
13
|
+
...this.members.map(({ primaryDiscriminantValue, type, typeCheck, unwrap }) => code `\
|
|
14
|
+
if (filter.on?.[${literalOf(primaryDiscriminantValue)}] !== undefined && ${typeCheck(code `value`)}) {
|
|
15
|
+
if (!${type.filterFunction}(filter.on[${literalOf(primaryDiscriminantValue)}], ${unwrap(code `value`)})) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}`),
|
|
19
|
+
])}
|
|
20
|
+
|
|
21
|
+
return true;
|
|
22
|
+
})`;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=DiscriminatedUnionType_filterFunctionExpression.js.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_filterTypeExpression.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_filterTypeExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_filterTypeExpression.d.ts.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_filterTypeExpression.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { code, joinCode, literalOf } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_filterTypeExpression() {
|
|
3
|
+
const syntheticNamePrefix = this.configuration.syntheticNamePrefix;
|
|
4
|
+
return code `\
|
|
5
|
+
{
|
|
6
|
+
${this.identifierType.map((identifierType) => code `readonly ${syntheticNamePrefix}identifier?: ${identifierType.filterType};`).orDefault(code ``)}
|
|
7
|
+
readonly on?: { ${joinCode(this.members.map(({ type, primaryDiscriminantValue }) => code `readonly ${literalOf(primaryDiscriminantValue)}?: ${type.filterType}`), { on: ";" })} }
|
|
8
|
+
}`;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=DiscriminatedUnionType_filterTypeExpression.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_fromJsonFunctionExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_fromJsonFunctionExpression.d.ts.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { code, joinCode } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_fromJsonFunctionExpression() {
|
|
3
|
+
return code `\
|
|
4
|
+
((value: ${this.jsonType().expression}): ${this.reusables.imports.Either}<Error, ${this.expression}> => {
|
|
5
|
+
${joinCode(this.members.map(({ jsonType, jsonTypeCheck, type, unwrap, wrap }) => code `if (${jsonTypeCheck(code `value`)}) { return ${type.fromJsonExpression({
|
|
6
|
+
variables: {
|
|
7
|
+
value: code `(${unwrap(code `value`)} as ${jsonType})`,
|
|
8
|
+
},
|
|
9
|
+
})}.map(value => (${wrap(code `value`)})); }`))}
|
|
10
|
+
|
|
11
|
+
throw new Error("unable to deserialize JSON");
|
|
12
|
+
})`;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=DiscriminatedUnionType_fromJsonFunctionExpression.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_fromRdfResourceValuesFunctionExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_fromRdfResourceValuesFunctionExpression.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { code, literalOf } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_fromRdfResourceValuesFunctionExpression() {
|
|
3
|
+
return code `\
|
|
4
|
+
(((values, options) =>
|
|
5
|
+
values.chainMap(value => {
|
|
6
|
+
const valueAsValues = value.toValues();
|
|
7
|
+
return ${this.members.reduce((expression, { type, primaryDiscriminantValue }, memberI) => {
|
|
8
|
+
let typeExpression = code `${type.fromRdfResourceValuesFunction}(valueAsValues, { ...options, schema: options.schema.members[${literalOf(primaryDiscriminantValue)}].type })`;
|
|
9
|
+
if (this.discriminant.kind === "Extrinsic" ||
|
|
10
|
+
(this.discriminant.kind === "Hybrid" &&
|
|
11
|
+
this.discriminant.memberValues[memberI].kind === "Extrinsic")) {
|
|
12
|
+
typeExpression = code `${typeExpression}.map(values => values.map(value => ({ ${this.discriminant.name}: ${literalOf(primaryDiscriminantValue)} as const, value }) as (${this.expression})))`;
|
|
13
|
+
}
|
|
14
|
+
typeExpression = code `(${typeExpression} as ${this.reusables.imports.Either}<Error, ${this.reusables.imports.Resource}.Values<${this.expression}>>)`;
|
|
15
|
+
return expression !== null
|
|
16
|
+
? code `${expression}.altLazy(() => ${typeExpression})`
|
|
17
|
+
: typeExpression;
|
|
18
|
+
}, null)}.chain(values => values.head());
|
|
19
|
+
})
|
|
20
|
+
) satisfies ${this.reusables.snippets.FromRdfResourceValuesFunction}<${this.expression}, ${this.schemaType}>)`;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=DiscriminatedUnionType_fromRdfResourceValuesFunctionExpression.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_hashFunctionExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_hashFunctionExpression.d.ts.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_hashFunctionExpression.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { code, joinCode } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_hashFunctionExpression() {
|
|
3
|
+
return code `\
|
|
4
|
+
(<HasherT extends ${this.reusables.snippets.Hasher}>(hasher: HasherT, value: ${this.expression}): HasherT => {
|
|
5
|
+
${joinCode(this.members.map(({ type, typeCheck, unwrap }) => code `if (${typeCheck(code `value`)}) { return ${type.hashFunction}(hasher, ${unwrap(code `value`)}); }`))}
|
|
6
|
+
return hasher;
|
|
7
|
+
})`;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=DiscriminatedUnionType_hashFunctionExpression.js.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_inferDiscriminant.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Maybe } from "purify-ts";
|
|
2
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
3
|
+
import type { Type } from "../Type.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_inferDiscriminant<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>, members: readonly {
|
|
5
|
+
readonly discriminantValue: Maybe<number | string>;
|
|
6
|
+
readonly type: Type;
|
|
7
|
+
}[]): DiscriminatedUnionType.Discriminant;
|
|
8
|
+
//# sourceMappingURL=DiscriminatedUnionType_inferDiscriminant.d.ts.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_inferDiscriminant.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { invariant } from "ts-invariant";
|
|
2
|
+
function termTypes(type) {
|
|
3
|
+
switch (type.kind) {
|
|
4
|
+
case "BlankNode":
|
|
5
|
+
case "Iri":
|
|
6
|
+
case "Identifier":
|
|
7
|
+
case "Literal":
|
|
8
|
+
case "Term":
|
|
9
|
+
return type.termTypes;
|
|
10
|
+
default:
|
|
11
|
+
return emptyTermTypesSet;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function DiscriminatedUnionType_inferDiscriminant(members) {
|
|
15
|
+
// extrinsic with user-specified values
|
|
16
|
+
if (members.some((member) => member.discriminantValue.isJust())) {
|
|
17
|
+
return {
|
|
18
|
+
jsonName: this.configuration.objectDiscriminantProperty.jsonName,
|
|
19
|
+
kind: "Extrinsic",
|
|
20
|
+
memberValues: members.map((member, memberI) => member.discriminantValue.orDefault(memberI)),
|
|
21
|
+
name: this.configuration.objectDiscriminantProperty.name,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const memberTypes = members.map((member) => member.type);
|
|
25
|
+
// intrinsic
|
|
26
|
+
{
|
|
27
|
+
let intrinsicDiscriminantProperty;
|
|
28
|
+
let memberValues = [];
|
|
29
|
+
for (const memberType of memberTypes) {
|
|
30
|
+
const memberTypeDiscriminantProperty = memberType.discriminantProperty.extract();
|
|
31
|
+
if (!memberTypeDiscriminantProperty) {
|
|
32
|
+
intrinsicDiscriminantProperty = undefined;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
if (!intrinsicDiscriminantProperty) {
|
|
36
|
+
intrinsicDiscriminantProperty = memberTypeDiscriminantProperty;
|
|
37
|
+
}
|
|
38
|
+
else if (memberTypeDiscriminantProperty.name !==
|
|
39
|
+
intrinsicDiscriminantProperty.name) {
|
|
40
|
+
intrinsicDiscriminantProperty = undefined;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
memberValues = memberValues.concat(memberTypeDiscriminantProperty.values);
|
|
44
|
+
}
|
|
45
|
+
if (intrinsicDiscriminantProperty) {
|
|
46
|
+
return {
|
|
47
|
+
jsonName: intrinsicDiscriminantProperty.jsonName,
|
|
48
|
+
kind: "Intrinsic",
|
|
49
|
+
memberValues,
|
|
50
|
+
name: intrinsicDiscriminantProperty.name,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// typeof
|
|
55
|
+
{
|
|
56
|
+
const memberTypeofs = [];
|
|
57
|
+
const memberTypeofsSet = new Set();
|
|
58
|
+
for (const memberType of memberTypes) {
|
|
59
|
+
for (const memberJsType of memberType.jsTypes) {
|
|
60
|
+
memberTypeofs.push(memberJsType.typeof);
|
|
61
|
+
memberTypeofsSet.add(memberJsType.typeof);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (memberTypeofsSet.size === memberTypes.length) {
|
|
65
|
+
return {
|
|
66
|
+
memberValues: memberTypeofs,
|
|
67
|
+
kind: "Typeof",
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// hybrid
|
|
72
|
+
// If some member type is an RDF/JS term then reuse "termType" as the discriminant.
|
|
73
|
+
if (memberTypes.some((memberType) => termTypes(memberType).size > 0)) {
|
|
74
|
+
const extrinsicMemberTypeAliasesSet = new Set();
|
|
75
|
+
let extrinsicMemberTypeCount = 0;
|
|
76
|
+
for (const memberType of memberTypes) {
|
|
77
|
+
if (termTypes(memberType).size > 0) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
extrinsicMemberTypeCount++;
|
|
81
|
+
if (memberType.name.isJust()) {
|
|
82
|
+
extrinsicMemberTypeAliasesSet.add(memberType.name.extract());
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
jsonName: "termType",
|
|
90
|
+
kind: "Hybrid",
|
|
91
|
+
memberValues: memberTypes.map((memberType, memberTypeI) => {
|
|
92
|
+
const memberTermTypes = termTypes(memberType);
|
|
93
|
+
if (memberTermTypes.size > 0) {
|
|
94
|
+
return {
|
|
95
|
+
kind: "Intrinsic",
|
|
96
|
+
values: [...memberTermTypes],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
kind: "Extrinsic",
|
|
101
|
+
values: extrinsicMemberTypeAliasesSet.size === extrinsicMemberTypeCount
|
|
102
|
+
? [memberType.name.unsafeCoerce()]
|
|
103
|
+
: [memberTypeI.toString()],
|
|
104
|
+
};
|
|
105
|
+
}),
|
|
106
|
+
name: "termType",
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// extrinsic with inferred values
|
|
110
|
+
{
|
|
111
|
+
let memberValues;
|
|
112
|
+
{
|
|
113
|
+
const memberTypeNames = memberTypes.map((memberType) => memberType.name.orDefault(memberType.jsTypes[0].typeof));
|
|
114
|
+
const memberTypeNamesSet = new Set(memberTypeNames);
|
|
115
|
+
if (memberTypeNamesSet.size === memberTypeNames.length) {
|
|
116
|
+
memberValues = memberTypeNames;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
// Otherwise prefix the non-unique strings with an index and use those as the discriminant values.
|
|
120
|
+
memberValues = memberTypeNames.map((memberTypeName, memberTypeI) => `${memberTypeI}-${memberTypeName}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
invariant(memberValues.length === memberTypes.length);
|
|
124
|
+
return {
|
|
125
|
+
jsonName: this.configuration.objectDiscriminantProperty.jsonName,
|
|
126
|
+
kind: "Extrinsic",
|
|
127
|
+
memberValues,
|
|
128
|
+
name: this.configuration.objectDiscriminantProperty.name,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const emptyTermTypesSet = new Set();
|
|
133
|
+
//# sourceMappingURL=DiscriminatedUnionType_inferDiscriminant.js.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_inlineExpression.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_inlineExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_inlineExpression.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { code, joinCode, literalOf } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_inlineExpression() {
|
|
3
|
+
const discriminant = this.discriminant; // To get type narrowing to work
|
|
4
|
+
switch (discriminant.kind) {
|
|
5
|
+
case "Extrinsic":
|
|
6
|
+
return code `(${joinCode(this.members.map(({ type, primaryDiscriminantValue }) => code `{ ${discriminant.name}: ${literalOf(primaryDiscriminantValue)}, value: ${type.expression} }`), { on: "|" })})`;
|
|
7
|
+
case "Hybrid":
|
|
8
|
+
return code `(${joinCode(this.members.map(({ primaryDiscriminantValue, type }, memberI) => {
|
|
9
|
+
switch (discriminant.memberValues[memberI].kind) {
|
|
10
|
+
case "Extrinsic":
|
|
11
|
+
return code `{ ${discriminant.name}: ${literalOf(primaryDiscriminantValue)}, value: ${type.expression} }`;
|
|
12
|
+
case "Intrinsic":
|
|
13
|
+
return code `${type.expression}`;
|
|
14
|
+
default:
|
|
15
|
+
throw new Error();
|
|
16
|
+
}
|
|
17
|
+
}), { on: "|" })})`;
|
|
18
|
+
case "Intrinsic":
|
|
19
|
+
// If every type shares a discriminant (e.g., RDF/JS "termType" or generated ObjectType "type"),
|
|
20
|
+
// just join their names with "|"
|
|
21
|
+
return code `(${joinCode(this.members.map(({ type }) => code `${type.expression}`), { on: "|" })})`;
|
|
22
|
+
case "Typeof":
|
|
23
|
+
// The type.name may include literal values, but they should still be unambiguous with other member types since the typeofs
|
|
24
|
+
// of the different member types are known to be different.
|
|
25
|
+
return code `(${joinCode(this.members.map(({ type }) => code `${type.expression}`), { on: "|" })})`;
|
|
26
|
+
default:
|
|
27
|
+
discriminant;
|
|
28
|
+
throw new Error("should never reach this point");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=DiscriminatedUnionType_inlineExpression.js.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_jsonSchemaExpression.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscriminatedUnionType } from "../DiscriminatedUnionType.js";
|
|
2
|
+
import type { Type } from "../Type.js";
|
|
3
|
+
import { type Code } from "../ts-poet-wrapper.js";
|
|
4
|
+
export declare function DiscriminatedUnionType_jsonSchemaExpression<MemberTypeT extends Type>(this: DiscriminatedUnionType<MemberTypeT>): Code;
|
|
5
|
+
//# sourceMappingURL=DiscriminatedUnionType_jsonSchemaExpression.d.ts.map
|
package/dist/generators/ts/_DiscriminatedUnionType/DiscriminatedUnionType_jsonSchemaExpression.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { code, joinCode, literalOf } from "../ts-poet-wrapper.js";
|
|
2
|
+
export function DiscriminatedUnionType_jsonSchemaExpression() {
|
|
3
|
+
const discriminant = this.discriminant; // To get type narrowing to work
|
|
4
|
+
switch (discriminant.kind) {
|
|
5
|
+
case "Extrinsic":
|
|
6
|
+
return code `${this.reusables.imports.z}.discriminatedUnion("${discriminant.name}", [${joinCode(this.members.map(({ type, primaryDiscriminantValue }) => code `${this.reusables.imports.z}.object({ ${discriminant.name}: ${this.reusables.imports.z}.literal(${literalOf(primaryDiscriminantValue)}), value: ${type.jsonSchema({ context: "type" })} })`), { on: "," })}]).readonly()`;
|
|
7
|
+
case "Hybrid":
|
|
8
|
+
return code `${this.reusables.imports.z}.discriminatedUnion("${discriminant.name}", [${joinCode(this.members.map(({ primaryDiscriminantValue, type }, memberI) => {
|
|
9
|
+
switch (discriminant.memberValues[memberI].kind) {
|
|
10
|
+
case "Extrinsic":
|
|
11
|
+
return code `${this.reusables.imports.z}.object({ ${discriminant.name}: ${this.reusables.imports.z}.literal(${literalOf(primaryDiscriminantValue)}), value: ${type.jsonSchema({ context: "type" })} })`;
|
|
12
|
+
case "Intrinsic":
|
|
13
|
+
return type.jsonSchema({
|
|
14
|
+
includeDiscriminantProperty: true,
|
|
15
|
+
context: "type",
|
|
16
|
+
});
|
|
17
|
+
default:
|
|
18
|
+
throw new Error();
|
|
19
|
+
}
|
|
20
|
+
}), { on: "," })}]).readonly()`;
|
|
21
|
+
case "Intrinsic":
|
|
22
|
+
return code `${this.reusables.imports.z}.discriminatedUnion("${discriminant.name}", [${joinCode(this.members.map(({ type }) => type.jsonSchema({
|
|
23
|
+
includeDiscriminantProperty: true,
|
|
24
|
+
context: "type",
|
|
25
|
+
})), { on: "," })}]).readonly()`;
|
|
26
|
+
case "Typeof":
|
|
27
|
+
return code `${this.reusables.imports.z}.union([${joinCode(this.members.map(({ type }) => type.jsonSchema({ context: "type" })), { on: "," })}]).readonly()`;
|
|
28
|
+
default:
|
|
29
|
+
throw discriminant;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=DiscriminatedUnionType_jsonSchemaExpression.js.map
|