@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
@@ -1,83 +1,117 @@
1
1
  import { Maybe } from "purify-ts";
2
+ import { invariant } from "ts-invariant";
3
+ import { rdfjsTermExpression } from "./_ObjectType/rdfjsTermExpression.js";
4
+ import { objectInitializer } from "./objectInitializer.js";
5
+ /**
6
+ * Abstract base class for generating TypeScript expressions and statemenst in the TypeScript generator.
7
+ *
8
+ * Subclasses are used for both property types (c.f., property* methods) and node/object types.
9
+ */
2
10
  export class Type {
3
- constructor({ configuration, }) {
4
- this.configuration = configuration;
11
+ constructor({ dataFactoryVariable, }) {
12
+ this.dataFactoryVariable = dataFactoryVariable;
5
13
  }
6
14
  /**
7
- * Expressions that convert a source type or types to this type. It should include the type itself.
15
+ * A property that discriminates sub-types of this type e.g., termType on RDF/JS terms.
8
16
  */
9
- get conversions() {
10
- return [
11
- {
12
- conversionExpression: (value) => value,
13
- sourceTypeName: this.name,
14
- },
15
- ];
17
+ get discriminatorProperty() {
18
+ return Maybe.empty();
16
19
  }
17
20
  /**
18
- * A property that discriminates sub-types of this type e.g., termType on RDF/JS terms.
21
+ * Element object for a JSON Forms UI schema.
19
22
  */
20
- get discriminatorProperty() {
23
+ jsonUiSchemaElement(_parameters) {
21
24
  return Maybe.empty();
22
25
  }
23
26
  /**
24
- * Imports used by other methods on this type.
27
+ * Reusable function, type, and other declarations that are not particular to this type but that type-specific code
28
+ * relies on. For example, the equals function/method of ObjectType has a custom return type that's the same across all
29
+ * ObjectType's. Instead of re-declaring the return type anonymously on every equals function, declare a named type
30
+ * as a snippet and reference it.
31
+ *
32
+ * The generator deduplicates snippet declarations across all types before adding them to the source.
25
33
  */
26
- get importStatements() {
34
+ snippetDeclarations(_features) {
27
35
  return [];
28
36
  }
29
37
  /**
30
- * An optional sparqlBuilder.GraphPattern expression that's chained to the object of another pattern, such as a list item.
38
+ * An array of SPARQL.js CONSTRUCT template triples for a value of this type, as strings (so they can incorporate runtime calls).
39
+ *
40
+ * This method is called in two contexts:
41
+ * (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.
42
+ * (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.
31
43
  *
32
- * If the type is e.g., an RDF/JS term it won't have additional graph patterns beyond the basic (s, p, o), and this
33
- * method will return nothing.
44
+ * Term types with no additional properties should return an empty array.
34
45
  */
35
- chainSparqlGraphPatternExpression(_) {
36
- return Maybe.empty();
46
+ sparqlConstructTemplateTriples({ context, variables, }) {
47
+ switch (context) {
48
+ case "property": {
49
+ const objectPrefix = `${this.dataFactoryVariable}.variable!(`;
50
+ const objectSuffix = ")";
51
+ invariant(variables.object.startsWith(objectPrefix));
52
+ invariant(variables.object.endsWith(objectSuffix));
53
+ return [
54
+ objectInitializer({
55
+ object: variables.object,
56
+ predicate: variables.predicate,
57
+ subject: variables.subject,
58
+ }),
59
+ ].concat(this.sparqlConstructTemplateTriples({
60
+ context: "type",
61
+ variables: {
62
+ subject: variables.object,
63
+ variablePrefix: variables.object.substring(objectPrefix.length, variables.object.length - objectSuffix.length),
64
+ },
65
+ }));
66
+ }
67
+ case "type":
68
+ return [];
69
+ }
37
70
  }
38
71
  /**
39
- * An sparqlBuilder.GraphPattern expression for a property, typically building a basic graph pattern.
72
+ * An array of SPARQL.js where patterns for a value of this type, as strings (so they can incorporate runtime calls).
73
+ *
74
+ * See note in sparqlConstructTemplateTriples re: how this method is used.
40
75
  */
41
- propertySparqlGraphPatternExpression({ variables, }) {
42
- let expression = `sparqlBuilder.GraphPattern.basic(${variables.subject}, ${variables.predicate}, ${variables.object})`;
43
- this.chainSparqlGraphPatternExpression({
44
- variables: { subject: "object" },
45
- }).ifJust((chainSparqlGraphPatternExpression) => {
46
- expression = `sparqlBuilder.GraphPattern.group(${expression}.chainObject(object => ${chainSparqlGraphPatternExpression.toSparqlGraphPatternsExpression()}))`;
47
- });
48
- return new Type.SparqlGraphPatternExpression(expression);
49
- }
50
- }
51
- (function (Type) {
52
- class SparqlGraphPatternExpression {
53
- constructor(value) {
54
- this.value = value;
55
- }
56
- toSparqlGraphPatternExpression() {
57
- return this;
58
- }
59
- toSparqlGraphPatternsExpression() {
60
- return new SparqlGraphPatternExpression(`[${this.value}]`);
61
- }
62
- toString() {
63
- return this.value;
76
+ sparqlWherePatterns({ context, variables, }) {
77
+ switch (context) {
78
+ case "property": {
79
+ const objectPrefix = `${this.dataFactoryVariable}.variable!(`;
80
+ const objectSuffix = ")";
81
+ invariant(variables.object.startsWith(objectPrefix));
82
+ invariant(variables.object.endsWith(objectSuffix));
83
+ return [
84
+ objectInitializer({
85
+ triples: `[${objectInitializer({
86
+ object: variables.object,
87
+ predicate: variables.predicate,
88
+ subject: variables.subject,
89
+ })}]`,
90
+ type: '"bgp"',
91
+ }),
92
+ ].concat(this.sparqlWherePatterns({
93
+ context: "type",
94
+ variables: {
95
+ subject: variables.object,
96
+ variablePrefix: variables.object.substring(objectPrefix.length, variables.object.length - objectSuffix.length),
97
+ },
98
+ }));
99
+ }
100
+ case "type":
101
+ return [];
64
102
  }
65
103
  }
66
- Type.SparqlGraphPatternExpression = SparqlGraphPatternExpression;
67
- class SparqlGraphPatternsExpression {
68
- constructor(value) {
69
- this.value = value;
70
- }
71
- toSparqlGraphPatternExpression() {
72
- return new SparqlGraphPatternExpression(`sparqlBuilder.GraphPattern.group(${this.value})`);
73
- }
74
- toSparqlGraphPatternsExpression() {
75
- return this;
76
- }
77
- toString() {
78
- return this.value;
79
- }
104
+ /**
105
+ * Imports necessary to use this type.
106
+ */
107
+ useImports(_features) {
108
+ return [];
80
109
  }
81
- Type.SparqlGraphPatternsExpression = SparqlGraphPatternsExpression;
82
- })(Type || (Type = {}));
110
+ rdfjsTermExpression(rdfjsTerm) {
111
+ return rdfjsTermExpression({
112
+ dataFactoryVariable: this.dataFactoryVariable,
113
+ rdfjsTerm,
114
+ });
115
+ }
116
+ }
83
117
  //# sourceMappingURL=Type.js.map
@@ -1,12 +1,11 @@
1
- import type * as ast from "../../ast";
2
- import type { Configuration } from "./Configuration";
1
+ import type * as ast from "../../ast/index.js";
3
2
  import type { Type } from "./Type.js";
4
3
  export declare class TypeFactory {
5
4
  private cachedObjectTypePropertiesByIdentifier;
6
5
  private cachedObjectTypesByIdentifier;
7
- private readonly configuration;
8
- constructor({ configuration }: {
9
- configuration: Configuration;
6
+ private readonly dataFactoryVariable;
7
+ constructor({ dataFactoryVariable }: {
8
+ dataFactoryVariable: string;
10
9
  });
11
10
  createTypeFromAstType(astType: ast.Type): Type;
12
11
  private createObjectTypeFromAstType;
@@ -1,101 +1,204 @@
1
1
  import TermMap from "@rdfjs/term-map";
2
- import { NodeKind } from "@shaclmate/shacl-ast";
3
- import { xsd } from "@tpluscode/rdf-ns-builders";
2
+ import TermSet from "@rdfjs/term-set";
3
+ import { rdf, xsd } from "@tpluscode/rdf-ns-builders";
4
4
  import { Maybe } from "purify-ts";
5
- import { BooleanType } from "./BooleanType";
6
- import { IdentifierType } from "./IdentifierType";
7
- import { IntersectionType } from "./IntersectionType";
5
+ import { fromRdf } from "rdf-literal";
6
+ import { logger } from "../../logger.js";
7
+ import { BooleanType } from "./BooleanType.js";
8
+ import { DateTimeType } from "./DateTimeType.js";
9
+ import { IdentifierType } from "./IdentifierType.js";
8
10
  import { ListType } from "./ListType.js";
9
11
  import { LiteralType } from "./LiteralType.js";
10
12
  import { NumberType } from "./NumberType.js";
11
13
  import { ObjectType } from "./ObjectType.js";
12
- import { OptionType } from "./OptionType";
13
- import { SetType } from "./SetType";
14
+ import { ObjectUnionType } from "./ObjectUnionType.js";
15
+ import { OptionType } from "./OptionType.js";
16
+ import { SetType } from "./SetType.js";
14
17
  import { StringType } from "./StringType.js";
15
- import { UnionType } from "./UnionType";
18
+ import { TermType } from "./TermType.js";
19
+ import { UnionType } from "./UnionType.js";
16
20
  import { tsName } from "./tsName.js";
17
21
  export class TypeFactory {
18
- constructor({ configuration }) {
22
+ constructor({ dataFactoryVariable }) {
19
23
  this.cachedObjectTypePropertiesByIdentifier = new TermMap();
20
24
  this.cachedObjectTypesByIdentifier = new TermMap();
21
- this.configuration = configuration;
25
+ this.dataFactoryVariable = dataFactoryVariable;
22
26
  }
23
27
  createTypeFromAstType(astType) {
24
28
  switch (astType.kind) {
25
29
  case "IdentifierType":
26
30
  return new IdentifierType({
27
- configuration: this.configuration,
31
+ dataFactoryVariable: this.dataFactoryVariable,
28
32
  defaultValue: astType.defaultValue,
29
- hasValue: astType.hasValue,
33
+ hasValues: astType.hasValues,
34
+ in_: astType.in_,
30
35
  nodeKinds: astType.nodeKinds,
31
36
  });
32
37
  case "IntersectionType":
33
- return new IntersectionType({
34
- configuration: this.configuration,
35
- memberTypes: astType.memberTypes.map((astType) => this.createTypeFromAstType(astType)),
38
+ throw new Error("not implemented");
39
+ case "ListType": {
40
+ return new ListType({
41
+ dataFactoryVariable: this.dataFactoryVariable,
42
+ identifierNodeKind: astType.identifierNodeKind,
43
+ itemType: this.createTypeFromAstType(astType.itemType),
44
+ mutable: astType.mutable.orDefault(false),
45
+ mintingStrategy: astType.mintingStrategy,
46
+ toRdfTypes: astType.toRdfTypes,
36
47
  });
48
+ }
37
49
  case "LiteralType": {
38
- const datatype = astType.datatype
39
- .altLazy(() => astType.defaultValue.map((defaultValue) => defaultValue.datatype))
40
- .altLazy(() => astType.hasValue.map((hasValue) => hasValue.datatype))
41
- .extractNullable();
42
- if (datatype !== null) {
50
+ // Look at sh:datatype as well as sh:defaultValue/sh:hasValue/sh:in term datatypes
51
+ // If there's one common datatype than we can refine the type
52
+ // Otherwise default to rdfjs.Literal
53
+ const datatypes = new TermSet();
54
+ astType.datatype.ifJust((datatype) => datatypes.add(datatype));
55
+ astType.defaultValue.ifJust((defaultValue) => datatypes.add(defaultValue.datatype));
56
+ for (const hasValue of astType.hasValues) {
57
+ datatypes.add(hasValue.datatype);
58
+ }
59
+ for (const value of astType.in_) {
60
+ datatypes.add(value.datatype);
61
+ }
62
+ if (datatypes.size === 1) {
63
+ const datatype = [...datatypes][0];
43
64
  if (datatype.equals(xsd.boolean)) {
44
65
  return new BooleanType({
45
- configuration: this.configuration,
66
+ dataFactoryVariable: this.dataFactoryVariable,
46
67
  defaultValue: astType.defaultValue,
47
- hasValue: astType.hasValue,
68
+ hasValues: astType.hasValues,
69
+ languageIn: [],
70
+ in_: astType.in_,
71
+ primitiveDefaultValue: astType.defaultValue
72
+ .map((value) => fromRdf(value, true))
73
+ .filter((value) => typeof value === "boolean"),
74
+ primitiveIn: astType.in_
75
+ .map((value) => fromRdf(value, true))
76
+ .filter((value) => typeof value === "boolean"),
48
77
  });
49
78
  }
50
- if (datatype.equals(xsd.integer)) {
51
- return new NumberType({
52
- configuration: this.configuration,
79
+ if (datatype.equals(xsd.dateTime)) {
80
+ return new DateTimeType({
81
+ dataFactoryVariable: this.dataFactoryVariable,
53
82
  defaultValue: astType.defaultValue,
54
- hasValue: astType.hasValue,
83
+ hasValues: astType.hasValues,
84
+ in_: astType.in_,
85
+ languageIn: [],
86
+ primitiveDefaultValue: astType.defaultValue
87
+ .map((value) => fromRdf(value, true))
88
+ .filter((value) => typeof value === "object" && value instanceof Date),
89
+ primitiveIn: astType.in_
90
+ .map((value) => fromRdf(value, true))
91
+ .filter((value) => typeof value === "object" && value instanceof Date),
55
92
  });
56
93
  }
94
+ for (const numberDatatype of [
95
+ // Integers
96
+ xsd.byte,
97
+ xsd.int,
98
+ xsd.integer,
99
+ xsd.long,
100
+ xsd.negativeInteger,
101
+ xsd.nonNegativeInteger,
102
+ xsd.nonPositiveInteger,
103
+ xsd.positiveInteger,
104
+ xsd.short,
105
+ xsd.unsignedByte,
106
+ xsd.unsignedInt,
107
+ xsd.unsignedLong,
108
+ xsd.unsignedShort,
109
+ // Floating point
110
+ xsd.decimal,
111
+ xsd.double,
112
+ xsd.float,
113
+ ]) {
114
+ if (datatype.equals(numberDatatype)) {
115
+ return new NumberType({
116
+ dataFactoryVariable: this.dataFactoryVariable,
117
+ defaultValue: astType.defaultValue,
118
+ hasValues: astType.hasValues,
119
+ in_: astType.in_,
120
+ languageIn: [],
121
+ primitiveDefaultValue: astType.defaultValue
122
+ .map((value) => fromRdf(value, true))
123
+ .filter((value) => typeof value === "number"),
124
+ primitiveIn: astType.in_
125
+ .map((value) => fromRdf(value, true))
126
+ .filter((value) => typeof value === "number"),
127
+ });
128
+ }
129
+ }
57
130
  if (datatype.equals(xsd.anyURI) || datatype.equals(xsd.string)) {
58
131
  return new StringType({
59
- configuration: this.configuration,
132
+ dataFactoryVariable: this.dataFactoryVariable,
60
133
  defaultValue: astType.defaultValue,
61
- hasValue: astType.hasValue,
134
+ hasValues: astType.hasValues,
135
+ languageIn: [],
136
+ in_: astType.in_,
137
+ primitiveDefaultValue: astType.defaultValue.map((value) => value.value),
138
+ primitiveIn: astType.in_.map((value) => value.value),
62
139
  });
63
140
  }
141
+ if (datatype.equals(rdf.langString)) {
142
+ // Drop down
143
+ }
144
+ else {
145
+ logger.warn("unrecognized literal datatype: %s", datatype.value);
146
+ }
147
+ }
148
+ else if (datatypes.size > 0) {
149
+ logger.warn("literal type has multiple datatypes: %s", JSON.stringify([...datatypes].map((datatype) => datatype.value)));
150
+ }
151
+ else {
152
+ logger.debug("literal type has no datatypes");
64
153
  }
65
154
  return new LiteralType({
66
- configuration: this.configuration,
155
+ dataFactoryVariable: this.dataFactoryVariable,
67
156
  defaultValue: astType.defaultValue,
68
- hasValue: astType.hasValue,
157
+ hasValues: astType.hasValues,
158
+ in_: astType.in_,
159
+ languageIn: astType.languageIn,
69
160
  });
70
161
  }
71
- case "ObjectType": {
72
- if (astType.listItemType.isJust()) {
73
- return new ListType({
74
- configuration: this.configuration,
75
- identifierNodeKind: astType.nodeKinds.has(NodeKind.BLANK_NODE)
76
- ? NodeKind.BLANK_NODE
77
- : NodeKind.IRI,
78
- itemType: this.createTypeFromAstType(astType.listItemType.unsafeCoerce()),
79
- mintingStrategy: astType.mintingStrategy,
80
- rdfType: astType.rdfType,
81
- });
82
- }
162
+ case "ObjectIntersectionType":
163
+ throw new Error("not implemented");
164
+ case "ObjectType":
83
165
  return this.createObjectTypeFromAstType(astType);
84
- }
166
+ case "ObjectUnionType":
167
+ return new ObjectUnionType({
168
+ comment: astType.comment,
169
+ dataFactoryVariable: this.dataFactoryVariable,
170
+ export_: astType.export,
171
+ features: astType.tsFeatures,
172
+ label: astType.label,
173
+ memberTypes: astType.memberTypes
174
+ .map((astType) => this.createTypeFromAstType(astType))
175
+ .filter((memberType) => memberType instanceof ObjectType),
176
+ name: tsName(astType.name),
177
+ });
85
178
  case "OptionType":
86
179
  return new OptionType({
87
- configuration: this.configuration,
180
+ dataFactoryVariable: this.dataFactoryVariable,
88
181
  itemType: this.createTypeFromAstType(astType.itemType),
89
182
  });
183
+ case "PlaceholderType":
184
+ throw new Error(astType.kind);
90
185
  case "SetType":
91
186
  return new SetType({
92
- configuration: this.configuration,
187
+ dataFactoryVariable: this.dataFactoryVariable,
93
188
  itemType: this.createTypeFromAstType(astType.itemType),
94
189
  minCount: astType.minCount,
95
190
  });
191
+ case "TermType":
192
+ return new TermType({
193
+ dataFactoryVariable: this.dataFactoryVariable,
194
+ defaultValue: astType.defaultValue,
195
+ hasValues: astType.hasValues,
196
+ in_: astType.in_,
197
+ nodeKinds: astType.nodeKinds,
198
+ });
96
199
  case "UnionType":
97
200
  return new UnionType({
98
- configuration: this.configuration,
201
+ dataFactoryVariable: this.dataFactoryVariable,
99
202
  memberTypes: astType.memberTypes.map((astType) => this.createTypeFromAstType(astType)),
100
203
  });
101
204
  }
@@ -108,29 +211,60 @@ export class TypeFactory {
108
211
  }
109
212
  }
110
213
  const identifierType = new IdentifierType({
111
- configuration: this.configuration,
214
+ dataFactoryVariable: this.dataFactoryVariable,
112
215
  defaultValue: Maybe.empty(),
113
- hasValue: Maybe.empty(),
216
+ hasValues: [],
217
+ in_: [],
114
218
  nodeKinds: astType.nodeKinds,
115
219
  });
116
220
  const objectType = new ObjectType({
117
221
  abstract: astType.abstract,
118
- configuration: this.configuration,
222
+ comment: astType.comment,
223
+ dataFactoryVariable: this.dataFactoryVariable,
224
+ declarationType: astType.tsObjectDeclarationType,
119
225
  export_: astType.export,
120
- identifierType,
226
+ extern: astType.extern,
227
+ features: astType.tsFeatures,
228
+ fromRdfType: astType.fromRdfType,
229
+ imports: astType.tsImports,
230
+ label: astType.label,
121
231
  lazyAncestorObjectTypes: () => astType.ancestorObjectTypes.map((astType) => this.createObjectTypeFromAstType(astType)),
122
232
  lazyDescendantObjectTypes: () => astType.descendantObjectTypes.map((astType) => this.createObjectTypeFromAstType(astType)),
123
233
  lazyParentObjectTypes: () => astType.parentObjectTypes.map((astType) => this.createObjectTypeFromAstType(astType)),
124
234
  lazyProperties: () => {
125
- const properties = astType.properties.map((astProperty) => this.createObjectTypePropertyFromAstProperty(astProperty));
126
- if (astType.parentObjectTypes.length === 0) {
127
- properties.push(new ObjectType.IdentifierProperty({
128
- configuration: this.configuration,
129
- mintingStrategy: astType.mintingStrategy,
130
- name: this.configuration.objectTypeIdentifierPropertyName,
131
- type: identifierType,
132
- }));
133
- } // Else parent will have the identifier property
235
+ const properties = astType.properties.map((astProperty) => this.createObjectTypePropertyFromAstProperty(astType, astProperty));
236
+ let identifierPropertyClassDeclarationVisibility;
237
+ if (astType.abstract) {
238
+ // If the type is abstract, don't declare a property.
239
+ identifierPropertyClassDeclarationVisibility = Maybe.empty();
240
+ }
241
+ else if (astType.ancestorObjectTypes.some((ancestorObjectType) => !ancestorObjectType.abstract)) {
242
+ // If the type has a non-abstract ancestor, that ancestor will declare the identifier property
243
+ identifierPropertyClassDeclarationVisibility = Maybe.empty();
244
+ }
245
+ else if (astType.descendantObjectTypes.some((descendantObjectType) => !descendantObjectType.abstract)) {
246
+ // If the type has a non-abstract descendant, declare the identifier property for it
247
+ identifierPropertyClassDeclarationVisibility = Maybe.of("protected");
248
+ }
249
+ else {
250
+ identifierPropertyClassDeclarationVisibility = Maybe.of("private");
251
+ }
252
+ const identifierProperty = new ObjectType.IdentifierProperty({
253
+ abstract: astType.abstract,
254
+ classDeclarationVisibility: identifierPropertyClassDeclarationVisibility,
255
+ dataFactoryVariable: this.dataFactoryVariable,
256
+ mintingStrategy: astType.mintingStrategy,
257
+ name: astType.tsIdentifierPropertyName,
258
+ lazyObjectTypeMutable: () => properties.some((property) => property.mutable || property.type.mutable),
259
+ objectType: {
260
+ declarationType: astType.tsObjectDeclarationType,
261
+ features: astType.tsFeatures,
262
+ },
263
+ override: astType.parentObjectTypes.length > 0,
264
+ type: identifierType,
265
+ visibility: "public",
266
+ });
267
+ properties.push(identifierProperty);
134
268
  // Type discriminator property
135
269
  const typeDiscriminatorValues = new Set();
136
270
  if (!astType.abstract) {
@@ -141,55 +275,54 @@ export class TypeFactory {
141
275
  typeDiscriminatorValues.add(descendantObjectType.discriminatorValue);
142
276
  }
143
277
  }
144
- properties.push(new ObjectType.TypeDiscriminatorProperty({
145
- abstract: astType.abstract,
146
- configuration: this.configuration,
147
- name: this.configuration.objectTypeDiscriminatorPropertyName,
148
- override: objectType.parentObjectTypes.length > 0 &&
149
- !objectType.parentObjectTypes[0].abstract,
150
- type: {
151
- name: [...typeDiscriminatorValues]
152
- .sort()
153
- .map((name) => `"${name}"`)
154
- .join("|"),
155
- },
156
- value: objectType.discriminatorValue,
157
- }));
278
+ if (typeDiscriminatorValues.size > 0) {
279
+ properties.push(new ObjectType.TypeDiscriminatorProperty({
280
+ abstract: astType.abstract,
281
+ dataFactoryVariable: this.dataFactoryVariable,
282
+ name: astType.tsTypeDiscriminatorPropertyName,
283
+ objectType: {
284
+ declarationType: astType.tsObjectDeclarationType,
285
+ features: astType.tsFeatures,
286
+ },
287
+ override: objectType.parentObjectTypes.length > 0,
288
+ type: new ObjectType.TypeDiscriminatorProperty.Type({
289
+ mutable: false,
290
+ values: [...typeDiscriminatorValues].sort(),
291
+ }),
292
+ visibility: "public",
293
+ value: objectType.discriminatorValue,
294
+ }));
295
+ }
158
296
  return properties.sort((left, right) => left.name.localeCompare(right.name));
159
297
  },
160
298
  mintingStrategy: astType.mintingStrategy,
161
299
  name: tsName(astType.name),
162
- rdfType: astType.rdfType,
300
+ toRdfTypes: astType.toRdfTypes,
163
301
  });
164
302
  this.cachedObjectTypesByIdentifier.set(astType.name.identifier, objectType);
165
303
  return objectType;
166
304
  }
167
- createObjectTypePropertyFromAstProperty(astObjectTypeProperty) {
305
+ createObjectTypePropertyFromAstProperty(astObjectType, astObjectTypeProperty) {
168
306
  {
169
307
  const cachedProperty = this.cachedObjectTypePropertiesByIdentifier.get(astObjectTypeProperty.name.identifier);
170
308
  if (cachedProperty) {
171
309
  return cachedProperty;
172
310
  }
173
311
  }
174
- let type;
175
- if (astObjectTypeProperty.type.kind === "ObjectType" &&
176
- !astObjectTypeProperty.inline) {
177
- // Non-inlined object type = its identifier
178
- type = new IdentifierType({
179
- configuration: this.configuration,
180
- defaultValue: Maybe.empty(),
181
- hasValue: Maybe.empty(),
182
- nodeKinds: astObjectTypeProperty.type.nodeKinds,
183
- });
184
- }
185
- else {
186
- type = this.createTypeFromAstType(astObjectTypeProperty.type);
187
- }
188
312
  const property = new ObjectType.ShaclProperty({
189
- configuration: this.configuration,
313
+ comment: astObjectTypeProperty.comment,
314
+ dataFactoryVariable: this.dataFactoryVariable,
315
+ description: astObjectTypeProperty.description,
316
+ label: astObjectTypeProperty.label,
317
+ mutable: astObjectTypeProperty.mutable.orDefault(false),
318
+ objectType: {
319
+ declarationType: astObjectType.tsObjectDeclarationType,
320
+ features: astObjectType.tsFeatures,
321
+ },
190
322
  name: tsName(astObjectTypeProperty.name),
191
323
  path: astObjectTypeProperty.path.iri,
192
- type,
324
+ type: this.createTypeFromAstType(astObjectTypeProperty.type),
325
+ visibility: astObjectTypeProperty.visibility,
193
326
  });
194
327
  this.cachedObjectTypePropertiesByIdentifier.set(astObjectTypeProperty.name.identifier, property);
195
328
  return property;
@@ -1,18 +1,32 @@
1
1
  import { Maybe } from "purify-ts";
2
+ import type { TsFeature } from "../../enums/index.js";
3
+ import type { Import } from "./Import.js";
2
4
  import { Type } from "./Type.js";
3
5
  export declare class UnionType extends Type {
4
6
  readonly kind = "UnionType";
5
7
  readonly memberTypes: readonly Type[];
6
- constructor({ memberTypes, ...superParameters }: ConstructorParameters<typeof Type>[0] & {
8
+ constructor({ memberTypes, name, ...superParameters }: ConstructorParameters<typeof Type>[0] & {
7
9
  memberTypes: readonly Type[];
10
+ name?: string;
8
11
  });
9
- get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
12
+ private _name?;
10
13
  get name(): string;
11
- private get typesSharedDiscriminatorProperty();
12
- equalsFunction(): string;
14
+ get conversions(): readonly Type.Conversion[];
15
+ get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
16
+ get equalsFunction(): string;
17
+ get jsonName(): string;
18
+ get mutable(): boolean;
19
+ private get _discriminatorProperty();
20
+ private get memberTypeTraits();
21
+ fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
13
22
  fromRdfExpression(parameters: Parameters<Type["fromRdfExpression"]>[0]): string;
14
- hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
15
- propertySparqlGraphPatternExpression(parameters: Parameters<Type["propertySparqlGraphPatternExpression"]>[0]): Type.SparqlGraphPatternExpression | Type.SparqlGraphPatternsExpression;
23
+ hashStatements({ depth, variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
24
+ jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
25
+ sparqlConstructTemplateTriples(parameters: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
26
+ sparqlWherePatterns(parameters: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
27
+ toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
16
28
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
29
+ useImports(features: Set<TsFeature>): readonly Import[];
30
+ private ternaryExpression;
17
31
  }
18
32
  //# sourceMappingURL=UnionType.d.ts.map