@shaclmate/compiler 2.0.22 → 2.0.23

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 (172) hide show
  1. package/dist/ShapesGraphToAstTransformer.d.ts +1 -0
  2. package/dist/ShapesGraphToAstTransformer.js +56 -12
  3. package/dist/_ShapesGraphToAstTransformer/flattenAstObjectCompositeTypeMemberTypes.d.ts +13 -0
  4. package/dist/_ShapesGraphToAstTransformer/flattenAstObjectCompositeTypeMemberTypes.js +51 -0
  5. package/dist/_ShapesGraphToAstTransformer/shapeAstName.js +53 -12
  6. package/dist/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.d.ts +5 -0
  7. package/dist/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +130 -54
  8. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +2 -3
  9. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +122 -129
  10. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +2 -2
  11. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +2 -2
  12. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +127 -5
  13. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +3 -3
  14. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +2 -3
  15. package/dist/ast/Ast.d.ts +0 -1
  16. package/dist/ast/Curie.d.ts +14 -0
  17. package/dist/ast/Curie.js +21 -0
  18. package/dist/ast/IdentifierType.d.ts +3 -2
  19. package/dist/ast/ListType.d.ts +2 -1
  20. package/dist/ast/LiteralType.d.ts +1 -1
  21. package/dist/ast/Name.d.ts +40 -5
  22. package/dist/ast/ObjectCompositeType.d.ts +11 -0
  23. package/dist/ast/ObjectType.d.ts +18 -13
  24. package/dist/ast/ObjectUnionType.d.ts +0 -11
  25. package/dist/ast/OptionType.d.ts +2 -2
  26. package/dist/ast/SetType.d.ts +2 -2
  27. package/dist/ast/TermType.d.ts +12 -4
  28. package/dist/ast/Type.d.ts +1 -1
  29. package/dist/enums/TsFeature.d.ts +1 -4
  30. package/dist/enums/TsFeature.js +1 -15
  31. package/dist/generators/json/AstJsonGenerator.js +19 -3
  32. package/dist/generators/ts/BooleanType.d.ts +3 -1
  33. package/dist/generators/ts/BooleanType.js +11 -2
  34. package/dist/generators/ts/DateTimeType.d.ts +8 -7
  35. package/dist/generators/ts/DateTimeType.js +31 -7
  36. package/dist/generators/ts/DateType.d.ts +5 -2
  37. package/dist/generators/ts/DateType.js +15 -1
  38. package/dist/generators/ts/DeclaredType.d.ts +2 -2
  39. package/dist/generators/ts/DeclaredType.js +2 -2
  40. package/dist/generators/ts/FloatType.d.ts +6 -0
  41. package/dist/generators/ts/FloatType.js +18 -0
  42. package/dist/generators/ts/IdentifierType.d.ts +12 -7
  43. package/dist/generators/ts/IdentifierType.js +94 -6
  44. package/dist/generators/ts/Import.d.ts +2 -0
  45. package/dist/generators/ts/Import.js +10 -0
  46. package/dist/generators/ts/IntType.d.ts +6 -0
  47. package/dist/generators/ts/IntType.js +18 -0
  48. package/dist/generators/ts/ListType.d.ts +15 -12
  49. package/dist/generators/ts/ListType.js +98 -57
  50. package/dist/generators/ts/LiteralType.d.ts +10 -9
  51. package/dist/generators/ts/LiteralType.js +13 -2
  52. package/dist/generators/ts/NumberType.d.ts +2 -1
  53. package/dist/generators/ts/NumberType.js +7 -2
  54. package/dist/generators/ts/ObjectType.d.ts +66 -43
  55. package/dist/generators/ts/ObjectType.js +144 -123
  56. package/dist/generators/ts/ObjectUnionType.d.ts +49 -8
  57. package/dist/generators/ts/ObjectUnionType.js +290 -97
  58. package/dist/generators/ts/OptionType.d.ts +14 -10
  59. package/dist/generators/ts/OptionType.js +60 -34
  60. package/dist/generators/ts/PrimitiveType.d.ts +4 -4
  61. package/dist/generators/ts/PrimitiveType.js +18 -3
  62. package/dist/generators/ts/SetType.d.ts +16 -12
  63. package/dist/generators/ts/SetType.js +69 -40
  64. package/dist/generators/ts/SnippetDeclarations.d.ts +13 -8
  65. package/dist/generators/ts/SnippetDeclarations.js +215 -101
  66. package/dist/generators/ts/StringType.d.ts +3 -1
  67. package/dist/generators/ts/StringType.js +14 -2
  68. package/dist/generators/ts/TermType.d.ts +22 -14
  69. package/dist/generators/ts/TermType.js +53 -25
  70. package/dist/generators/ts/TsGenerator.d.ts +2 -1
  71. package/dist/generators/ts/TsGenerator.js +23 -12
  72. package/dist/generators/ts/Type.d.ts +89 -31
  73. package/dist/generators/ts/Type.js +57 -54
  74. package/dist/generators/ts/TypeFactory.d.ts +4 -5
  75. package/dist/generators/ts/TypeFactory.js +290 -226
  76. package/dist/generators/ts/UnionType.d.ts +14 -10
  77. package/dist/generators/ts/UnionType.js +319 -142
  78. package/dist/generators/ts/_ObjectType/EagerShaclProperty.d.ts +12 -0
  79. package/dist/generators/ts/_ObjectType/EagerShaclProperty.js +30 -0
  80. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.d.ts +13 -12
  81. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.js +46 -47
  82. package/dist/generators/ts/_ObjectType/IdentifierProperty.d.ts +20 -13
  83. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +152 -124
  84. package/dist/generators/ts/_ObjectType/LazyShaclProperty.d.ts +87 -0
  85. package/dist/generators/ts/_ObjectType/LazyShaclProperty.js +237 -0
  86. package/dist/generators/ts/_ObjectType/Property.d.ts +39 -44
  87. package/dist/generators/ts/_ObjectType/Property.js +1 -10
  88. package/dist/generators/ts/_ObjectType/ShaclProperty.d.ts +25 -30
  89. package/dist/generators/ts/_ObjectType/ShaclProperty.js +108 -85
  90. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +21 -17
  91. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +61 -53
  92. package/dist/generators/ts/_ObjectType/classDeclaration.js +6 -5
  93. package/dist/generators/ts/_ObjectType/createFunctionDeclaration.js +5 -4
  94. package/dist/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +5 -4
  95. package/dist/generators/ts/_ObjectType/fromRdfTypeVariableStatement.d.ts +1 -1
  96. package/dist/generators/ts/_ObjectType/fromRdfTypeVariableStatement.js +8 -5
  97. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.d.ts +5 -0
  98. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.js +37 -0
  99. package/dist/generators/ts/_ObjectType/hashFunctionOrMethodDeclarations.js +7 -8
  100. package/dist/generators/ts/_ObjectType/identifierTypeDeclarations.d.ts +7 -0
  101. package/dist/generators/ts/_ObjectType/identifierTypeDeclarations.js +54 -0
  102. package/dist/generators/ts/_ObjectType/index.d.ts +9 -7
  103. package/dist/generators/ts/_ObjectType/index.js +9 -7
  104. package/dist/generators/ts/_ObjectType/interfaceDeclaration.js +1 -1
  105. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.d.ts +4 -0
  106. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.js +189 -0
  107. package/dist/generators/ts/_ObjectType/jsonTypeAliasDeclaration.d.ts +5 -0
  108. package/dist/generators/ts/_ObjectType/jsonTypeAliasDeclaration.js +28 -0
  109. package/dist/generators/ts/_ObjectType/objectSetMethodNames.d.ts +9 -0
  110. package/dist/generators/ts/_ObjectType/objectSetMethodNames.js +18 -0
  111. package/dist/generators/ts/_ObjectType/propertiesVariableStatement.d.ts +5 -0
  112. package/dist/generators/ts/_ObjectType/propertiesVariableStatement.js +37 -0
  113. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.d.ts +4 -0
  114. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.js +152 -0
  115. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.d.ts +1 -1
  116. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +4 -3
  117. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.d.ts +1 -1
  118. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +3 -2
  119. package/dist/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +103 -35
  120. package/dist/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +6 -6
  121. package/dist/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +15 -13
  122. package/dist/generators/ts/graphqlSchemaVariableStatement.d.ts +9 -0
  123. package/dist/generators/ts/graphqlSchemaVariableStatement.js +86 -0
  124. package/dist/generators/ts/objectSetDeclarations.d.ts +8 -0
  125. package/dist/generators/ts/objectSetDeclarations.js +59 -0
  126. package/dist/generators/ts/objectSetInterfaceDeclaration.d.ts +8 -0
  127. package/dist/generators/ts/objectSetInterfaceDeclaration.js +46 -0
  128. package/dist/generators/ts/objectSetMethodSignatures.d.ts +11 -0
  129. package/dist/generators/ts/objectSetMethodSignatures.js +52 -0
  130. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.d.ts +8 -0
  131. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.js +393 -0
  132. package/dist/generators/ts/rdfjsTermExpression.d.ts +3 -0
  133. package/dist/generators/ts/rdfjsTermExpression.js +57 -0
  134. package/dist/generators/ts/sparqlObjectSetClassDeclaration.d.ts +8 -0
  135. package/dist/generators/ts/sparqlObjectSetClassDeclaration.js +415 -0
  136. package/dist/generators/ts/syntheticNamePrefix.d.ts +2 -0
  137. package/dist/generators/ts/syntheticNamePrefix.js +2 -0
  138. package/dist/generators/ts/tsName.js +27 -13
  139. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.d.ts +10 -0
  140. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.js +19 -0
  141. package/dist/input/NodeShape.d.ts +6 -8
  142. package/dist/input/NodeShape.js +20 -44
  143. package/dist/input/Ontology.d.ts +0 -3
  144. package/dist/input/Ontology.js +0 -9
  145. package/dist/input/PropertyPath.d.ts +6 -5
  146. package/dist/input/PropertyPath.js +14 -22
  147. package/dist/input/PropertyShape.d.ts +3 -1
  148. package/dist/input/PropertyShape.js +8 -2
  149. package/dist/input/ShapesGraph.js +4 -4
  150. package/dist/input/generated.d.ts +923 -105
  151. package/dist/input/generated.js +1865 -969
  152. package/dist/input/tsFeatures.d.ts +3 -2
  153. package/dist/input/tsFeatures.js +44 -27
  154. package/package.json +18 -16
  155. package/dist/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +0 -4
  156. package/dist/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +0 -78
  157. package/dist/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +0 -4
  158. package/dist/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +0 -91
  159. package/dist/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +0 -5
  160. package/dist/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +0 -19
  161. package/dist/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +0 -5
  162. package/dist/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +0 -31
  163. package/dist/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +0 -5
  164. package/dist/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +0 -37
  165. package/dist/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +0 -6
  166. package/dist/generators/ts/_ObjectType/rdfjsTermExpression.js +0 -17
  167. package/dist/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +0 -5
  168. package/dist/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +0 -19
  169. package/dist/generators/ts/_ObjectType/toJsonReturnType.d.ts +0 -3
  170. package/dist/generators/ts/_ObjectType/toJsonReturnType.js +0 -17
  171. package/dist/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +0 -5
  172. package/dist/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +0 -19
