@shaclmate/compiler 2.0.13 → 2.0.14

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.
Files changed (204) hide show
  1. package/Compiler.d.ts +15 -0
  2. package/Compiler.js +20 -0
  3. package/ShapesGraphToAstTransformer.d.ts +18 -23
  4. package/ShapesGraphToAstTransformer.js +21 -447
  5. package/_ShapesGraphToAstTransformer/NodeShapeAstType.d.ts +3 -0
  6. package/_ShapesGraphToAstTransformer/NodeShapeAstType.js +2 -0
  7. package/_ShapesGraphToAstTransformer/index.d.ts +10 -0
  8. package/_ShapesGraphToAstTransformer/index.js +10 -0
  9. package/_ShapesGraphToAstTransformer/pickLiteral.d.ts +4 -0
  10. package/_ShapesGraphToAstTransformer/pickLiteral.js +18 -0
  11. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.d.ts +4 -0
  12. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.js +26 -0
  13. package/_ShapesGraphToAstTransformer/shapeAstName.d.ts +5 -0
  14. package/_ShapesGraphToAstTransformer/shapeAstName.js +28 -0
  15. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.d.ts +6 -0
  16. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +165 -0
  17. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +13 -0
  18. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +198 -0
  19. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +12 -0
  20. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +29 -0
  21. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +12 -0
  22. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +43 -0
  23. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.d.ts +6 -0
  24. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +31 -0
  25. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +14 -0
  26. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +21 -0
  27. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +16 -0
  28. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +58 -0
  29. package/ast/Ast.d.ts +5 -0
  30. package/ast/CompositeType.d.ts +13 -0
  31. package/ast/CompositeType.js +2 -0
  32. package/ast/IdentifierType.d.ts +3 -6
  33. package/ast/IntersectionType.d.ts +2 -2
  34. package/ast/ListType.d.ts +53 -0
  35. package/ast/ListType.js +2 -0
  36. package/ast/LiteralType.d.ts +4 -3
  37. package/ast/Name.d.ts +1 -0
  38. package/ast/ObjectCompositeType.d.ts +22 -0
  39. package/ast/ObjectCompositeType.js +2 -0
  40. package/ast/ObjectIntersectionType.d.ts +8 -0
  41. package/ast/ObjectIntersectionType.js +2 -0
  42. package/ast/ObjectType.d.ts +91 -19
  43. package/ast/ObjectType.js +26 -1
  44. package/ast/ObjectUnionType.d.ts +19 -0
  45. package/ast/ObjectUnionType.js +2 -0
  46. package/ast/PlaceholderType.d.ts +7 -0
  47. package/ast/PlaceholderType.js +2 -0
  48. package/ast/SetType.d.ts +1 -1
  49. package/ast/TermType.d.ts +11 -0
  50. package/ast/TermType.js +2 -0
  51. package/ast/Type.d.ts +11 -3
  52. package/ast/UnionType.d.ts +3 -3
  53. package/ast/index.d.ts +6 -3
  54. package/ast/index.js +6 -3
  55. package/enums/MintingStrategy.d.ts +5 -0
  56. package/enums/MintingStrategy.js +2 -0
  57. package/enums/PropertyVisibility.d.ts +2 -0
  58. package/enums/PropertyVisibility.js +2 -0
  59. package/enums/TsFeature.d.ts +5 -0
  60. package/enums/TsFeature.js +16 -0
  61. package/enums/TsObjectDeclarationType.d.ts +5 -0
  62. package/enums/TsObjectDeclarationType.js +2 -0
  63. package/enums/index.d.ts +5 -0
  64. package/enums/index.js +5 -0
  65. package/generators/Generator.d.ts +5 -0
  66. package/generators/Generator.js +2 -0
  67. package/generators/index.d.ts +2 -2
  68. package/generators/index.js +2 -2
  69. package/generators/json/AstJsonGenerator.d.ts +4 -5
  70. package/generators/json/AstJsonGenerator.js +31 -22
  71. package/generators/ts/BooleanType.d.ts +7 -3
  72. package/generators/ts/BooleanType.js +58 -4
  73. package/generators/ts/DateTimeType.d.ts +17 -0
  74. package/generators/ts/DateTimeType.js +58 -0
  75. package/generators/ts/DeclaredType.d.ts +17 -0
  76. package/generators/ts/DeclaredType.js +10 -0
  77. package/generators/ts/IdentifierType.d.ts +7 -17
  78. package/generators/ts/IdentifierType.js +69 -60
  79. package/generators/ts/Import.d.ts +18 -0
  80. package/generators/ts/Import.js +59 -0
  81. package/generators/ts/ListType.d.ts +18 -8
  82. package/generators/ts/ListType.js +197 -43
  83. package/generators/ts/LiteralType.d.ts +13 -11
  84. package/generators/ts/LiteralType.js +36 -51
  85. package/generators/ts/NumberType.d.ts +7 -3
  86. package/generators/ts/NumberType.js +55 -4
  87. package/generators/ts/ObjectType.d.ts +47 -25
  88. package/generators/ts/ObjectType.js +222 -49
  89. package/generators/ts/ObjectUnionType.d.ts +58 -0
  90. package/generators/ts/ObjectUnionType.js +481 -0
  91. package/generators/ts/OptionType.d.ts +12 -4
  92. package/generators/ts/OptionType.js +65 -16
  93. package/generators/ts/PrimitiveType.d.ts +15 -5
  94. package/generators/ts/PrimitiveType.js +20 -29
  95. package/generators/ts/SetType.d.ts +12 -4
  96. package/generators/ts/SetType.js +103 -29
  97. package/generators/ts/StringType.d.ts +8 -3
  98. package/generators/ts/StringType.js +57 -3
  99. package/generators/ts/TermType.d.ts +52 -0
  100. package/generators/ts/TermType.js +234 -0
  101. package/generators/ts/TsGenerator.d.ts +4 -12
  102. package/generators/ts/TsGenerator.js +44 -100
  103. package/generators/ts/Type.d.ts +107 -46
  104. package/generators/ts/Type.js +78 -61
  105. package/generators/ts/TypeFactory.d.ts +4 -5
  106. package/generators/ts/TypeFactory.js +212 -89
  107. package/generators/ts/UnionType.d.ts +19 -6
  108. package/generators/ts/UnionType.js +230 -120
  109. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +27 -7
  110. package/generators/ts/_ObjectType/IdentifierProperty.js +143 -31
  111. package/generators/ts/_ObjectType/Property.d.ts +125 -9
  112. package/generators/ts/_ObjectType/Property.js +25 -3
  113. package/generators/ts/_ObjectType/ShaclProperty.d.ts +23 -5
  114. package/generators/ts/_ObjectType/ShaclProperty.js +100 -26
  115. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +24 -6
  116. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +75 -13
  117. package/generators/ts/_ObjectType/classDeclaration.d.ts +2 -1
  118. package/generators/ts/_ObjectType/classDeclaration.js +79 -111
  119. package/generators/ts/_ObjectType/createFunctionDeclaration.d.ts +5 -0
  120. package/generators/ts/_ObjectType/createFunctionDeclaration.js +60 -0
  121. package/generators/ts/_ObjectType/equalsFunctionDeclaration.d.ts +2 -1
  122. package/generators/ts/_ObjectType/equalsFunctionDeclaration.js +12 -20
  123. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.d.ts +11 -0
  124. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +70 -0
  125. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +4 -0
  126. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +78 -0
  127. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +4 -0
  128. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +91 -0
  129. package/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +3 -3
  130. package/generators/ts/_ObjectType/hashFunctionDeclaration.js +11 -55
  131. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.d.ts +11 -0
  132. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.js +64 -0
  133. package/generators/ts/_ObjectType/index.d.ts +8 -2
  134. package/generators/ts/_ObjectType/index.js +8 -2
  135. package/generators/ts/_ObjectType/interfaceDeclaration.d.ts +3 -2
  136. package/generators/ts/_ObjectType/interfaceDeclaration.js +11 -2
  137. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +5 -0
  138. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +19 -0
  139. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +5 -0
  140. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +31 -0
  141. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +5 -0
  142. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +37 -0
  143. package/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +6 -0
  144. package/generators/ts/{rdfjsTermExpression.js → _ObjectType/rdfjsTermExpression.js} +3 -1
  145. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.d.ts +4 -0
  146. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +101 -0
  147. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +5 -0
  148. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +19 -0
  149. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.d.ts +10 -0
  150. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +59 -0
  151. package/generators/ts/_ObjectType/toJsonReturnType.d.ts +3 -0
  152. package/generators/ts/_ObjectType/toJsonReturnType.js +19 -0
  153. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +3 -2
  154. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +11 -45
  155. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.d.ts +10 -0
  156. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +67 -0
  157. package/generators/ts/objectInitializer.d.ts +2 -0
  158. package/generators/ts/objectInitializer.js +14 -0
  159. package/generators/ts/tsComment.d.ts +5 -0
  160. package/generators/ts/tsComment.js +11 -0
  161. package/generators/ts/tsName.d.ts +1 -1
  162. package/generators/ts/tsName.js +1 -0
  163. package/index.d.ts +1 -1
  164. package/index.js +1 -1
  165. package/input/NodeShape.d.ts +46 -0
  166. package/input/NodeShape.js +165 -0
  167. package/input/Ontology.d.ts +13 -0
  168. package/input/Ontology.js +30 -0
  169. package/input/PropertyShape.d.ts +15 -0
  170. package/input/PropertyShape.js +34 -0
  171. package/input/Shape.d.ts +4 -0
  172. package/input/Shape.js +2 -0
  173. package/input/ShapesGraph.d.ts +9 -0
  174. package/input/ShapesGraph.js +50 -0
  175. package/input/ancestorClassIris.d.ts +4 -0
  176. package/input/ancestorClassIris.js +21 -0
  177. package/input/descendantClassIris.d.ts +4 -0
  178. package/input/descendantClassIris.js +21 -0
  179. package/input/generated.d.ts +274 -0
  180. package/input/generated.js +1420 -0
  181. package/input/index.d.ts +7 -0
  182. package/input/index.js +7 -0
  183. package/input/tsFeatures.d.ts +10 -0
  184. package/input/tsFeatures.js +42 -0
  185. package/package.json +14 -8
  186. package/ast/MintingStrategy.d.ts +0 -8
  187. package/ast/MintingStrategy.js +0 -9
  188. package/generators/ts/Configuration.d.ts +0 -29
  189. package/generators/ts/Configuration.js +0 -40
  190. package/generators/ts/IntersectionType.d.ts +0 -14
  191. package/generators/ts/IntersectionType.js +0 -26
  192. package/generators/ts/RdfjsTermType.d.ts +0 -25
  193. package/generators/ts/RdfjsTermType.js +0 -46
  194. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.d.ts +0 -4
  195. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.js +0 -72
  196. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.d.ts +0 -4
  197. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.js +0 -46
  198. package/generators/ts/rdfjsTermExpression.d.ts +0 -5
  199. package/vocabularies/dashDataset.d.ts +0 -3
  200. package/vocabularies/dashDataset.js +0 -2370
  201. package/vocabularies/index.d.ts +0 -2
  202. package/vocabularies/index.js +0 -2
  203. package/vocabularies/shaclmate.d.ts +0 -15
  204. package/vocabularies/shaclmate.js +0 -6
