@shaclmate/compiler 2.0.13 → 2.0.15

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