@shaclmate/compiler 2.0.22 → 2.0.24

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 (194) hide show
  1. package/dist/ShapesGraphToAstTransformer.d.ts +6 -5
  2. package/dist/ShapesGraphToAstTransformer.js +61 -17
  3. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.d.ts +2 -0
  4. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.js +2 -0
  5. package/dist/_ShapesGraphToAstTransformer/ShapeStack.d.ts +15 -0
  6. package/dist/_ShapesGraphToAstTransformer/ShapeStack.js +51 -0
  7. package/dist/_ShapesGraphToAstTransformer/flattenAstObjectCompositeTypeMemberTypes.d.ts +13 -0
  8. package/dist/_ShapesGraphToAstTransformer/flattenAstObjectCompositeTypeMemberTypes.js +51 -0
  9. package/dist/_ShapesGraphToAstTransformer/index.d.ts +5 -5
  10. package/dist/_ShapesGraphToAstTransformer/index.js +5 -5
  11. package/dist/_ShapesGraphToAstTransformer/shapeAstName.js +53 -12
  12. package/dist/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.d.ts +5 -0
  13. package/dist/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +130 -54
  14. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +175 -5
  15. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.d.ts +10 -0
  16. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.js +185 -0
  17. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.d.ts +10 -0
  18. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.js +32 -0
  19. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.d.ts +10 -0
  20. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.js +46 -0
  21. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.d.ts +12 -0
  22. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.js +24 -0
  23. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.d.ts +13 -0
  24. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.js +14 -0
  25. package/dist/ast/Ast.d.ts +0 -1
  26. package/dist/ast/Curie.d.ts +14 -0
  27. package/dist/ast/Curie.js +21 -0
  28. package/dist/ast/IdentifierType.d.ts +3 -2
  29. package/dist/ast/ListType.d.ts +2 -1
  30. package/dist/ast/LiteralType.d.ts +1 -1
  31. package/dist/ast/Name.d.ts +40 -5
  32. package/dist/ast/ObjectCompositeType.d.ts +11 -0
  33. package/dist/ast/ObjectType.d.ts +18 -13
  34. package/dist/ast/ObjectUnionType.d.ts +0 -11
  35. package/dist/ast/OptionType.d.ts +2 -4
  36. package/dist/ast/SetType.d.ts +2 -7
  37. package/dist/ast/TermType.d.ts +12 -4
  38. package/dist/ast/Type.d.ts +1 -1
  39. package/dist/ast/index.d.ts +1 -0
  40. package/dist/ast/index.js +1 -0
  41. package/dist/enums/TsFeature.d.ts +1 -4
  42. package/dist/enums/TsFeature.js +1 -15
  43. package/dist/generators/json/AstJsonGenerator.js +26 -4
  44. package/dist/generators/ts/BooleanType.d.ts +4 -2
  45. package/dist/generators/ts/BooleanType.js +12 -3
  46. package/dist/generators/ts/DateTimeType.d.ts +9 -8
  47. package/dist/generators/ts/DateTimeType.js +32 -8
  48. package/dist/generators/ts/DateType.d.ts +5 -2
  49. package/dist/generators/ts/DateType.js +15 -1
  50. package/dist/generators/ts/DeclaredType.d.ts +2 -2
  51. package/dist/generators/ts/DeclaredType.js +2 -2
  52. package/dist/generators/ts/FloatType.d.ts +6 -0
  53. package/dist/generators/ts/FloatType.js +18 -0
  54. package/dist/generators/ts/IdentifierType.d.ts +17 -7
  55. package/dist/generators/ts/IdentifierType.js +112 -18
  56. package/dist/generators/ts/Import.d.ts +2 -0
  57. package/dist/generators/ts/Import.js +10 -0
  58. package/dist/generators/ts/IntType.d.ts +6 -0
  59. package/dist/generators/ts/IntType.js +18 -0
  60. package/dist/generators/ts/ListType.d.ts +15 -12
  61. package/dist/generators/ts/ListType.js +113 -61
  62. package/dist/generators/ts/LiteralType.d.ts +17 -9
  63. package/dist/generators/ts/LiteralType.js +92 -17
  64. package/dist/generators/ts/NumberType.d.ts +3 -2
  65. package/dist/generators/ts/NumberType.js +8 -3
  66. package/dist/generators/ts/ObjectType.d.ts +65 -43
  67. package/dist/generators/ts/ObjectType.js +143 -123
  68. package/dist/generators/ts/ObjectUnionType.d.ts +50 -8
  69. package/dist/generators/ts/ObjectUnionType.js +303 -101
  70. package/dist/generators/ts/OptionType.d.ts +14 -10
  71. package/dist/generators/ts/OptionType.js +62 -34
  72. package/dist/generators/ts/PrimitiveType.d.ts +23 -5
  73. package/dist/generators/ts/PrimitiveType.js +34 -6
  74. package/dist/generators/ts/SetType.d.ts +17 -13
  75. package/dist/generators/ts/SetType.js +76 -44
  76. package/dist/generators/ts/SnippetDeclarations.d.ts +13 -8
  77. package/dist/generators/ts/SnippetDeclarations.js +226 -101
  78. package/dist/generators/ts/StringType.d.ts +4 -2
  79. package/dist/generators/ts/StringType.js +20 -8
  80. package/dist/generators/ts/TermType.d.ts +29 -33
  81. package/dist/generators/ts/TermType.js +89 -68
  82. package/dist/generators/ts/TsGenerator.d.ts +2 -1
  83. package/dist/generators/ts/TsGenerator.js +26 -12
  84. package/dist/generators/ts/Type.d.ts +100 -32
  85. package/dist/generators/ts/Type.js +58 -54
  86. package/dist/generators/ts/TypeFactory.d.ts +12 -5
  87. package/dist/generators/ts/TypeFactory.js +380 -226
  88. package/dist/generators/ts/UnionType.d.ts +14 -10
  89. package/dist/generators/ts/UnionType.js +319 -142
  90. package/dist/generators/ts/_ObjectType/EagerShaclProperty.d.ts +12 -0
  91. package/dist/generators/ts/_ObjectType/EagerShaclProperty.js +31 -0
  92. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.d.ts +13 -12
  93. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.js +59 -46
  94. package/dist/generators/ts/_ObjectType/IdentifierProperty.d.ts +20 -13
  95. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +198 -140
  96. package/dist/generators/ts/_ObjectType/LazyShaclProperty.d.ts +100 -0
  97. package/dist/generators/ts/_ObjectType/LazyShaclProperty.js +315 -0
  98. package/dist/generators/ts/_ObjectType/Property.d.ts +47 -46
  99. package/dist/generators/ts/_ObjectType/Property.js +1 -10
  100. package/dist/generators/ts/_ObjectType/ShaclProperty.d.ts +25 -30
  101. package/dist/generators/ts/_ObjectType/ShaclProperty.js +120 -85
  102. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +21 -17
  103. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +61 -53
  104. package/dist/generators/ts/_ObjectType/classDeclaration.js +22 -17
  105. package/dist/generators/ts/_ObjectType/createFunctionDeclaration.js +26 -13
  106. package/dist/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +8 -5
  107. package/dist/generators/ts/_ObjectType/fromRdfTypeVariableStatement.d.ts +1 -1
  108. package/dist/generators/ts/_ObjectType/fromRdfTypeVariableStatement.js +8 -5
  109. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.d.ts +5 -0
  110. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.js +49 -0
  111. package/dist/generators/ts/_ObjectType/hashFunctionOrMethodDeclarations.js +7 -8
  112. package/dist/generators/ts/_ObjectType/identifierTypeDeclarations.d.ts +7 -0
  113. package/dist/generators/ts/_ObjectType/identifierTypeDeclarations.js +54 -0
  114. package/dist/generators/ts/_ObjectType/index.d.ts +9 -7
  115. package/dist/generators/ts/_ObjectType/index.js +9 -7
  116. package/dist/generators/ts/_ObjectType/interfaceDeclaration.js +1 -1
  117. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.d.ts +4 -0
  118. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.js +189 -0
  119. package/dist/generators/ts/_ObjectType/jsonTypeAliasDeclaration.d.ts +5 -0
  120. package/dist/generators/ts/_ObjectType/jsonTypeAliasDeclaration.js +28 -0
  121. package/dist/generators/ts/_ObjectType/objectSetMethodNames.d.ts +9 -0
  122. package/dist/generators/ts/_ObjectType/objectSetMethodNames.js +18 -0
  123. package/dist/generators/ts/_ObjectType/propertiesVariableStatement.d.ts +5 -0
  124. package/dist/generators/ts/_ObjectType/propertiesVariableStatement.js +37 -0
  125. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.d.ts +4 -0
  126. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.js +144 -0
  127. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.d.ts +1 -1
  128. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +6 -5
  129. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.d.ts +1 -1
  130. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +4 -3
  131. package/dist/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +109 -37
  132. package/dist/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +6 -6
  133. package/dist/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +33 -24
  134. package/dist/generators/ts/graphqlSchemaVariableStatement.d.ts +9 -0
  135. package/dist/generators/ts/graphqlSchemaVariableStatement.js +86 -0
  136. package/dist/generators/ts/objectSetDeclarations.d.ts +8 -0
  137. package/dist/generators/ts/objectSetDeclarations.js +59 -0
  138. package/dist/generators/ts/objectSetInterfaceDeclaration.d.ts +8 -0
  139. package/dist/generators/ts/objectSetInterfaceDeclaration.js +46 -0
  140. package/dist/generators/ts/objectSetMethodSignatures.d.ts +11 -0
  141. package/dist/generators/ts/objectSetMethodSignatures.js +52 -0
  142. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.d.ts +8 -0
  143. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.js +393 -0
  144. package/dist/generators/ts/rdfjsTermExpression.d.ts +3 -0
  145. package/dist/generators/ts/rdfjsTermExpression.js +57 -0
  146. package/dist/generators/ts/sparqlObjectSetClassDeclaration.d.ts +8 -0
  147. package/dist/generators/ts/sparqlObjectSetClassDeclaration.js +415 -0
  148. package/dist/generators/ts/syntheticNamePrefix.d.ts +2 -0
  149. package/dist/generators/ts/syntheticNamePrefix.js +2 -0
  150. package/dist/generators/ts/tsName.js +27 -13
  151. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.d.ts +10 -0
  152. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.js +19 -0
  153. package/dist/input/NodeShape.d.ts +6 -8
  154. package/dist/input/NodeShape.js +20 -44
  155. package/dist/input/Ontology.d.ts +0 -3
  156. package/dist/input/Ontology.js +0 -9
  157. package/dist/input/PropertyPath.d.ts +5 -5
  158. package/dist/input/PropertyPath.js +14 -24
  159. package/dist/input/PropertyShape.d.ts +3 -1
  160. package/dist/input/PropertyShape.js +8 -2
  161. package/dist/input/ShapesGraph.js +4 -8
  162. package/dist/input/generated.d.ts +967 -107
  163. package/dist/input/generated.js +2548 -1046
  164. package/dist/input/tsFeatures.d.ts +3 -2
  165. package/dist/input/tsFeatures.js +44 -27
  166. package/package.json +18 -16
  167. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +0 -13
  168. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +0 -200
  169. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +0 -12
  170. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +0 -29
  171. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +0 -12
  172. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +0 -43
  173. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +0 -14
  174. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +0 -21
  175. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +0 -16
  176. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +0 -60
  177. package/dist/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +0 -4
  178. package/dist/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +0 -78
  179. package/dist/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +0 -4
  180. package/dist/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +0 -91
  181. package/dist/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +0 -5
  182. package/dist/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +0 -19
  183. package/dist/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +0 -5
  184. package/dist/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +0 -31
  185. package/dist/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +0 -5
  186. package/dist/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +0 -37
  187. package/dist/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +0 -6
  188. package/dist/generators/ts/_ObjectType/rdfjsTermExpression.js +0 -17
  189. package/dist/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +0 -5
  190. package/dist/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +0 -19
  191. package/dist/generators/ts/_ObjectType/toJsonReturnType.d.ts +0 -3
  192. package/dist/generators/ts/_ObjectType/toJsonReturnType.js +0 -17
  193. package/dist/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +0 -5
  194. 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);