@@ -0,0 +1,481 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { camelCase, pascalCase } from "change-case";
8
+ import { Maybe } from "purify-ts";
9
+ import { invariant } from "ts-invariant";
10
+ import { StructureKind, } from "ts-morph";
11
+ import { Memoize } from "typescript-memoize";
12
+ import { DeclaredType } from "./DeclaredType.js";
13
+ import { hasherTypeConstraint } from "./_ObjectType/hashFunctionOrMethodDeclaration.js";
14
+ import { objectInitializer } from "./objectInitializer.js";
15
+ import { tsComment } from "./tsComment.js";
16
+ /**
17
+ * A union of object types, generated as a type alias
18
+ *
19
+ * type SomeUnion = Member1 | Member2 | ...
20
+ *
21
+ * with associated functions that switch on the type discriminator property and delegate to the appropriate
22
+ * member type code.
23
+ *
24
+ * It also generates SPARQL graph patterns that UNION the member object types.
25
+ */
26
+ export class ObjectUnionType extends DeclaredType {
27
+ constructor({ comment, label, memberTypes, ...superParameters }) {
28
+ super(superParameters);
29
+ this.kind = "ObjectUnionType";
30
+ this.comment = comment;
31
+ this.label = label;
32
+ invariant(memberTypes.length > 0);
33
+ this.memberTypes = memberTypes;
34
+ const discriminatorPropertyName = memberTypes[0].discriminatorProperty.unsafeCoerce().name;
35
+ const discriminatorPropertyValues = [];
36
+ for (const memberType of this.memberTypes) {
37
+ invariant(memberType.declarationType === memberTypes[0].declarationType);
38
+ invariant(memberType._discriminatorProperty.name === discriminatorPropertyName);
39
+ discriminatorPropertyValues.push(...memberType._discriminatorProperty.values);
40
+ }
41
+ this._discriminatorProperty = {
42
+ name: discriminatorPropertyName,
43
+ values: discriminatorPropertyValues,
44
+ };
45
+ }
46
+ get conversions() {
47
+ return [
48
+ {
49
+ conversionExpression: (value) => value,
50
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
51
+ sourceTypeName: this.name,
52
+ },
53
+ ];
54
+ }
55
+ get declarationImports() {
56
+ return this.memberTypes.flatMap((memberType) => memberType.useImports);
57
+ }
58
+ get declarations() {
59
+ const declarations = [this.typeAliasDeclaration];
60
+ const moduleStatements = [
61
+ ...this.equalsFunctionDeclaration.toList(),
62
+ ...this.fromJsonFunctionDeclaration.toList(),
63
+ ...this.fromRdfFunctionDeclaration.toList(),
64
+ ...this.hashFunctionDeclaration.toList(),
65
+ ...this.jsonZodSchemaFunctionDeclaration.toList(),
66
+ ...this.sparqlFunctionDeclarations,
67
+ ...this.toJsonFunctionDeclaration.toList(),
68
+ ...this.toRdfFunctionDeclaration.toList(),
69
+ ];
70
+ if (moduleStatements.length > 0) {
71
+ declarations.push({
72
+ isExported: this.export,
73
+ kind: StructureKind.Module,
74
+ name: this.name,
75
+ statements: moduleStatements,
76
+ });
77
+ }
78
+ return declarations;
79
+ }
80
+ get discriminatorProperty() {
81
+ return Maybe.of(this._discriminatorProperty);
82
+ }
83
+ get equalsFunction() {
84
+ return `${this.name}.equals`;
85
+ }
86
+ get jsonName() {
87
+ return this.memberTypes
88
+ .map((memberType) => memberType.jsonName)
89
+ .join(" | ");
90
+ }
91
+ get mutable() {
92
+ return this.memberTypes.some((memberType) => memberType.mutable);
93
+ }
94
+ get useImports() {
95
+ return [];
96
+ }
97
+ get thisVariable() {
98
+ return `_${camelCase(this.name)}`;
99
+ }
100
+ get equalsFunctionDeclaration() {
101
+ if (!this.features.has("equals")) {
102
+ return Maybe.empty();
103
+ }
104
+ const caseBlocks = this.memberTypes.map((memberType) => {
105
+ let returnExpression;
106
+ switch (memberType.declarationType) {
107
+ case "class":
108
+ returnExpression = `left.equals(right as unknown as ${memberType.name})`;
109
+ break;
110
+ case "interface":
111
+ returnExpression = `${memberType.name}.equals(left, right as unknown as ${memberType.name})`;
112
+ break;
113
+ }
114
+ return `case "${memberType.name}": return ${returnExpression};`;
115
+ });
116
+ return Maybe.of({
117
+ isExported: true,
118
+ kind: StructureKind.Function,
119
+ name: "equals",
120
+ parameters: [
121
+ {
122
+ name: "left",
123
+ type: this.name,
124
+ },
125
+ {
126
+ name: "right",
127
+ type: this.name,
128
+ },
129
+ ],
130
+ returnType: "purifyHelpers.Equatable.EqualsResult",
131
+ statements: `\
132
+ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
133
+ switch (left.${this._discriminatorProperty.name}) {
134
+ ${caseBlocks.join(" ")}
135
+ }
136
+ })`,
137
+ });
138
+ }
139
+ get fromJsonFunctionDeclaration() {
140
+ if (!this.features.has("fromJson")) {
141
+ return Maybe.empty();
142
+ }
143
+ return Maybe.of({
144
+ isExported: true,
145
+ kind: StructureKind.Function,
146
+ name: "fromJson",
147
+ parameters: [
148
+ {
149
+ name: "json",
150
+ type: "unknown",
151
+ },
152
+ ],
153
+ returnType: `purify.Either<zod.ZodError, ${this.name}>`,
154
+ statements: [
155
+ `return ${this.memberTypes.reduce((expression, memberType) => {
156
+ const memberTypeExpression = `(${memberType.name}.fromJson(json) as purify.Either<zod.ZodError, ${this.name}>)`;
157
+ return expression.length > 0
158
+ ? `${expression}.altLazy(() => ${memberTypeExpression})`
159
+ : memberTypeExpression;
160
+ }, "")};`,
161
+ ],
162
+ });
163
+ }
164
+ get fromRdfFunctionDeclaration() {
165
+ if (!this.features.has("fromRdf")) {
166
+ return Maybe.empty();
167
+ }
168
+ return Maybe.of({
169
+ isExported: true,
170
+ kind: StructureKind.Function,
171
+ name: "fromRdf",
172
+ parameters: [
173
+ {
174
+ name: "parameters",
175
+ type: `{ [_index: string]: any; ignoreRdfType?: boolean; resource: ${this.rdfjsResourceType().name}; }`,
176
+ },
177
+ ],
178
+ returnType: `purify.Either<rdfjsResource.Resource.ValueError, ${this.name}>`,
179
+ statements: [
180
+ `return ${this.memberTypes.reduce((expression, memberType) => {
181
+ const memberTypeExpression = `(${memberType.name}.fromRdf(parameters) as purify.Either<rdfjsResource.Resource.ValueError, ${this.name}>)`;
182
+ return expression.length > 0
183
+ ? `${expression}.altLazy(() => ${memberTypeExpression})`
184
+ : memberTypeExpression;
185
+ }, "")};`,
186
+ ],
187
+ });
188
+ }
189
+ get hashFunctionDeclaration() {
190
+ if (!this.features.has("hash")) {
191
+ return Maybe.empty();
192
+ }
193
+ const hasherVariable = "_hasher";
194
+ const caseBlocks = this.memberTypes.map((memberType) => {
195
+ let returnExpression;
196
+ switch (memberType.declarationType) {
197
+ case "class":
198
+ returnExpression = `${this.thisVariable}.hash(${hasherVariable})`;
199
+ break;
200
+ case "interface":
201
+ returnExpression = `${memberType.name}.${memberType.hashFunctionName}(${this.thisVariable}, ${hasherVariable})`;
202
+ break;
203
+ }
204
+ return `case "${memberType.name}": return ${returnExpression};`;
205
+ });
206
+ return Maybe.of({
207
+ isExported: true,
208
+ kind: StructureKind.Function,
209
+ name: "hash",
210
+ parameters: [
211
+ {
212
+ name: this.thisVariable,
213
+ type: this.name,
214
+ },
215
+ {
216
+ name: hasherVariable,
217
+ type: "HasherT",
218
+ },
219
+ ],
220
+ returnType: "HasherT",
221
+ statements: `switch (${this.thisVariable}.${this._discriminatorProperty.name}) { ${caseBlocks.join(" ")} }`,
222
+ typeParameters: [
223
+ {
224
+ name: "HasherT",
225
+ constraint: hasherTypeConstraint,
226
+ },
227
+ ],
228
+ });
229
+ }
230
+ get jsonZodSchemaFunctionDeclaration() {
231
+ if (!this.features.has("fromJson")) {
232
+ return Maybe.empty();
233
+ }
234
+ const variables = { zod: "zod" };
235
+ return Maybe.of({
236
+ isExported: true,
237
+ kind: StructureKind.Function,
238
+ name: "jsonZodSchema",
239
+ statements: `return ${variables.zod}.discriminatedUnion("${this._discriminatorProperty.name}", [${this.memberTypes.map((memberType) => memberType.jsonZodSchema({ variables })).join(", ")}]);`,
240
+ });
241
+ }
242
+ get sparqlFunctionDeclarations() {
243
+ if (!this.features.has("sparql")) {
244
+ return [];
245
+ }
246
+ return [
247
+ {
248
+ isExported: true,
249
+ kind: StructureKind.Function,
250
+ name: "sparqlConstructQuery",
251
+ parameters: [
252
+ {
253
+ hasQuestionToken: true,
254
+ name: "parameters",
255
+ type: '{ prefixes?: { [prefix: string]: string }; subject: rdfjs.Variable } & Omit<sparqljs.ConstructQuery, "prefixes" | "queryType" | "template" | "where">',
256
+ },
257
+ ],
258
+ returnType: "sparqljs.ConstructQuery",
259
+ statements: [
260
+ `const subject = parameters?.subject ?? ${this.dataFactoryVariable}.variable!("${camelCase(this.name)}");`,
261
+ `return { ...parameters, prefixes: parameters?.prefixes ?? {}, queryType: "CONSTRUCT", template: ${this.name}.sparqlConstructTemplateTriples({ subject }).concat(), type: "query", where: ${this.name}.sparqlWherePatterns({ subject }).concat() };`,
262
+ ],
263
+ },
264
+ {
265
+ isExported: true,
266
+ kind: StructureKind.Function,
267
+ name: "sparqlConstructQueryString",
268
+ parameters: [
269
+ {
270
+ hasQuestionToken: true,
271
+ name: "parameters",
272
+ type: '{ subject: rdfjs.Variable } & Omit<sparqljs.ConstructQuery, "prefixes" | "queryType" | "template" | "where"> & sparqljs.GeneratorOptions',
273
+ },
274
+ ],
275
+ returnType: "string",
276
+ statements: [
277
+ `return new sparqljs.Generator(parameters).stringify(${this.name}.sparqlConstructQuery(parameters));`,
278
+ ],
279
+ },
280
+ {
281
+ isExported: true,
282
+ kind: StructureKind.Function,
283
+ name: "sparqlConstructTemplateTriples",
284
+ parameters: [
285
+ {
286
+ name: "{ subject, variablePrefix: variablePrefixParameter }",
287
+ type: "{ subject: rdfjs.Variable, variablePrefix?: string }",
288
+ },
289
+ ],
290
+ returnType: "readonly sparqljs.Triple[]",
291
+ statements: [
292
+ "const variablePrefix = variablePrefixParameter ?? subject.value;",
293
+ `return [${this.memberTypes
294
+ .map((memberType) => `...${memberType.name}.sparqlConstructTemplateTriples({ subject, variablePrefix: \`\${variablePrefix}${pascalCase(memberType.name)}\` }).concat()`)
295
+ .join(", ")}];`,
296
+ ],
297
+ },
298
+ {
299
+ isExported: true,
300
+ kind: StructureKind.Function,
301
+ name: "sparqlWherePatterns",
302
+ parameters: [
303
+ {
304
+ name: "{ subject, variablePrefix: variablePrefixParameter }",
305
+ type: "{ subject: rdfjs.Variable, variablePrefix?: string }",
306
+ },
307
+ ],
308
+ returnType: "readonly sparqljs.Pattern[]",
309
+ statements: [
310
+ "const variablePrefix = variablePrefixParameter ?? subject.value;",
311
+ `return [{ patterns: [${this.memberTypes
312
+ .map((memberType) => objectInitializer({
313
+ patterns: `${memberType.name}.sparqlWherePatterns({ subject, variablePrefix: \`\${variablePrefix}${pascalCase(memberType.name)}\` }).concat()`,
314
+ type: '"group"',
315
+ }))
316
+ .join(", ")}], type: "union" }];`,
317
+ ],
318
+ },
319
+ ];
320
+ }
321
+ get toJsonFunctionDeclaration() {
322
+ if (!this.features.has("toJson")) {
323
+ return Maybe.empty();
324
+ }
325
+ const caseBlocks = this.memberTypes.map((memberType) => {
326
+ let returnExpression;
327
+ switch (memberType.declarationType) {
328
+ case "class":
329
+ returnExpression = `${this.thisVariable}.toJson()`;
330
+ break;
331
+ case "interface":
332
+ returnExpression = `${memberType.name}.toJson(${this.thisVariable})`;
333
+ break;
334
+ }
335
+ return `case "${memberType.name}": return ${returnExpression};`;
336
+ });
337
+ return Maybe.of({
338
+ isExported: true,
339
+ kind: StructureKind.Function,
340
+ name: "toJson",
341
+ parameters: [
342
+ {
343
+ name: this.thisVariable,
344
+ type: this.name,
345
+ },
346
+ ],
347
+ returnType: this.jsonName,
348
+ statements: `switch (${this.thisVariable}.${this._discriminatorProperty.name}) { ${caseBlocks.join(" ")} }`,
349
+ });
350
+ }
351
+ get toRdfFunctionDeclaration() {
352
+ if (!this.features.has("toRdf")) {
353
+ return Maybe.empty();
354
+ }
355
+ const parametersVariable = "_parameters";
356
+ const caseBlocks = this.memberTypes.map((memberType) => {
357
+ let returnExpression;
358
+ switch (memberType.declarationType) {
359
+ case "class":
360
+ returnExpression = `${this.thisVariable}.toRdf(${parametersVariable})`;
361
+ break;
362
+ case "interface":
363
+ returnExpression = `${memberType.name}.toRdf(${this.thisVariable}, ${parametersVariable})`;
364
+ break;
365
+ }
366
+ return `case "${memberType.name}": return ${returnExpression};`;
367
+ });
368
+ return Maybe.of({
369
+ isExported: true,
370
+ kind: StructureKind.Function,
371
+ name: "toRdf",
372
+ parameters: [
373
+ {
374
+ name: this.thisVariable,
375
+ type: this.name,
376
+ },
377
+ {
378
+ name: parametersVariable,
379
+ type: "{ mutateGraph: rdfjsResource.MutableResource.MutateGraph, resourceSet: rdfjsResource.MutableResourceSet }",
380
+ },
381
+ ],
382
+ returnType: this.rdfjsResourceType({ mutable: true }).name,
383
+ statements: `switch (${this.thisVariable}.${this._discriminatorProperty.name}) { ${caseBlocks.join(" ")} }`,
384
+ });
385
+ }
386
+ get typeAliasDeclaration() {
387
+ return {
388
+ isExported: true,
389
+ leadingTrivia: this.comment.alt(this.label).map(tsComment).extract(),
390
+ kind: StructureKind.TypeAlias,
391
+ name: this.name,
392
+ type: this.memberTypes.map((memberType) => memberType.name).join(" | "),
393
+ };
394
+ }
395
+ fromJsonExpression({ variables, }) {
396
+ // Assumes the JSON object has been recursively validated already.
397
+ return `${this.name}.fromJson(${variables.value}).unsafeCoerce()`;
398
+ }
399
+ fromRdfExpression({ variables, }) {
400
+ return `${variables.resourceValues}.head().chain(value => value.to${this.rdfjsResourceType().named ? "Named" : ""}Resource()).chain(_resource => ${this.name}.fromRdf({ ...${variables.context}, resource: _resource }))`;
401
+ }
402
+ hashStatements({ variables, }) {
403
+ switch (this.memberTypes[0].declarationType) {
404
+ case "class":
405
+ return [`${variables.value}.hash(${variables.hasher});`];
406
+ case "interface":
407
+ return [`${this.name}.hash(${variables.value}, ${variables.hasher});`];
408
+ }
409
+ }
410
+ jsonZodSchema() {
411
+ return `${this.name}.jsonZodSchema()`;
412
+ }
413
+ sparqlConstructTemplateTriples({ context, variables, }) {
414
+ switch (context) {
415
+ case "property":
416
+ return super.sparqlConstructTemplateTriples({ context, variables });
417
+ case "type":
418
+ return [
419
+ `...${this.name}.sparqlConstructTemplateTriples(${objectInitializer({
420
+ ignoreRdfType: true,
421
+ subject: variables.subject,
422
+ variablePrefix: variables.variablePrefix,
423
+ })})`,
424
+ ];
425
+ }
426
+ }
427
+ sparqlWherePatterns({ context, variables, }) {
428
+ switch (context) {
429
+ case "property":
430
+ return super.sparqlWherePatterns({ context, variables });
431
+ case "type":
432
+ return [
433
+ `...${this.name}.sparqlWherePatterns(${objectInitializer({
434
+ ignoreRdfType: true,
435
+ subject: variables.subject,
436
+ variablePrefix: variables.variablePrefix,
437
+ })})`,
438
+ ];
439
+ }
440
+ }
441
+ toJsonExpression({ variables, }) {
442
+ switch (this.memberTypes[0].declarationType) {
443
+ case "class":
444
+ return `${variables.value}.toJson()`;
445
+ case "interface":
446
+ throw new Error("not implemented: need a freestanding toJson function like the toRdf function");
447
+ // return `${this.name}.toJson(${variables.value})`;
448
+ }
449
+ }
450
+ toRdfExpression({ variables, }) {
451
+ const options = `{ mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }`;
452
+ switch (this.memberTypes[0].declarationType) {
453
+ case "class":
454
+ return `${variables.value}.toRdf(${options})`;
455
+ case "interface":
456
+ return `${this.name}.toRdf(${variables.value}, ${options})`;
457
+ }
458
+ }
459
+ rdfjsResourceType(options) {
460
+ const memberRdfjsResourceTypes = [];
461
+ for (const memberType of this.memberTypes) {
462
+ const memberRdfjsResourceType = memberType.rdfjsResourceType(options);
463
+ if (memberRdfjsResourceTypes.some((existingMemberRdfjsResourceType) => existingMemberRdfjsResourceType.name !==
464
+ memberRdfjsResourceType.name)) {
465
+ // The types don't agree, return a generic type
466
+ return {
467
+ mutable: !!options?.mutable,
468
+ name: "rdfjsResource.Resource",
469
+ named: false,
470
+ };
471
+ }
472
+ memberRdfjsResourceTypes.push(memberRdfjsResourceType);
473
+ }
474
+ // The types agree
475
+ return memberRdfjsResourceTypes[0];
476
+ }
477
+ }
478
+ __decorate([
479
+ Memoize()
480
+ ], ObjectUnionType.prototype, "thisVariable", null);
481
+ //# sourceMappingURL=ObjectUnionType.js.map
@@ -1,3 +1,4 @@
1
+ import { Import } from "./Import.js";
1
2
  import { Type } from "./Type.js";
2
3
  export declare class OptionType extends Type {
3
4
  readonly itemType: Type;
@@ -6,12 +7,19 @@ export declare class OptionType extends Type {
6
7
  itemType: Type;
7
8
  });
8
9
  get conversions(): readonly Type.Conversion[];
10
+ get equalsFunction(): string;
11
+ get jsonName(): string;
12
+ get mutable(): boolean;
9
13
  get name(): string;
10
- chainSparqlGraphPatternExpression(parameters: Parameters<Type["chainSparqlGraphPatternExpression"]>[0]): ReturnType<Type["chainSparqlGraphPatternExpression"]>;
11
- equalsFunction(): string;
14
+ get useImports(): readonly Import[];
15
+ fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
12
16
  fromRdfExpression(parameters: Parameters<Type["fromRdfExpression"]>[0]): string;
13
- hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
14
- propertySparqlGraphPatternExpression(parameters: Parameters<Type["propertySparqlGraphPatternExpression"]>[0]): Type.SparqlGraphPatternExpression;
17
+ hashStatements({ depth, variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
18
+ jsonUiSchemaElement(parameters: Parameters<Type["jsonUiSchemaElement"]>[0]): ReturnType<Type["jsonUiSchemaElement"]>;
19
+ jsonZodSchema(parameters: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
20
+ sparqlConstructTemplateTriples({ context, variables, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
21
+ sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
22
+ toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
15
23
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
16
24
  }
17
25
  //# sourceMappingURL=OptionType.d.ts.map
@@ -5,6 +5,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { Memoize } from "typescript-memoize";
8
+ import { Import } from "./Import.js";
8
9
  import { Type } from "./Type.js";
9
10
  export class OptionType extends Type {
10
11
  constructor({ itemType, ...superParameters }) {
@@ -28,19 +29,14 @@ export class OptionType extends Type {
28
29
  if (!conversions.some((conversion) => conversion.sourceTypeName === "undefined")) {
29
30
  conversions.push({
30
31
  conversionExpression: () => "purify.Maybe.empty()",
32
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "undefined"`,
31
33
  sourceTypeName: "undefined",
32
34
  });
33
35
  }
34
36
  return conversions;
35
37
  }