@@ -12,18 +12,26 @@ import { Import } from "./Import.js";
12
12
  import { SnippetDeclarations } from "./SnippetDeclarations.js";
13
13
  import { Type } from "./Type.js";
14
14
  import { objectInitializer } from "./objectInitializer.js";
15
+ import { rdfjsTermExpression } from "./rdfjsTermExpression.js";
16
+ import { syntheticNamePrefix } from "./syntheticNamePrefix.js";
15
17
  /**
16
18
  * Abstract base class for IdentifierType and LiteralType.
19
+ *
20
+ * ConstantTermT is the type of sh:defaultValue, sh:hasValue, and sh:in.
21
+ * RuntimeTermT is the type of values at runtime.
22
+ *
23
+ * The two are differentiated because identifiers can have BlankNode or NamedNode values at runtime but only NamedNode values for sh:defaultValue et al.
17
24
  */
18
25
  export class TermType extends Type {
19
26
  defaultValue;
20
- equalsFunction = "booleanEquals";
27
+ equalsFunction = `${syntheticNamePrefix}booleanEquals`;
21
28
  hasValues;
22
29
  in_;
23
30
  mutable = false;
24
31
  nodeKinds;
25
- constructor({ defaultValue, hasValues, in_, nodeKinds, ...superParameters }) {
26
- super(superParameters);
32
+ typeof = "object";
33
+ constructor({ defaultValue, hasValues, in_, nodeKinds, }) {
34
+ super();
27
35
  this.defaultValue = defaultValue;
28
36
  this.hasValues = hasValues;
29
37
  this.in_ = in_;
@@ -34,26 +42,26 @@ export class TermType extends Type {
34
42
  const conversions = [];
35
43
  if (this.nodeKinds.has("Literal")) {
36
44
  conversions.push({
37
- conversionExpression: (value) => `rdfLiteral.toRdf(${value}, ${objectInitializer({ dataFactory: this.dataFactoryVariable })})`,
45
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value}, ${objectInitializer({ dataFactory: "dataFactory" })})`,
38
46
  sourceTypeCheckExpression: (value) => `typeof ${value} === "boolean"`,
39
47
  sourceTypeName: "boolean",
40
48
  }, {
41
- conversionExpression: (value) => `rdfLiteral.toRdf(${value}, ${objectInitializer({ dataFactory: this.dataFactoryVariable })})`,
49
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value}, ${objectInitializer({ dataFactory: "dataFactory" })})`,
42
50
  sourceTypeCheckExpression: (value) => `typeof ${value} === "object" && ${value} instanceof Date`,
43
51
  sourceTypeName: "Date",
44
52
  }, {
45
- conversionExpression: (value) => `rdfLiteral.toRdf(${value}, ${objectInitializer({ dataFactory: this.dataFactoryVariable })})`,
53
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value}, ${objectInitializer({ dataFactory: "dataFactory" })})`,
46
54
  sourceTypeCheckExpression: (value) => `typeof ${value} === "number"`,
47
55
  sourceTypeName: "number",
48
56
  }, {
49
- conversionExpression: (value) => `${this.dataFactoryVariable}.literal(${value})`,
57
+ conversionExpression: (value) => `dataFactory.literal(${value})`,
50
58
  sourceTypeCheckExpression: (value) => `typeof ${value} === "string"`,
51
59
  sourceTypeName: "string",
52
60
  });
53
61
  }
54
62
  this.defaultValue.ifJust((defaultValue) => {
55
63
  conversions.push({
56
- conversionExpression: () => this.rdfjsTermExpression(defaultValue),
64
+ conversionExpression: () => rdfjsTermExpression(defaultValue),
57
65
  sourceTypeCheckExpression: (value) => `typeof ${value} === "undefined"`,
58
66
  sourceTypeName: "undefined",
59
67
  });
@@ -68,17 +76,21 @@ export class TermType extends Type {
68
76
  get discriminatorProperty() {
69
77
  return Maybe.of({
70
78
  name: "termType",
79
+ ownValues: [...this.nodeKinds],
80
+ descendantValues: [],
71
81
  type: "string",
72
- values: [...this.nodeKinds],
73
82
  });
74
83
  }
84
+ get graphqlName() {
85
+ throw new Error("not implemented");
86
+ }
75
87
  get jsonName() {
76
88
  invariant(this.nodeKinds.has("Literal") &&
77
89
  (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
78
- return `{ readonly "@id": string, readonly termType: ${[...this.nodeKinds]
90
+ return new Type.JsonName(`{ readonly "@id": string, readonly termType: ${[...this.nodeKinds]
79
91
  .filter((nodeKind) => nodeKind !== "Literal")
80
92
  .map((nodeKind) => `"${nodeKind}"`)
81
- .join(" | ")} } | { readonly "@language": string | undefined, readonly "@type": string | undefined, readonly "@value": string, readonly termType: "Literal" }`;
93
+ .join(" | ")} } | { readonly "@language"?: string, readonly "@type"?: string, readonly "@value": string, readonly termType: "Literal" }`);
82
94
  }
83
95
  get name() {
84
96
  return `(${[...this.nodeKinds]
@@ -92,13 +104,13 @@ export class TermType extends Type {
92
104
  let valueToNodeKind;
93
105
  switch (nodeKind) {
94
106
  case "BlankNode":
95
- valueToNodeKind = `${this.dataFactoryVariable}.blankNode(${variables.value}["@id"].substring(2))`;
107
+ valueToNodeKind = `dataFactory.blankNode(${variables.value}["@id"].substring(2))`;
96
108
  break;
97
109
  case "Literal":
98
- valueToNodeKind = `${this.dataFactoryVariable}.literal(${variables.value}["@value"], typeof ${variables.value}["@language"] !== "undefined" ? ${variables.value}["@language"] : (typeof ${variables.value}["@type"] !== "undefined" ? dataFactory.namedNode(${variables.value}["@type"]) : undefined))`;
110
+ valueToNodeKind = `dataFactory.literal(${variables.value}["@value"], typeof ${variables.value}["@language"] !== "undefined" ? ${variables.value}["@language"] : (typeof ${variables.value}["@type"] !== "undefined" ? dataFactory.namedNode(${variables.value}["@type"]) : undefined))`;
99
111
  break;
100
112
  case "NamedNode":
101
- valueToNodeKind = `${this.dataFactoryVariable}.namedNode(${variables.value}["@id"])`;
113
+ valueToNodeKind = `dataFactory.namedNode(${variables.value}["@id"])`;
102
114
  break;
103
115
  default:
104
116
  throw new RangeError(nodeKind);
@@ -114,7 +126,7 @@ export class TermType extends Type {
114
126
  ];
115
127
  // Have an rdfjsResource.Resource.Values here
116
128
  if (this.hasValues.length === 1) {
117
- chain.push(`find(_value => _value.toTerm().equals(${this.rdfjsTermExpression(this.hasValues[0])}))`);
129
+ chain.push(`find(value => value.toTerm().equals(${rdfjsTermExpression(this.hasValues[0])}))`);
118
130
  }
119
131
  else {
120
132
  chain.push("head()");
@@ -122,24 +134,30 @@ export class TermType extends Type {
122
134
  // Have an rdfjsResource.Resource.Value here
123
135
  this.defaultValue.ifJust((defaultValue) => {
124
136
  // alt the default value before trying to convert the rdfjsResource.Resource.Value to the type
125
- chain.push(`alt(purify.Either.of(new rdfjsResource.Resource.Value(${objectInitializer({ subject: variables.resource, predicate: variables.predicate, object: this.rdfjsTermExpression(defaultValue) })})))`);
137
+ chain.push(`alt(purify.Either.of(new rdfjsResource.Resource.Value(${objectInitializer({ subject: variables.resource, predicate: variables.predicate, object: rdfjsTermExpression(defaultValue) })})))`);
126
138
  });
127
139
  // Last step: convert the rdfjsResource.Resource.Value to the type
128
- chain.push(`chain(_value => ${this.propertyFromRdfResourceValueExpression({
140
+ chain.push(`chain(value => ${this.propertyFromRdfResourceValueExpression({
129
141
  variables: {
130
142
  predicate: variables.predicate,
131
143
  resource: variables.resource,
132
- resourceValue: "_value",
144
+ resourceValue: "value",
133
145
  },
134
146
  })})`);
135
147
  return chain.join(".");
136
148
  }
149
+ graphqlResolveExpression(_parameters) {
150
+ throw new Error("not implemented");
151
+ }
137
152
  hashStatements({ variables, }) {
138
153
  return [
139
154
  `${variables.hasher}.update(${variables.value}.termType);`,
140
155
  `${variables.hasher}.update(${variables.value}.value);`,
141
156
  ];
142
157
  }
158
+ jsonUiSchemaElement() {
159
+ return Maybe.empty();
160
+ }
143
161
  jsonZodSchema({ variables, }) {
144
162
  invariant(this.nodeKinds.has("Literal") &&
145
163
  (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
@@ -159,7 +177,7 @@ export class TermType extends Type {
159
177
  })
160
178
  .join(", ")}])`;
161
179
  }
162
- snippetDeclarations(features) {
180
+ snippetDeclarations({ features, }) {
163
181
  const snippetDeclarations = [];
164
182
  if (features.has("equals")) {
165
183
  snippetDeclarations.push(SnippetDeclarations.booleanEquals);
@@ -168,13 +186,13 @@ export class TermType extends Type {
168
186
  }
169
187
  sparqlWherePatterns(parameters) {
170
188
  switch (parameters.context) {
171
- case "property":
189
+ case "object":
172
190
  return this.defaultValue
173
191
  .map(() => [
174
192
  `{ patterns: [${super.sparqlWherePatterns(parameters).join(", ")}], type: "optional" }`,
175
193
  ])
176
194
  .orDefault(super.sparqlWherePatterns(parameters));
177
- case "type":
195
+ case "subject":
178
196
  return super.sparqlWherePatterns(parameters);
179
197
  }
180
198
  }
@@ -203,7 +221,7 @@ export class TermType extends Type {
203
221
  }
204
222
  toRdfExpression({ variables, }) {
205
223
  return this.defaultValue
206
- .map((defaultValue) => `!${variables.value}.equals(${this.rdfjsTermExpression(defaultValue)}) ? ${variables.value} : undefined`)
224
+ .map((defaultValue) => `!${variables.value}.equals(${rdfjsTermExpression(defaultValue)}) ? ${variables.value} : undefined`)
207
225
  .orDefault(variables.value);
208
226
  }
209
227
  useImports() {
@@ -229,17 +247,27 @@ export class TermType extends Type {
229
247
  propertyFromRdfResourceValueExpression({ variables, }) {
230
248
  invariant(this.nodeKinds.has("Literal") &&
231
249
  (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
232
- let expression = `purify.Either.of(${variables.resourceValue}.toTerm())`;
250
+ let expression = `purify.Either.of<Error, rdfjs.BlankNode | rdfjs.Literal | rdfjs.NamedNode>(${variables.resourceValue}.toTerm())`;
233
251
  if (this.nodeKinds.size < 3) {
252
+ const eitherTypeParameters = `<Error, ${this.name}>`;
234
253
  expression = `${expression}.chain(term => {
235
254
  switch (term.termType) {
236
- ${[...this.nodeKinds].map((nodeKind) => `case "${nodeKind}":`).join("\n")} return purify.Either.of(term);
237
- default: return purify.Left(new rdfjsResource.Resource.MistypedValueError(${objectInitializer({ actualValue: "term", expectedValueType: JSON.stringify(this.name), focusResource: variables.resource, predicate: variables.predicate })}));
255
+ ${[...this.nodeKinds].map((nodeKind) => `case "${nodeKind}":`).join("\n")} return purify.Either.of${eitherTypeParameters}(term);
256
+ default: return purify.Left${eitherTypeParameters}(new rdfjsResource.Resource.MistypedValueError(${objectInitializer({ actualValue: "term", expectedValueType: JSON.stringify(this.name), focusResource: variables.resource, predicate: variables.predicate })}));
238
257
  }})`;
239
258
  }
240
259
  return expression;
241
260
  }
242
261
  }
262
+ __decorate([
263
+ Memoize()
264
+ ], TermType.prototype, "conversions", null);
265
+ __decorate([
266
+ Memoize()
267
+ ], TermType.prototype, "discriminatorProperty", null);
268
+ __decorate([
269
+ Memoize()
270
+ ], TermType.prototype, "jsonName", null);
243
271
  __decorate([
244
272
  Memoize()
245
273
  ], TermType.prototype, "name", null);
@@ -1,7 +1,8 @@
1
1
  import * as ast from "../../ast/index.js";
2
2
  import type { Generator } from "../Generator.js";
3
3
  export declare class TsGenerator implements Generator {
4
+ private readonly typeFactory;
4
5
  generate(ast_: ast.Ast): string;
5
- private addDeclarations;
6
+ private addStatements;
6
7
  }
7
8
  //# sourceMappingURL=TsGenerator.d.ts.map
@@ -3,23 +3,22 @@ import * as ast from "../../ast/index.js";
3
3
  import { ObjectType } from "./ObjectType.js";
4
4
  import { ObjectUnionType } from "./ObjectUnionType.js";
5
5
  import { TypeFactory } from "./TypeFactory.js";
6
+ import { graphqlSchemaVariableStatement } from "./graphqlSchemaVariableStatement.js";
7
+ import { objectSetDeclarations } from "./objectSetDeclarations.js";
6
8
  export class TsGenerator {
9
+ typeFactory = new TypeFactory();
7
10
  generate(ast_) {
8
- const sortedAstObjectTypes = ast.ObjectType.toposort(ast_.objectTypes);
9
11
  const project = new Project({
10
12
  useInMemoryFileSystem: true,
11
13
  });
12
14
  const sourceFile = project.createSourceFile("generated.ts");
13
- const typeFactory = new TypeFactory({
14
- dataFactoryVariable: ast_.tsDataFactoryVariable,
15
- });
16
- this.addDeclarations({
17
- objectTypes: sortedAstObjectTypes.flatMap((astObjectType) => {
18
- const type = typeFactory.createTypeFromAstType(astObjectType);
15
+ this.addStatements({
16
+ objectTypes: ast.ObjectType.toposort(ast_.objectTypes).flatMap((astObjectType) => {
17
+ const type = this.typeFactory.createTypeFromAstType(astObjectType);
19
18
  return type instanceof ObjectType ? [type] : [];
20
19
  }),
21
20
  objectUnionTypes: ast_.objectUnionTypes.flatMap((astObjectUnionType) => {
22
- const type = typeFactory.createTypeFromAstType(astObjectUnionType);
21
+ const type = this.typeFactory.createTypeFromAstType(astObjectUnionType);
23
22
  return type instanceof ObjectUnionType ? [type] : [];
24
23
  }),
25
24
  sourceFile,
@@ -27,9 +26,9 @@ export class TsGenerator {
27
26
  sourceFile.saveSync();
28
27
  return project.getFileSystem().readFileSync(sourceFile.getFilePath());
29
28
  }
30
- addDeclarations({ objectTypes, objectUnionTypes, sourceFile, }) {
29
+ addStatements({ objectTypes, objectUnionTypes, sourceFile, }) {
31
30
  // sourceFile.addStatements(this.configuration.dataFactoryImport);
32
- sourceFile.addStatements('import { DataFactory as dataFactory } from "n3"');
31
+ sourceFile.addStatements('import N3, { DataFactory as dataFactory } from "n3"');
33
32
  const declaredTypes = [
34
33
  ...objectTypes,
35
34
  ...objectUnionTypes,
@@ -58,20 +57,32 @@ export class TsGenerator {
58
57
  // Deduplicate and add snippet declarations
59
58
  const addedSnippetDeclarations = new Set();
60
59
  for (const declaredType of declaredTypes) {
61
- for (const snippetDeclaration of declaredType.snippetDeclarations(declaredType.features)) {
60
+ for (const snippetDeclaration of declaredType.snippetDeclarations({
61
+ features: declaredType.features,
62
+ recursionStack: [],
63
+ })) {
62
64
  if (!addedSnippetDeclarations.has(snippetDeclaration)) {
63
65
  sourceFile.addStatements([snippetDeclaration]);
64
66
  addedSnippetDeclarations.add(snippetDeclaration);
65
67
  }
66
68
  }
67
69
  }
68
- // Add type declarations
69
70
  for (const objectType of objectTypes) {
70
71
  sourceFile.addStatements(objectType.declarations);
71
72
  }
72
73
  for (const objectUnionType of objectUnionTypes) {
73
74
  sourceFile.addStatements(objectUnionType.declarations);
74
75
  }
76
+ const objectTypesSortedByName = objectTypes.toSorted((left, right) => left.name.localeCompare(right.name));
77
+ const objectUnionTypesSortedByName = objectUnionTypes.toSorted((left, right) => left.name.localeCompare(right.name));
78
+ sourceFile.addStatements(objectSetDeclarations({
79
+ objectTypes: objectTypesSortedByName,
80
+ objectUnionTypes: objectUnionTypesSortedByName,
81
+ }));
82
+ sourceFile.addVariableStatements(graphqlSchemaVariableStatement({
83
+ objectTypes: objectTypesSortedByName,
84
+ objectUnionTypes: objectUnionTypesSortedByName,
85
+ }).toList());
75
86
  }
76
87
  }
77
88
  //# sourceMappingURL=TsGenerator.js.map
@@ -1,5 +1,4 @@
1
- import type { BlankNode, Literal, NamedNode, Variable } from "@rdfjs/types";
2
- import { Maybe } from "purify-ts";
1
+ import type { Maybe } from "purify-ts";
3
2
  import type { TsFeature } from "../../enums/index.js";
4
3
  import type { Import } from "./Import.js";
5
4
  /**
@@ -12,33 +11,38 @@ export declare abstract class Type {
12
11
  * Expressions that convert a source type or types to this type. It should include the type itself.
13
12
  */
14
13
  abstract readonly conversions: readonly Type.Conversion[];
14
+ /**
15
+ * A property that discriminates sub-types of this type e.g., termType on RDF/JS terms.
16
+ */
17
+ abstract readonly discriminatorProperty: Maybe<Type.DiscriminatorProperty>;
15
18
  /**
16
19
  * A function (reference or declaration) that compares two property values of this type, returning a
17
- * EqualsResult.
20
+ * $EqualsResult.
18
21
  */
19
22
  abstract readonly equalsFunction: string;
20
23
  /**
21
- * JSON-compatible returned by propertyToJsonExpression.
24
+ * GraphQL-compatible version of the type.
25
+ */
26
+ abstract readonly graphqlName: Type.GraphqlName;
27
+ /**
28
+ * JSON-compatible version of the type.
22
29
  */
23
- abstract readonly jsonName: string;
30
+ abstract readonly jsonName: Type.JsonName;
24
31
  /**
25
32
  * Is a value of this type mutable?
26
33
  */
27
34
  abstract readonly mutable: boolean;
28
35
  /**
29
- * Name of the type.
36
+ * TypeScript name of the type.
30
37
  */
31
38
  abstract readonly name: string;
32
- protected readonly dataFactoryVariable: string;
33
- constructor({ dataFactoryVariable, }: {
34
- dataFactoryVariable: string;
35
- });
36
39
  /**
37
- * A property that discriminates sub-types of this type e.g., termType on RDF/JS terms.
40
+ * JavaScript typeof the type.
38
41
  */
39
- get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
42
+ abstract readonly typeof: "boolean" | "object" | "number" | "string";
40
43
  /**
41
- * An expression that converts a JSON object in the same format as the propertyToJsonExpression to a value of this type.
44
+ * An expression that converts this type's JSON type to a value of this type. It doesn't return a purify.Either because the JSON has
45
+ * already been validated and converted to the expected JSON type with Zod.
42
46
  */
43
47
  abstract fromJsonExpression(parameters: {
44
48
  variables: {
@@ -46,18 +50,28 @@ export declare abstract class Type {
46
50
  };
47
51
  }): string;
48
52
  /**
49
- * An expression that converts a rdfjsResource.Resource.Values to an Either of value/values
53
+ * An expression that converts a rdfjsResource.Resource.Values to a purify.Either of value/values
50
54
  * of this type for a property.
51
55
  */
52
56
  abstract fromRdfExpression(parameters: {
53
57
  variables: {
54
58
  context: string;
59
+ ignoreRdfType?: boolean;
55
60
  languageIn: string;
61
+ objectSet: string;
56
62
  predicate: string;
57
63
  resource: string;
58
64
  resourceValues: string;
59
65
  };
60
66
  }): string;
67
+ /**
68
+ * An expression that resolves a value of this type in the GraphQL server.
69
+ */
70
+ abstract graphqlResolveExpression(parameters: {
71
+ variables: {
72
+ value: string;
73
+ };
74
+ }): string;
61
75
  /**
62
76
  * Statements that use hasher.update to hash a property value of this type.
63
77
  */
@@ -71,15 +85,22 @@ export declare abstract class Type {
71
85
  /**
72
86
  * Element object for a JSON Forms UI schema.
73
87
  */
74
- jsonUiSchemaElement(_parameters: {
88
+ abstract jsonUiSchemaElement(parameters: {
75
89
  variables: {
76
90
  scopePrefix: string;
77
91
  };
78
92
  }): Maybe<string>;
79
93
  /**
80
- * Zod schema for the JSON version of the type (the result of propertyToJson).
94
+ * Zod schema for the JSON type of this type.
95
+ *
96
+ * This method is called in two contexts:
97
+ * "property": from a ShaclProperty, while generating the z.object properties of an ObjectType
98
+ * "type": from another Type e.g., an OptionType or UnionType
99
+ *
100
+ * z.lazy() should only be returned for "property".
81
101
  */
82
102
  abstract jsonZodSchema(parameters: {
103
+ context: "property" | "type";
83
104
  variables: {
84
105
  zod: string;
85
106
  };
@@ -92,18 +113,22 @@ export declare abstract class Type {
92
113
  *
93
114
  * The generator deduplicates snippet declarations across all types before adding them to the source.
94
115
  */
95
- snippetDeclarations(_features: Set<TsFeature>): readonly string[];
116
+ abstract snippetDeclarations(parameters: {
117
+ features: Set<TsFeature>;
118
+ recursionStack: Type[];
119
+ }): readonly string[];
96
120
  /**
97
121
  * An array of SPARQL.js CONSTRUCT template triples for a value of this type, as strings (so they can incorporate runtime calls).
98
122
  *
99
123
  * This method is called in two contexts:
100
- * (1) By an ObjectType.Property. The property expects a basic graph pattern (subject, property path, property object). The property calls this method to get additional triples in which the propertyObject is the subject. For example, if the type is a nested object, it would include (propertyObject, nestedPredicate, nestedObject) triples.
101
- * (2) By another Type. For example, ListType calls this method to with the item variable as a subject in order to chain additional patterns on items.
102
- *
103
- * Term types with no additional properties should return an empty array.
104
- */
105
- sparqlConstructTemplateTriples({ context, variables, }: {
106
- context: "property";
124
+ * (1) When an instance of the type is an "object" of a property.
125
+ * This method should return a BGP (variables.subject, variables.predicate, variables.object) and recursively call itself with the variables.object as a "subject" context.
126
+ * (2) When an instance of the type is a "subject".
127
+ * For example, ListType calls this method to with the item variable as a subject in order to chain additional patterns on items. Term types with no additional patterns should return an empty array.
128
+ */
129
+ sparqlConstructTemplateTriples({ allowIgnoreRdfType, context, variables, }: {
130
+ allowIgnoreRdfType: boolean;
131
+ context: "object";
107
132
  variables: {
108
133
  object: string;
109
134
  predicate: string;
@@ -111,7 +136,8 @@ export declare abstract class Type {
111
136
  variablePrefix: string;
112
137
  };
113
138
  } | {
114
- context: "type";
139
+ allowIgnoreRdfType: boolean;
140
+ context: "subject";
115
141
  variables: {
116
142
  subject: string;
117
143
  variablePrefix: string;
@@ -122,8 +148,9 @@ export declare abstract class Type {
122
148
  *
123
149
  * See note in sparqlConstructTemplateTriples re: how this method is used.
124
150
  */
125
- sparqlWherePatterns({ context, variables, }: {
126
- context: "property";
151
+ sparqlWherePatterns({ allowIgnoreRdfType, context, variables, }: {
152
+ allowIgnoreRdfType: boolean;
153
+ context: "object";
127
154
  variables: {
128
155
  object: string;
129
156
  predicate: string;
@@ -131,7 +158,8 @@ export declare abstract class Type {
131
158
  variablePrefix: string;
132
159
  };
133
160
  } | {
134
- context: "type";
161
+ allowIgnoreRdfType: boolean;
162
+ context: "subject";
135
163
  variables: {
136
164
  subject: string;
137
165
  variablePrefix: string;
@@ -162,8 +190,9 @@ export declare abstract class Type {
162
190
  /**
163
191
  * Imports necessary to use this type.
164
192
  */
165
- useImports(_features: Set<TsFeature>): readonly Import[];
166
- protected rdfjsTermExpression(rdfjsTerm: Omit<BlankNode, "equals"> | Omit<Literal, "equals"> | Omit<NamedNode, "equals"> | Omit<Variable, "equals">): string;
193
+ abstract useImports(parameters: {
194
+ features: Set<TsFeature>;
195
+ }): readonly Import[];
167
196
  }
168
197
  export declare namespace Type {
169
198
  interface Conversion {
@@ -173,7 +202,36 @@ export declare namespace Type {
173
202
  }
174
203
  interface DiscriminatorProperty {
175
204
  readonly name: string;
176
- readonly values: readonly string[];
205
+ readonly ownValues: readonly string[];
206
+ readonly descendantValues: readonly string[];
207
+ }
208
+ class JsonName {
209
+ /**
210
+ * Is the type optional in JSON? Equivalent to ? in TypeScript or | undefined.
211
+ */
212
+ readonly optional: boolean;
213
+ /**
214
+ * The name of the type when it's required i.e. -- so it should never include "| undefined".
215
+ */
216
+ readonly requiredName: string;
217
+ constructor(requiredName: string, parameters?: {
218
+ optional: boolean;
219
+ });
220
+ toString(): string;
221
+ }
222
+ class GraphqlName {
223
+ /**
224
+ * Is the type nullable in GraphQL?
225
+ */
226
+ readonly nullable: boolean;
227
+ /**
228
+ * The name of the type when it's nullable -- so it should never include "new graphql.GraphQLNonNull(...)" around it.
229
+ */
230
+ readonly nullableName: string;
231
+ constructor(nullableName: string, parameters?: {
232
+ nullable: boolean;
233
+ });
234
+ toString(): string;
177
235
  }
178
236
  }
179
237
  //# sourceMappingURL=Type.d.ts.map