@@ -108,31 +120,54 @@ export class TermType extends Type {
108
120
  : `((${variables.value}.termType === "${nodeKind}") ? (${valueToNodeKind}) : (${expression}))`;
109
121
  }, "");
110
122
  }
111
- fromRdfExpression({ variables, }) {
112
- const chain = [
113
- this.propertyFilterRdfResourceValuesExpression({ variables }),
114
- ];
115
- // Have an rdfjsResource.Resource.Values here
116
- if (this.hasValues.length === 1) {
117
- chain.push(`find(_value => _value.toTerm().equals(${this.rdfjsTermExpression(this.hasValues[0])}))`);
118
- }
119
- else {
120
- chain.push("head()");
123
+ fromRdfExpression(parameters) {
124
+ // invariant(
125
+ // this.nodeKinds.has("Literal") &&
126
+ // (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")),
127
+ // "IdentifierType and LiteralType should override",
128
+ // );
129
+ const chain = this.fromRdfExpressionChain(parameters);
130
+ const { variables } = parameters;
131
+ return [
132
+ variables.resourceValues,
133
+ chain.defaultValue,
134
+ chain.hasValues,
135
+ chain.languageIn,
136
+ chain.valueTo,
137
+ ]
138
+ .filter((_) => typeof _ !== "undefined")
139
+ .join(".");
140
+ }
141
+ fromRdfExpressionChain({ variables, }) {
142
+ let valueToExpression = "purify.Either.of<Error, rdfjs.BlankNode | rdfjs.Literal | rdfjs.NamedNode>(value.toTerm())";
143
+ if (this.nodeKinds.size < 3) {
144
+ const eitherTypeParameters = `<Error, ${this.name}>`;
145
+ valueToExpression = `${valueToExpression}.chain(term => {
146
+ switch (term.termType) {
147
+ ${[...this.nodeKinds].map((nodeKind) => `case "${nodeKind}":`).join("\n")} return purify.Either.of${eitherTypeParameters}(term);
148
+ default: return purify.Left${eitherTypeParameters}(new rdfjsResource.Resource.MistypedValueError(${objectInitializer({ actualValue: "term", expectedValueType: JSON.stringify(this.name), focusResource: variables.resource, predicate: variables.predicate })}));
149
+ }})`;
121
150
  }
122
- // Have an rdfjsResource.Resource.Value here
123
- this.defaultValue.ifJust((defaultValue) => {
124
- // 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) })})))`);
126
- });
127
- // Last step: convert the rdfjsResource.Resource.Value to the type
128
- chain.push(`chain(_value => ${this.propertyFromRdfResourceValueExpression({
129
- variables: {
130
- predicate: variables.predicate,
131
- resource: variables.resource,
132
- resourceValue: "_value",
133
- },
134
- })})`);
135
- return chain.join(".");
151
+ return {
152
+ defaultValue: this.defaultValue
153
+ .map((defaultValue) => `map(values => values.length > 0 ? values : new rdfjsResource.Resource.Value(${objectInitializer({ subject: variables.resource, predicate: variables.predicate, object: rdfjsTermExpression(defaultValue) })}).toValues())`)
154
+ .extract(),
155
+ hasValues: this.hasValues.length > 0
156
+ ? `chain(values => {
157
+ for (const hasValue of [${this.hasValues.map(rdfjsTermExpression).join(", ")}]) {
158
+ const findResult = values.find(value => value.toTerm().equals(hasValue));
159
+ if (findResult.isLeft()) {
160
+ return findResult;
161
+ }
162
+ }
163
+ return purify.Either.of<Error, rdfjsResource.Resource.Values<rdfjsResource.Resource.Value>>(values);
164
+ })`
165
+ : undefined,
166
+ valueTo: `chain(values => values.chainMap(value => ${valueToExpression}))`,
167
+ };
168
+ }
169
+ graphqlResolveExpression(_parameters) {
170
+ throw new Error("not implemented");
136
171
  }
137
172
  hashStatements({ variables, }) {
138
173
  return [
@@ -140,6 +175,9 @@ export class TermType extends Type {
140
175
  `${variables.hasher}.update(${variables.value}.value);`,
141
176
  ];
142
177
  }
178
+ jsonUiSchemaElement() {
179
+ return Maybe.empty();
180
+ }
143
181
  jsonZodSchema({ variables, }) {
144
182
  invariant(this.nodeKinds.has("Literal") &&
145
183
  (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
@@ -159,7 +197,7 @@ export class TermType extends Type {
159
197
  })
160
198
  .join(", ")}])`;
161
199
  }
162
- snippetDeclarations(features) {
200
+ snippetDeclarations({ features, }) {
163
201
  const snippetDeclarations = [];
164
202
  if (features.has("equals")) {
165
203
  snippetDeclarations.push(SnippetDeclarations.booleanEquals);
@@ -168,13 +206,13 @@ export class TermType extends Type {
168
206
  }
169
207
  sparqlWherePatterns(parameters) {
170
208
  switch (parameters.context) {
171
- case "property":
209
+ case "object":
172
210
  return this.defaultValue
173
211
  .map(() => [
174
212
  `{ patterns: [${super.sparqlWherePatterns(parameters).join(", ")}], type: "optional" }`,
175
213
  ])
176
214
  .orDefault(super.sparqlWherePatterns(parameters));
177
- case "type":
215
+ case "subject":
178
216
  return super.sparqlWherePatterns(parameters);
179
217
  }
180
218
  }
@@ -203,7 +241,7 @@ export class TermType extends Type {
203
241
  }
204
242
  toRdfExpression({ variables, }) {
205
243
  return this.defaultValue
206
- .map((defaultValue) => `!${variables.value}.equals(${this.rdfjsTermExpression(defaultValue)}) ? ${variables.value} : undefined`)
244
+ .map((defaultValue) => `!${variables.value}.equals(${rdfjsTermExpression(defaultValue)}) ? ${variables.value} : undefined`)
207
245
  .orDefault(variables.value);
208
246
  }
209
247
  useImports() {
@@ -213,33 +251,16 @@ export class TermType extends Type {
213
251
  }
214
252
  return imports;
215
253
  }
216
- /**
217
- * Filter the rdfjsResource.Resource.Values to those that are relevant to the type.
218
- *
219
- * This is done before
220
- */
221
- propertyFilterRdfResourceValuesExpression({ variables, }) {
222
- return variables.resourceValues;
223
- }
224
- /**
225
- * Convert an rdfjsResource.Resource.Value to a value of this type.
226
- * @param variables
227
- * @protected
228
- */
229
- propertyFromRdfResourceValueExpression({ variables, }) {
230
- invariant(this.nodeKinds.has("Literal") &&
231
- (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
232
- let expression = `purify.Either.of(${variables.resourceValue}.toTerm())`;
233
- if (this.nodeKinds.size < 3) {
234
- expression = `${expression}.chain(term => {
235
- 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 })}));
238
- }})`;
239
- }
240
- return expression;
241
- }
242
254
  }
255
+ __decorate([
256
+ Memoize()
257
+ ], TermType.prototype, "conversions", null);
258
+ __decorate([
259
+ Memoize()
260
+ ], TermType.prototype, "discriminatorProperty", null);
261
+ __decorate([
262
+ Memoize()
263
+ ], TermType.prototype, "jsonName", null);
243
264
  __decorate([
244
265
  Memoize()
245
266
  ], 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,12 @@ 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([
32
+ 'import { DataFactory as dataFactory, StoreFactory as _DatasetFactory } from "n3";',
33
+ "const datasetFactory: rdfjs.DatasetCoreFactory = new _DatasetFactory();",
34
+ ]);
33
35
  const declaredTypes = [
34
36
  ...objectTypes,
35
37
  ...objectUnionTypes,
@@ -58,20 +60,32 @@ export class TsGenerator {
58
60
  // Deduplicate and add snippet declarations
59
61
  const addedSnippetDeclarations = new Set();
60
62
  for (const declaredType of declaredTypes) {
61
- for (const snippetDeclaration of declaredType.snippetDeclarations(declaredType.features)) {
63
+ for (const snippetDeclaration of declaredType.snippetDeclarations({
64
+ features: declaredType.features,
65
+ recursionStack: [],
66
+ })) {
62
67
  if (!addedSnippetDeclarations.has(snippetDeclaration)) {
63
68
  sourceFile.addStatements([snippetDeclaration]);
64
69
  addedSnippetDeclarations.add(snippetDeclaration);
65
70
  }
66
71
  }
67
72
  }
68
- // Add type declarations
69
73
  for (const objectType of objectTypes) {
70
74
  sourceFile.addStatements(objectType.declarations);
71
75
  }
72
76
  for (const objectUnionType of objectUnionTypes) {
73
77
  sourceFile.addStatements(objectUnionType.declarations);
74
78
  }
79
+ const objectTypesSortedByName = objectTypes.toSorted((left, right) => left.name.localeCompare(right.name));
80
+ const objectUnionTypesSortedByName = objectUnionTypes.toSorted((left, right) => left.name.localeCompare(right.name));
81
+ sourceFile.addStatements(objectSetDeclarations({
82
+ objectTypes: objectTypesSortedByName,
83
+ objectUnionTypes: objectUnionTypesSortedByName,
84
+ }));
85
+ sourceFile.addVariableStatements(graphqlSchemaVariableStatement({
86
+ objectTypes: objectTypesSortedByName,
87
+ objectUnionTypes: objectUnionTypesSortedByName,
88
+ }).toList());
75
89
  }
76
90
  }
77
91
  //# 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.
22
25
  */
23
- abstract readonly jsonName: string;
26
+ abstract readonly graphqlName: Type.GraphqlName;
27
+ /**
28
+ * JSON-compatible version of the type.
29
+ */
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,36 @@ 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
50
- * of this type for a property.
53
+ * An expression that converts a purify.Either<Error, rdfjsResource.Resource.Values<rdfjsResource.Resource.Value>> to
54
+ * (1) a purify.Either<Error, rdfjsResource.Resource.Values<this type>> if this is an item type (identifier, object, et al.) or
55
+ * (2) a purify.Either<Error, cardinality type> if this is a cardinality type
56
+ *
57
+ * Some types need to filter on the set of all objects/values of a (subject, predicate). For example, all sh:hasValue values must be present in the set for any values
58
+ * to be considered valid. Similar
59
+ *
60
+ * Values may also need to be sorted. For example, applying sh:languageIn should sort the values in the order of the specified languages so that the first value
61
+ * (if it exists) is always of the first specified language -- a common situation when sh:maxCount is 1.
51
62
  */
52
63
  abstract fromRdfExpression(parameters: {
53
64
  variables: {
54
65
  context: string;
66
+ ignoreRdfType?: boolean;
55
67
  languageIn: string;
68
+ objectSet: string;
56
69
  predicate: string;
57
70
  resource: string;
58
71
  resourceValues: string;
59
72
  };
60
73
  }): string;
74
+ /**
75
+ * An expression that resolves a value of this type in the GraphQL server.
76
+ */
77
+ abstract graphqlResolveExpression(parameters: {
78
+ variables: {
79
+ args: string;
80
+ value: string;
81
+ };
82
+ }): string;
61
83
  /**
62
84
  * Statements that use hasher.update to hash a property value of this type.
63
85
  */
@@ -71,15 +93,22 @@ export declare abstract class Type {
71
93
  /**
72
94
  * Element object for a JSON Forms UI schema.
73
95
  */
74
- jsonUiSchemaElement(_parameters: {
96
+ abstract jsonUiSchemaElement(parameters: {
75
97
  variables: {
76
98
  scopePrefix: string;
77
99
  };
78
100
  }): Maybe<string>;
79
101
  /**
80
- * Zod schema for the JSON version of the type (the result of propertyToJson).
102
+ * Zod schema for the JSON type of this type.
103
+ *
104
+ * This method is called in two contexts:
105
+ * "property": from a ShaclProperty, while generating the z.object properties of an ObjectType
106
+ * "type": from another Type e.g., an OptionType or UnionType
107
+ *
108
+ * z.lazy() should only be returned for "property".
81
109
  */
82
110
  abstract jsonZodSchema(parameters: {
111
+ context: "property" | "type";
83
112
  variables: {
84
113
  zod: string;
85
114
  };
@@ -92,18 +121,22 @@ export declare abstract class Type {
92
121
  *
93
122
  * The generator deduplicates snippet declarations across all types before adding them to the source.
94
123
  */
95
- snippetDeclarations(_features: Set<TsFeature>): readonly string[];
124
+ abstract snippetDeclarations(parameters: {
125
+ features: Set<TsFeature>;
126
+ recursionStack: Type[];
127
+ }): readonly string[];
96
128
  /**
97
129
  * An array of SPARQL.js CONSTRUCT template triples for a value of this type, as strings (so they can incorporate runtime calls).
98
130
  *
99
131
  * 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";
132
+ * (1) When an instance of the type is an "object" of a property.
133
+ * This method should return a BGP (variables.subject, variables.predicate, variables.object) and recursively call itself with the variables.object as a "subject" context.
134
+ * (2) When an instance of the type is a "subject".
135
+ * 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.
136
+ */
137
+ sparqlConstructTemplateTriples({ allowIgnoreRdfType, context, variables, }: {
138
+ allowIgnoreRdfType: boolean;
139
+ context: "object";
107
140
  variables: {
108
141
  object: string;
109
142
  predicate: string;
@@ -111,7 +144,8 @@ export declare abstract class Type {
111
144
  variablePrefix: string;
112
145
  };
113
146
  } | {
114
- context: "type";
147
+ allowIgnoreRdfType: boolean;
148
+ context: "subject";
115
149
  variables: {
116
150
  subject: string;
117
151
  variablePrefix: string;
@@ -122,17 +156,21 @@ export declare abstract class Type {
122
156
  *
123
157
  * See note in sparqlConstructTemplateTriples re: how this method is used.
124
158
  */
125
- sparqlWherePatterns({ context, variables, }: {
126
- context: "property";
159
+ sparqlWherePatterns({ allowIgnoreRdfType, context, variables, }: {
160
+ allowIgnoreRdfType: boolean;
161
+ context: "object";
127
162
  variables: {
163
+ languageIn: string;
128
164
  object: string;
129
165
  predicate: string;
130
166
  subject: string;
131
167
  variablePrefix: string;
132
168
  };
133
169
  } | {
134
- context: "type";
170
+ allowIgnoreRdfType: boolean;
171
+ context: "subject";
135
172
  variables: {
173
+ languageIn: string;
136
174
  subject: string;
137
175
  variablePrefix: string;
138
176
  };
@@ -162,8 +200,9 @@ export declare abstract class Type {
162
200
  /**
163
201
  * Imports necessary to use this type.
164
202
  */
165
- useImports(_features: Set<TsFeature>): readonly Import[];
166
- protected rdfjsTermExpression(rdfjsTerm: Omit<BlankNode, "equals"> | Omit<Literal, "equals"> | Omit<NamedNode, "equals"> | Omit<Variable, "equals">): string;
203
+ abstract useImports(parameters: {
204
+ features: Set<TsFeature>;
205
+ }): readonly Import[];
167
206
  }
168
207
  export declare namespace Type {
169
208
  interface Conversion {
@@ -173,7 +212,36 @@ export declare namespace Type {
173
212
  }
174
213
  interface DiscriminatorProperty {
175
214
  readonly name: string;
176
- readonly values: readonly string[];
215
+ readonly ownValues: readonly string[];
216
+ readonly descendantValues: readonly string[];
217
+ }
218
+ class JsonName {
219
+ /**
220
+ * Is the type optional in JSON? Equivalent to ? in TypeScript or | undefined.
221
+ */
222
+ readonly optional: boolean;
223
+ /**
224
+ * The name of the type when it's required i.e. -- so it should never include "| undefined".
225
+ */
226
+ readonly requiredName: string;
227
+ constructor(requiredName: string, parameters?: {
228
+ optional: boolean;
229
+ });
230
+ toString(): string;
231
+ }
232
+ class GraphqlName {
233
+ /**
234
+ * Is the type nullable in GraphQL?
235
+ */
236
+ readonly nullable: boolean;
237
+ /**
238
+ * The name of the type when it's nullable -- so it should never include "new graphql.GraphQLNonNull(...)" around it.
239
+ */
240
+ readonly nullableName: string;
241
+ constructor(nullableName: string, parameters?: {
242
+ nullable: boolean;
243
+ });
244
+ toString(): string;
177
245
  }
178
246
  }
179
247
  //# sourceMappingURL=Type.d.ts.map