36
- get name() {
37
- return `purify.Maybe<${this.itemType.name}>`;
38
- }
39
- chainSparqlGraphPatternExpression(parameters) {
40
- return this.itemType.chainSparqlGraphPatternExpression(parameters);
41
- }
42
- equalsFunction() {
43
- const itemTypeEqualsFunction = this.itemType.equalsFunction();
38
+ get equalsFunction() {
39
+ const itemTypeEqualsFunction = this.itemType.equalsFunction;
44
40
  if (itemTypeEqualsFunction === "purifyHelpers.Equatable.equals") {
45
41
  return "purifyHelpers.Equatable.maybeEquals";
46
42
  }
@@ -49,32 +45,85 @@ export class OptionType extends Type {
49
45
  }
50
46
  return `(left, right) => purifyHelpers.Maybes.equals(left, right, ${itemTypeEqualsFunction})`;
51
47
  }
48
+ get jsonName() {
49
+ return `(${this.itemType.jsonName}) | undefined`;
50
+ }
51
+ get mutable() {
52
+ return this.itemType.mutable;
53
+ }
54
+ get name() {
55
+ return `purify.Maybe<${this.itemType.name}>`;
56
+ }
57
+ get useImports() {
58
+ return [...this.itemType.useImports, Import.PURIFY];
59
+ }
60
+ fromJsonExpression({ variables, }) {
61
+ const expression = `purify.Maybe.fromNullable(${variables.value})`;
62
+ const itemFromJsonExpression = this.itemType.fromJsonExpression({
63
+ variables: { value: "_item" },
64
+ });
65
+ return itemFromJsonExpression === "_item"
66
+ ? expression
67
+ : `${expression}.map(_item => (${itemFromJsonExpression}))`;
68
+ }
52
69
  fromRdfExpression(parameters) {
53
70
  return `purify.Either.of(${this.itemType.fromRdfExpression(parameters)}.toMaybe())`;
54
71
  }
55
- hashStatements({ variables, }) {
72
+ hashStatements({ depth, variables, }) {
56
73
  return [
57
- `${variables.value}.ifJust((value) => { ${this.itemType
74
+ `${variables.value}.ifJust((_value${depth}) => { ${this.itemType
58
75
  .hashStatements({
76
+ depth: depth + 1,
59
77
  variables: {
60
78
  hasher: variables.hasher,
61
- value: "value",
79
+ value: `_value${depth}`,
62
80
  },
63
81
  })
64
82
  .join("\n")} })`,
65
83
  ];
66
84
  }
67
- propertySparqlGraphPatternExpression(parameters) {
68
- return new Type.SparqlGraphPatternExpression(`sparqlBuilder.GraphPattern.optional(${this.itemType.propertySparqlGraphPatternExpression(parameters).toSparqlGraphPatternExpression()})`);
85
+ jsonUiSchemaElement(parameters) {
86
+ return this.itemType.jsonUiSchemaElement(parameters);
87
+ }
88
+ jsonZodSchema(parameters) {
89
+ return `${this.itemType.jsonZodSchema(parameters)}.optional()`;
90
+ }
91
+ sparqlConstructTemplateTriples({ context, variables, }) {
92
+ switch (context) {
93
+ case "property":
94
+ return super.sparqlConstructTemplateTriples({ context, variables });
95
+ case "type":
96
+ return this.itemType.sparqlConstructTemplateTriples({
97
+ context,
98
+ variables,
99
+ });
100
+ }
101
+ }
102
+ sparqlWherePatterns({ context, variables, }) {
103
+ switch (context) {
104
+ case "property": {
105
+ const patterns = super.sparqlWherePatterns({ context, variables });
106
+ if (patterns.length === 0) {
107
+ return [];
108
+ }
109
+ return [`{ patterns: [${patterns.join(", ")}], type: "optional" }`];
110
+ }
111
+ case "type": {
112
+ return this.itemType.sparqlWherePatterns({ context, variables });
113
+ }
114
+ }
115
+ }
116
+ toJsonExpression({ variables, }) {
117
+ return `${variables.value}.map(_item => (${this.itemType.toJsonExpression({ variables: { value: "_item" } })})).extract()`;
69
118
  }
70
119
  toRdfExpression({ variables, }) {
71
120
  const itemTypeToRdfExpression = this.itemType.toRdfExpression({
72
- variables: { ...variables, value: "value" },
121
+ variables: { ...variables, value: "_value" },
73
122
  });
74
- if (itemTypeToRdfExpression === "value") {
123
+ if (itemTypeToRdfExpression === "_value") {
75
124
  return variables.value;
76
125
  }
77
- return `${variables.value}.map((value) => ${itemTypeToRdfExpression})`;
126
+ return `${variables.value}.map((_value) => ${itemTypeToRdfExpression})`;
78
127
  }
79
128
  }
80
129
  __decorate([
@@ -1,11 +1,21 @@
1
1
  import { Maybe } from "purify-ts";
2
+ import type { Import } from "./Import.js";
2
3
  import { LiteralType } from "./LiteralType.js";
3
4
  import type { Type } from "./Type.js";
4
- export declare abstract class PrimitiveType extends LiteralType {
5
- get conversions(): readonly Type.Conversion[];
5
+ export declare abstract class PrimitiveType<ValueT extends boolean | Date | string | number> extends LiteralType {
6
+ readonly equalsFunction: string;
7
+ readonly primitiveDefaultValue: Maybe<ValueT>;
8
+ readonly primitiveIn: readonly ValueT[];
9
+ constructor({ primitiveDefaultValue, primitiveIn, ...superParameters }: {
10
+ primitiveDefaultValue: Maybe<ValueT>;
11
+ primitiveIn: readonly ValueT[];
12
+ } & ConstructorParameters<typeof LiteralType>[0]);
6
13
  get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
7
- get importStatements(): readonly string[];
8
- equalsFunction(): string;
9
- toRdfExpression({ variables, }: Parameters<LiteralType["toRdfExpression"]>[0]): string;
14
+ get jsonName(): string;
15
+ get useImports(): readonly Import[];
16
+ fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
17
+ hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
18
+ toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
19
+ protected propertyFilterRdfResourceValuesExpression({ variables, }: Parameters<LiteralType["propertyFilterRdfResourceValuesExpression"]>[0]): string;
10
20
  }
11
21
  //# sourceMappingURL=PrimitiveType.d.ts.map