@shaclmate/compiler 2.0.12 → 2.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/Compiler.d.ts +15 -0
  2. package/Compiler.js +20 -0
  3. package/ShapesGraphToAstTransformer.d.ts +18 -23
  4. package/ShapesGraphToAstTransformer.js +21 -447
  5. package/_ShapesGraphToAstTransformer/NodeShapeAstType.d.ts +3 -0
  6. package/_ShapesGraphToAstTransformer/NodeShapeAstType.js +2 -0
  7. package/_ShapesGraphToAstTransformer/index.d.ts +10 -0
  8. package/_ShapesGraphToAstTransformer/index.js +10 -0
  9. package/_ShapesGraphToAstTransformer/pickLiteral.d.ts +4 -0
  10. package/_ShapesGraphToAstTransformer/pickLiteral.js +18 -0
  11. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.d.ts +4 -0
  12. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.js +26 -0
  13. package/_ShapesGraphToAstTransformer/shapeAstName.d.ts +5 -0
  14. package/_ShapesGraphToAstTransformer/shapeAstName.js +28 -0
  15. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.d.ts +6 -0
  16. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +165 -0
  17. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +13 -0
  18. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +198 -0
  19. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +12 -0
  20. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +29 -0
  21. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +12 -0
  22. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +43 -0
  23. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.d.ts +6 -0
  24. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +31 -0
  25. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +14 -0
  26. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +21 -0
  27. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +16 -0
  28. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +58 -0
  29. package/ast/Ast.d.ts +5 -0
  30. package/ast/CompositeType.d.ts +13 -0
  31. package/ast/CompositeType.js +2 -0
  32. package/ast/IdentifierType.d.ts +3 -6
  33. package/ast/IntersectionType.d.ts +2 -2
  34. package/ast/ListType.d.ts +53 -0
  35. package/ast/ListType.js +2 -0
  36. package/ast/LiteralType.d.ts +4 -3
  37. package/ast/Name.d.ts +1 -0
  38. package/ast/ObjectCompositeType.d.ts +22 -0
  39. package/ast/ObjectCompositeType.js +2 -0
  40. package/ast/ObjectIntersectionType.d.ts +8 -0
  41. package/ast/ObjectIntersectionType.js +2 -0
  42. package/ast/ObjectType.d.ts +92 -20
  43. package/ast/ObjectType.js +26 -1
  44. package/ast/ObjectUnionType.d.ts +19 -0
  45. package/ast/ObjectUnionType.js +2 -0
  46. package/ast/PlaceholderType.d.ts +7 -0
  47. package/ast/PlaceholderType.js +2 -0
  48. package/ast/SetType.d.ts +1 -1
  49. package/ast/TermType.d.ts +11 -0
  50. package/ast/TermType.js +2 -0
  51. package/ast/Type.d.ts +11 -3
  52. package/ast/UnionType.d.ts +3 -3
  53. package/ast/index.d.ts +6 -3
  54. package/ast/index.js +6 -3
  55. package/enums/MintingStrategy.d.ts +5 -0
  56. package/enums/MintingStrategy.js +2 -0
  57. package/enums/PropertyVisibility.d.ts +2 -0
  58. package/enums/PropertyVisibility.js +2 -0
  59. package/enums/TsFeature.d.ts +5 -0
  60. package/enums/TsFeature.js +16 -0
  61. package/enums/TsObjectDeclarationType.d.ts +5 -0
  62. package/enums/TsObjectDeclarationType.js +2 -0
  63. package/enums/index.d.ts +5 -0
  64. package/enums/index.js +5 -0
  65. package/generators/Generator.d.ts +5 -0
  66. package/generators/Generator.js +2 -0
  67. package/generators/index.d.ts +2 -2
  68. package/generators/index.js +2 -2
  69. package/generators/json/AstJsonGenerator.d.ts +4 -5
  70. package/generators/json/AstJsonGenerator.js +31 -22
  71. package/generators/ts/BooleanType.d.ts +7 -3
  72. package/generators/ts/BooleanType.js +58 -4
  73. package/generators/ts/DateTimeType.d.ts +17 -0
  74. package/generators/ts/DateTimeType.js +58 -0
  75. package/generators/ts/DeclaredType.d.ts +17 -0
  76. package/generators/ts/DeclaredType.js +10 -0
  77. package/generators/ts/IdentifierType.d.ts +7 -17
  78. package/generators/ts/IdentifierType.js +69 -60
  79. package/generators/ts/Import.d.ts +18 -0
  80. package/generators/ts/Import.js +59 -0
  81. package/generators/ts/ListType.d.ts +18 -8
  82. package/generators/ts/ListType.js +197 -43
  83. package/generators/ts/LiteralType.d.ts +13 -11
  84. package/generators/ts/LiteralType.js +36 -51
  85. package/generators/ts/NumberType.d.ts +7 -3
  86. package/generators/ts/NumberType.js +55 -4
  87. package/generators/ts/ObjectType.d.ts +47 -25
  88. package/generators/ts/ObjectType.js +222 -49
  89. package/generators/ts/ObjectUnionType.d.ts +58 -0
  90. package/generators/ts/ObjectUnionType.js +481 -0
  91. package/generators/ts/OptionType.d.ts +12 -4
  92. package/generators/ts/OptionType.js +65 -16
  93. package/generators/ts/PrimitiveType.d.ts +15 -5
  94. package/generators/ts/PrimitiveType.js +20 -29
  95. package/generators/ts/SetType.d.ts +12 -4
  96. package/generators/ts/SetType.js +103 -29
  97. package/generators/ts/StringType.d.ts +8 -3
  98. package/generators/ts/StringType.js +57 -3
  99. package/generators/ts/TermType.d.ts +52 -0
  100. package/generators/ts/TermType.js +234 -0
  101. package/generators/ts/TsGenerator.d.ts +4 -12
  102. package/generators/ts/TsGenerator.js +44 -100
  103. package/generators/ts/Type.d.ts +107 -46
  104. package/generators/ts/Type.js +78 -61
  105. package/generators/ts/TypeFactory.d.ts +4 -5
  106. package/generators/ts/TypeFactory.js +212 -89
  107. package/generators/ts/UnionType.d.ts +19 -6
  108. package/generators/ts/UnionType.js +230 -120
  109. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +27 -7
  110. package/generators/ts/_ObjectType/IdentifierProperty.js +143 -31
  111. package/generators/ts/_ObjectType/Property.d.ts +125 -9
  112. package/generators/ts/_ObjectType/Property.js +25 -3
  113. package/generators/ts/_ObjectType/ShaclProperty.d.ts +23 -5
  114. package/generators/ts/_ObjectType/ShaclProperty.js +100 -26
  115. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +24 -6
  116. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +75 -13
  117. package/generators/ts/_ObjectType/classDeclaration.d.ts +2 -1
  118. package/generators/ts/_ObjectType/classDeclaration.js +79 -111
  119. package/generators/ts/_ObjectType/createFunctionDeclaration.d.ts +5 -0
  120. package/generators/ts/_ObjectType/createFunctionDeclaration.js +60 -0
  121. package/generators/ts/_ObjectType/equalsFunctionDeclaration.d.ts +2 -1
  122. package/generators/ts/_ObjectType/equalsFunctionDeclaration.js +12 -20
  123. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.d.ts +11 -0
  124. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +70 -0
  125. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +4 -0
  126. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +78 -0
  127. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +4 -0
  128. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +91 -0
  129. package/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +3 -3
  130. package/generators/ts/_ObjectType/hashFunctionDeclaration.js +11 -55
  131. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.d.ts +11 -0
  132. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.js +64 -0
  133. package/generators/ts/_ObjectType/index.d.ts +8 -2
  134. package/generators/ts/_ObjectType/index.js +8 -2
  135. package/generators/ts/_ObjectType/interfaceDeclaration.d.ts +3 -2
  136. package/generators/ts/_ObjectType/interfaceDeclaration.js +11 -2
  137. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +5 -0
  138. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +19 -0
  139. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +5 -0
  140. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +31 -0
  141. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +5 -0
  142. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +37 -0
  143. package/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +6 -0
  144. package/generators/ts/{rdfjsTermExpression.js → _ObjectType/rdfjsTermExpression.js} +3 -1
  145. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.d.ts +4 -0
  146. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +101 -0
  147. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +5 -0
  148. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +19 -0
  149. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.d.ts +10 -0
  150. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +59 -0
  151. package/generators/ts/_ObjectType/toJsonReturnType.d.ts +3 -0
  152. package/generators/ts/_ObjectType/toJsonReturnType.js +19 -0
  153. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +3 -2
  154. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +11 -45
  155. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.d.ts +10 -0
  156. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +67 -0
  157. package/generators/ts/objectInitializer.d.ts +2 -0
  158. package/generators/ts/objectInitializer.js +14 -0
  159. package/generators/ts/tsComment.d.ts +5 -0
  160. package/generators/ts/tsComment.js +11 -0
  161. package/generators/ts/tsName.d.ts +1 -1
  162. package/generators/ts/tsName.js +1 -0
  163. package/index.d.ts +1 -1
  164. package/index.js +1 -1
  165. package/input/NodeShape.d.ts +46 -0
  166. package/input/NodeShape.js +165 -0
  167. package/input/Ontology.d.ts +13 -0
  168. package/input/Ontology.js +30 -0
  169. package/input/PropertyShape.d.ts +15 -0
  170. package/input/PropertyShape.js +34 -0
  171. package/input/Shape.d.ts +4 -0
  172. package/input/Shape.js +2 -0
  173. package/input/ShapesGraph.d.ts +9 -0
  174. package/input/ShapesGraph.js +50 -0
  175. package/input/ancestorClassIris.d.ts +4 -0
  176. package/input/ancestorClassIris.js +21 -0
  177. package/input/descendantClassIris.d.ts +4 -0
  178. package/input/descendantClassIris.js +21 -0
  179. package/input/generated.d.ts +274 -0
  180. package/input/generated.js +1420 -0
  181. package/input/index.d.ts +7 -0
  182. package/input/index.js +7 -0
  183. package/input/tsFeatures.d.ts +10 -0
  184. package/input/tsFeatures.js +42 -0
  185. package/package.json +14 -8
  186. package/ast/MintingStrategy.d.ts +0 -8
  187. package/ast/MintingStrategy.js +0 -9
  188. package/generators/ts/Configuration.d.ts +0 -29
  189. package/generators/ts/Configuration.js +0 -40
  190. package/generators/ts/IntersectionType.d.ts +0 -14
  191. package/generators/ts/IntersectionType.js +0 -26
  192. package/generators/ts/RdfjsTermType.d.ts +0 -25
  193. package/generators/ts/RdfjsTermType.js +0 -46
  194. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.d.ts +0 -4
  195. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.js +0 -72
  196. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.d.ts +0 -4
  197. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.js +0 -46
  198. package/generators/ts/rdfjsTermExpression.d.ts +0 -5
  199. package/vocabularies/dashDataset.d.ts +0 -3
  200. package/vocabularies/dashDataset.js +0 -2370
  201. package/vocabularies/index.d.ts +0 -2
  202. package/vocabularies/index.js +0 -2
  203. package/vocabularies/shaclmate.d.ts +0 -15
  204. package/vocabularies/shaclmate.js +0 -6
@@ -1,41 +1,32 @@
1
1
  import { Maybe } from "purify-ts";
2
- import { fromRdf } from "rdf-literal";
3
2
  import { LiteralType } from "./LiteralType.js";
4
3
  export class PrimitiveType extends LiteralType {
5
- get conversions() {
6
- const conversions = [
7
- {
8
- conversionExpression: (value) => value,
9
- sourceTypeName: this.name,
10
- },
11
- ];
12
- this.defaultValue.ifJust((defaultValue) => {
13
- conversions.push({
14
- conversionExpression: () => fromRdf(defaultValue, true),
15
- sourceTypeName: "undefined",
16
- });
17
- });
18
- return conversions;
4
+ constructor({ primitiveDefaultValue, primitiveIn, ...superParameters }) {
5
+ super(superParameters);
6
+ this.equalsFunction = "purifyHelpers.Equatable.strictEquals";
7
+ this.primitiveDefaultValue = primitiveDefaultValue;
8
+ this.primitiveIn = primitiveIn;
19
9
  }
20
10
  get discriminatorProperty() {
21
11
  return Maybe.empty();
22
12
  }
23
- get importStatements() {
13
+ get jsonName() {
14
+ return this.name;
15
+ }
16
+ get useImports() {
24
17
  return [];
25
18
  }
26
- equalsFunction() {
27
- return "purifyHelpers.Equatable.strictEquals";
28
- }
29
- toRdfExpression({ variables, }) {
30
- return this.defaultValue
31
- .map((defaultValue) => {
32
- let primitiveDefaultValue = fromRdf(defaultValue, true);
33
- if (typeof primitiveDefaultValue === "string") {
34
- primitiveDefaultValue = `"${primitiveDefaultValue}"`;
35
- }
36
- return `${variables.value} !== ${primitiveDefaultValue} ? ${variables.value} : undefined`;
37
- })
38
- .orDefault(variables.value);
19
+ fromJsonExpression({ variables, }) {
20
+ return variables.value;
21
+ }
22
+ hashStatements({ variables, }) {
23
+ return [`${variables.hasher}.update(${variables.value}.toString());`];
24
+ }
25
+ toJsonExpression({ variables, }) {
26
+ return variables.value;
27
+ }
28
+ propertyFilterRdfResourceValuesExpression({ variables, }) {
29
+ return variables.resourceValues;
39
30
  }
40
31
  }
41
32
  //# sourceMappingURL=PrimitiveType.js.map
@@ -1,3 +1,4 @@
1
+ import type { Import } from "./Import.js";
1
2
  import { Type } from "./Type.js";
2
3
  export declare class SetType extends Type {
3
4
  readonly itemType: Type;
@@ -8,12 +9,19 @@ export declare class SetType extends Type {
8
9
  minCount: number;
9
10
  });
10
11
  get conversions(): readonly Type.Conversion[];
12
+ get equalsFunction(): string;
13
+ get jsonName(): string;
14
+ get mutable(): boolean;
11
15
  get name(): string;
12
- chainSparqlGraphPatternExpression(parameters: Parameters<Type["chainSparqlGraphPatternExpression"]>[0]): ReturnType<Type["chainSparqlGraphPatternExpression"]>;
13
- equalsFunction(): string;
16
+ get useImports(): readonly Import[];
17
+ fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
14
18
  fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
15
- hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
16
- propertySparqlGraphPatternExpression(parameters: Parameters<Type["propertySparqlGraphPatternExpression"]>[0]): Type.SparqlGraphPatternExpression | Type.SparqlGraphPatternsExpression;
19
+ hashStatements({ depth, variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
20
+ jsonUiSchemaElement(parameters: Parameters<Type["jsonUiSchemaElement"]>[0]): ReturnType<Type["jsonUiSchemaElement"]>;
21
+ jsonZodSchema(parameters: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
22
+ sparqlConstructTemplateTriples({ context, variables, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
23
+ sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
24
+ toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
17
25
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
18
26
  }
19
27
  //# sourceMappingURL=SetType.d.ts.map
@@ -4,70 +4,144 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
+ import { invariant } from "ts-invariant";
7
8
  import { Memoize } from "typescript-memoize";
8
9
  import { Type } from "./Type.js";
10
+ import { objectInitializer } from "./objectInitializer.js";
9
11
  export class SetType extends Type {
10
12
  constructor({ itemType, minCount, ...superParameters }) {
11
13
  super(superParameters);
12
14
  this.kind = "SetType";
13
15
  this.itemType = itemType;
14
16
  this.minCount = minCount;
17
+ invariant(this.minCount >= 0);
15
18
  }
16
19
  get conversions() {
17
- return [
18
- {
20
+ const conversions = [];
21
+ if (this.minCount === 0) {
22
+ conversions.push({
19
23
  conversionExpression: () => "[]",
24
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "undefined"`,
20
25
  sourceTypeName: "undefined",
21
- },
22
- {
26
+ });
27
+ conversions.push({
23
28
  conversionExpression: (value) => value,
24
29
  sourceTypeCheckExpression: (value) => `Array.isArray(${value})`,
30
+ sourceTypeName: `readonly (${this.itemType.name})[]`,
31
+ });
32
+ }
33
+ else {
34
+ conversions.push({
35
+ conversionExpression: (value) => value,
36
+ sourceTypeCheckExpression: (value) => `purify.NonEmptyList.isNonEmpty(${value})`,
25
37
  sourceTypeName: this.name,
26
- },
27
- ];
28
- }
29
- get name() {
30
- return `readonly (${this.itemType.name})[]`;
31
- }
32
- chainSparqlGraphPatternExpression(parameters) {
33
- return this.itemType.chainSparqlGraphPatternExpression(parameters);
38
+ });
39
+ }
40
+ return conversions;
34
41
  }
35
- equalsFunction() {
36
- const itemTypeEqualsFunction = this.itemType.equalsFunction();
42
+ get equalsFunction() {
43
+ const itemTypeEqualsFunction = this.itemType.equalsFunction;
37
44
  if (itemTypeEqualsFunction === "purifyHelpers.Equatable.equals") {
38
45
  return "purifyHelpers.Equatable.arrayEquals";
39
46
  }
40
47
  return `(left, right) => purifyHelpers.Arrays.equals(left, right, ${itemTypeEqualsFunction})`;
41
48
  }
49
+ get jsonName() {
50
+ if (this.minCount === 0) {
51
+ return `readonly (${this.itemType.jsonName})[]`;
52
+ }
53
+ return `purify.NonEmptyList<${this.itemType.jsonName}>`;
54
+ }
55
+ get mutable() {
56
+ return this.itemType.mutable;
57
+ }
58
+ get name() {
59
+ if (this.minCount === 0) {
60
+ return `readonly (${this.itemType.name})[]`;
61
+ }
62
+ return `purify.NonEmptyList<${this.itemType.name}>`;
63
+ }
64
+ get useImports() {
65
+ return this.itemType.useImports;
66
+ }
67
+ fromJsonExpression({ variables, }) {
68
+ let expression = variables.value;
69
+ if (this.minCount > 0) {
70
+ expression = `purify.NonEmptyList.fromArray(${expression}).unsafeCoerce()`;
71
+ }
72
+ const itemFromJsonExpression = this.itemType.fromJsonExpression({
73
+ variables: { value: "_item" },
74
+ });
75
+ return itemFromJsonExpression === "_item"
76
+ ? expression
77
+ : `${expression}.map(_item => (${itemFromJsonExpression}))`;
78
+ }
42
79
  fromRdfExpression({ variables, }) {
43
- return `purify.Either.of([...${variables.resourceValues}.flatMap(value => ${this.itemType.fromRdfExpression({ variables: { ...variables, resourceValues: "value.toValues()" } })}.toMaybe().toList())])`;
80
+ const itemFromRdfExpression = this.itemType.fromRdfExpression({
81
+ variables: { ...variables, resourceValues: "_item.toValues()" },
82
+ });
83
+ if (this.minCount === 0) {
84
+ return `purify.Either.of([...${variables.resourceValues}.flatMap(_item => ${itemFromRdfExpression}.toMaybe().toList())])`;
85
+ }
86
+ return `purify.NonEmptyList.fromArray([...${variables.resourceValues}.flatMap(_item => ${itemFromRdfExpression}.toMaybe().toList())]).toEither(new rdfjsResource.Resource.ValueError(${objectInitializer({ focusResource: variables.resource, message: `\`\${rdfjsResource.Resource.Identifier.toString(${variables.resource}.identifier)} is empty\``, predicate: variables.predicate })}))`;
44
87
  }
45
- hashStatements({ variables, }) {
88
+ hashStatements({ depth, variables, }) {
46
89
  return [
47
- `for (const element of ${variables.value}) { ${this.itemType
90
+ `for (const _item${depth} of ${variables.value}) { ${this.itemType
48
91
  .hashStatements({
92
+ depth: depth + 1,
49
93
  variables: {
50
94
  hasher: variables.hasher,
51
- value: "element",
95
+ value: `_item${depth}`,
52
96
  },
53
97
  })
54
98
  .join("\n")} }`,
55
99
  ];
56
100
  }
57
- propertySparqlGraphPatternExpression(parameters) {
58
- if (this.minCount === 0) {
59
- return new Type.SparqlGraphPatternExpression(`sparqlBuilder.GraphPattern.optional(${this.itemType.propertySparqlGraphPatternExpression(parameters).toSparqlGraphPatternExpression()})`);
101
+ jsonUiSchemaElement(parameters) {
102
+ return this.itemType.jsonUiSchemaElement(parameters);
103
+ }
104
+ jsonZodSchema(parameters) {
105
+ let schema = `${this.itemType.jsonZodSchema(parameters)}.array()`;
106
+ if (this.minCount > 0) {
107
+ schema = `${schema}.nonempty().min(${this.minCount})`;
60
108
  }
61
- return this.itemType.propertySparqlGraphPatternExpression(parameters);
109
+ return schema;
62
110
  }
63
- toRdfExpression({ variables, }) {
64
- const itemTypeToRdfExpression = this.itemType.toRdfExpression({
65
- variables: { ...variables, value: "value" },
66
- });
67
- if (itemTypeToRdfExpression === "value") {
68
- return variables.value;
111
+ sparqlConstructTemplateTriples({ context, variables, }) {
112
+ switch (context) {
113
+ case "property":
114
+ return super.sparqlConstructTemplateTriples({ context, variables });
115
+ case "type":
116
+ return this.itemType.sparqlConstructTemplateTriples({
117
+ context,
118
+ variables,
119
+ });
120
+ }
121
+ }
122
+ sparqlWherePatterns({ context, variables, }) {
123
+ switch (context) {
124
+ case "property": {
125
+ const patterns = super.sparqlWherePatterns({ context, variables });
126
+ if (patterns.length === 0) {
127
+ return [];
128
+ }
129
+ return this.minCount > 0
130
+ ? patterns
131
+ : [`{ patterns: [${patterns.join(", ")}], type: "optional" }`];
132
+ }
133
+ case "type": {
134
+ return this.itemType.sparqlWherePatterns({ context, variables });
135
+ }
69
136
  }
70
- return `${variables.value}.map((value) => ${itemTypeToRdfExpression})`;
137
+ }
138
+ toJsonExpression({ variables, }) {
139
+ return `${variables.value}.map(_item => (${this.itemType.toJsonExpression({ variables: { value: "_item" } })}))`;
140
+ }
141
+ toRdfExpression({ variables, }) {
142
+ return `${variables.value}.map((_item) => ${this.itemType.toRdfExpression({
143
+ variables: { ...variables, value: "_item" },
144
+ })})`;
71
145
  }
72
146
  }
73
147
  __decorate([
@@ -1,7 +1,12 @@
1
1
  import { PrimitiveType } from "./PrimitiveType.js";
2
- export declare class StringType extends PrimitiveType {
2
+ import type { Type } from "./Type.js";
3
+ export declare class StringType extends PrimitiveType<string> {
4
+ readonly kind = "StringType";
5
+ get conversions(): readonly Type.Conversion[];
3
6
  get name(): string;
4
- fromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType["fromRdfResourceValueExpression"]>[0]): string;
5
- hashStatements({ variables, }: Parameters<PrimitiveType["hashStatements"]>[0]): readonly string[];
7
+ hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
8
+ jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
9
+ propertyFromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<string>["propertyFromRdfResourceValueExpression"]>[0]): string;
10
+ toRdfExpression({ variables, }: Parameters<PrimitiveType<string>["toRdfExpression"]>[0]): string;
6
11
  }
7
12
  //# sourceMappingURL=StringType.d.ts.map
@@ -1,13 +1,67 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Memoize } from "typescript-memoize";
1
8
  import { PrimitiveType } from "./PrimitiveType.js";
9
+ import { objectInitializer } from "./objectInitializer.js";
2
10
  export class StringType extends PrimitiveType {
11
+ constructor() {
12
+ super(...arguments);
13
+ this.kind = "StringType";
14
+ }
15
+ get conversions() {
16
+ const conversions = [
17
+ {
18
+ conversionExpression: (value) => value,
19
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "string"`,
20
+ sourceTypeName: this.name,
21
+ },
22
+ ];
23
+ this.primitiveDefaultValue.ifJust((defaultValue) => {
24
+ conversions.push({
25
+ conversionExpression: () => `"${defaultValue}"`,
26
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "undefined"`,
27
+ sourceTypeName: "undefined",
28
+ });
29
+ });
30
+ return conversions;
31
+ }
3
32
  get name() {
33
+ if (this.primitiveIn.length > 0) {
34
+ return this.primitiveIn.map((value) => `"${value}"`).join(" | ");
35
+ }
4
36
  return "string";
5
37
  }
6
- fromRdfResourceValueExpression({ variables, }) {
7
- return `${variables.resourceValue}.toString()`;
8
- }
9
38
  hashStatements({ variables, }) {
10
39
  return [`${variables.hasher}.update(${variables.value});`];
11
40
  }
41
+ jsonZodSchema({ variables, }) {
42
+ switch (this.primitiveIn.length) {
43
+ case 0:
44
+ return `${variables.zod}.string()`;
45
+ case 1:
46
+ return `${variables.zod}.literal(${this.primitiveIn[0]})`;
47
+ default:
48
+ return `${variables.zod}.enum(${JSON.stringify(this.primitiveIn)})`;
49
+ }
50
+ }
51
+ propertyFromRdfResourceValueExpression({ variables, }) {
52
+ let expression = `${variables.resourceValue}.toString()`;
53
+ if (this.primitiveIn.length > 0) {
54
+ expression = `${expression}.chain(value => { switch (value) { ${this.primitiveIn.map((value) => `case "${value}":`).join(" ")} return purify.Either.of(value); default: return purify.Left(new rdfjsResource.Resource.MistypedValueError(${objectInitializer({ actualValue: "rdfLiteral.toRdf(value)", expectedValueType: JSON.stringify(this.name), focusResource: variables.resource, predicate: variables.predicate })})); } })`;
55
+ }
56
+ return expression;
57
+ }
58
+ toRdfExpression({ variables, }) {
59
+ return this.primitiveDefaultValue
60
+ .map((defaultValue) => `${variables.value} !== "${defaultValue}" ? ${variables.value} : undefined`)
61
+ .orDefault(variables.value);
62
+ }
12
63
  }
64
+ __decorate([
65
+ Memoize()
66
+ ], StringType.prototype, "name", null);
13
67
  //# sourceMappingURL=StringType.js.map
@@ -0,0 +1,52 @@
1
+ import type { BlankNode, Literal, NamedNode } from "@rdfjs/types";
2
+ import { Maybe } from "purify-ts";
3
+ import { Import } from "./Import.js";
4
+ import { Type } from "./Type.js";
5
+ /**
6
+ * Abstract base class for IdentifierType and LiteralType.
7
+ */
8
+ export declare class TermType<TermT extends BlankNode | Literal | NamedNode> extends Type {
9
+ readonly defaultValue: Maybe<TermT>;
10
+ readonly equalsFunction: string;
11
+ readonly hasValues: readonly TermT[];
12
+ readonly in_: readonly TermT[];
13
+ readonly mutable: boolean;
14
+ readonly nodeKinds: Set<TermT["termType"]>;
15
+ constructor({ defaultValue, hasValues, in_, nodeKinds, ...superParameters }: {
16
+ defaultValue: Maybe<TermT>;
17
+ hasValues: readonly TermT[];
18
+ in_: readonly TermT[];
19
+ nodeKinds: Set<TermT["termType"]>;
20
+ } & ConstructorParameters<typeof Type>[0]);
21
+ get conversions(): readonly Type.Conversion[];
22
+ get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
23
+ get jsonName(): string;
24
+ get name(): string;
25
+ get useImports(): readonly Import[];
26
+ fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
27
+ fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
28
+ hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
29
+ jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
30
+ sparqlWherePatterns(parameters: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
31
+ toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
32
+ toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
33
+ /**
34
+ * Filter the rdfjsResource.Resource.Values to those that are relevant to the type.
35
+ *
36
+ * This is done before
37
+ */
38
+ protected propertyFilterRdfResourceValuesExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
39
+ /**
40
+ * Convert an rdfjsResource.Resource.Value to a value of this type.
41
+ * @param variables
42
+ * @protected
43
+ */
44
+ protected propertyFromRdfResourceValueExpression({ variables, }: {
45
+ variables: {
46
+ predicate: string;
47
+ resource: string;
48
+ resourceValue: string;
49
+ };
50
+ }): string;
51
+ }
52
+ //# sourceMappingURL=TermType.d.ts.map
@@ -0,0 +1,234 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { xsd } from "@tpluscode/rdf-ns-builders";
8
+ import { Maybe } from "purify-ts";
9
+ import { invariant } from "ts-invariant";
10
+ import { Memoize } from "typescript-memoize";
11
+ import { Import } from "./Import.js";
12
+ import { Type } from "./Type.js";
13
+ import { objectInitializer } from "./objectInitializer.js";
14
+ /**
15
+ * Abstract base class for IdentifierType and LiteralType.
16
+ */
17
+ export class TermType extends Type {
18
+ constructor({ defaultValue, hasValues, in_, nodeKinds, ...superParameters }) {
19
+ super(superParameters);
20
+ this.equalsFunction = "purifyHelpers.Equatable.booleanEquals";
21
+ this.mutable = false;
22
+ this.defaultValue = defaultValue;
23
+ this.hasValues = hasValues;
24
+ this.in_ = in_;
25
+ this.nodeKinds = new Set([...nodeKinds]);
26
+ invariant(this.nodeKinds.size > 0);
27
+ }
28
+ get conversions() {
29
+ const conversions = [];
30
+ if (this.nodeKinds.has("Literal")) {
31
+ conversions.push({
32
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value})`,
33
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "boolean"`,
34
+ sourceTypeName: "boolean",
35
+ }, {
36
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value})`,
37
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object" && ${value} instanceof Date`,
38
+ sourceTypeName: "Date",
39
+ }, {
40
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value})`,
41
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "number"`,
42
+ sourceTypeName: "number",
43
+ }, {
44
+ conversionExpression: (value) => `${this.dataFactoryVariable}.literal(${value})`,
45
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "string"`,
46
+ sourceTypeName: "string",
47
+ });
48
+ }
49
+ this.defaultValue.ifJust((defaultValue) => {
50
+ conversions.push({
51
+ conversionExpression: () => this.rdfjsTermExpression(defaultValue),
52
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "undefined"`,
53
+ sourceTypeName: "undefined",
54
+ });
55
+ });
56
+ conversions.push({
57
+ conversionExpression: (value) => value,
58
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
59
+ sourceTypeName: this.name,
60
+ });
61
+ return conversions;
62
+ }
63
+ get discriminatorProperty() {
64
+ return Maybe.of({
65
+ name: "termType",
66
+ type: "string",
67
+ values: [...this.nodeKinds],
68
+ });
69
+ }
70
+ get jsonName() {
71
+ invariant(this.nodeKinds.has("Literal") &&
72
+ (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
73
+ return `{ readonly "@id": string, readonly termType: ${[...this.nodeKinds]
74
+ .filter((nodeKind) => nodeKind !== "Literal")
75
+ .map((nodeKind) => `"${nodeKind}"`)
76
+ .join(" | ")} } | { readonly "@language": string | undefined, readonly "@type": string | undefined, readonly "@value": string, readonly termType: "Literal" }`;
77
+ }
78
+ get name() {
79
+ return `(${[...this.nodeKinds]
80
+ .map((nodeKind) => `rdfjs.${nodeKind}`)
81
+ .join(" | ")})`;
82
+ }
83
+ get useImports() {
84
+ const imports = [Import.RDFJS_TYPES];
85
+ if (this.nodeKinds.has("Literal")) {
86
+ imports.push(Import.RDF_LITERAL);
87
+ }
88
+ return imports;
89
+ }
90
+ fromJsonExpression({ variables, }) {
91
+ invariant(this.nodeKinds.has("Literal") &&
92
+ (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
93
+ return [...this.nodeKinds].reduce((expression, nodeKind) => {
94
+ let valueToNodeKind;
95
+ switch (nodeKind) {
96
+ case "BlankNode":
97
+ valueToNodeKind = `${this.dataFactoryVariable}.blankNode(${variables.value}["@id"].substring(2))`;
98
+ break;
99
+ case "Literal":
100
+ 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))`;
101
+ break;
102
+ case "NamedNode":
103
+ valueToNodeKind = `${this.dataFactoryVariable}.namedNode(${variables.value}["@id"])`;
104
+ break;
105
+ default:
106
+ throw new RangeError(nodeKind);
107
+ }
108
+ return expression.length === 0
109
+ ? valueToNodeKind
110
+ : `((${variables.value}.termType === "${nodeKind}") ? (${valueToNodeKind}) : (${expression}))`;
111
+ }, "");
112
+ }
113
+ fromRdfExpression({ variables, }) {
114
+ const chain = [
115
+ this.propertyFilterRdfResourceValuesExpression({ variables }),
116
+ ];
117
+ // Have an rdfjsResource.Resource.Values here
118
+ if (this.hasValues.length === 1) {
119
+ chain.push(`find(_value => _value.toTerm().equals(${this.rdfjsTermExpression(this.hasValues[0])}))`);
120
+ }
121
+ else {
122
+ chain.push("head()");
123
+ }
124
+ // Have an rdfjsResource.Resource.Value here
125
+ this.defaultValue.ifJust((defaultValue) => {
126
+ // alt the default value before trying to convert the rdfjsResource.Resource.Value to the type
127
+ chain.push(`alt(purify.Either.of(new rdfjsResource.Resource.Value(${objectInitializer({ subject: variables.resource, predicate: variables.predicate, object: this.rdfjsTermExpression(defaultValue) })})))`);
128
+ });
129
+ // Last step: convert the rdfjsResource.Resource.Value to the type
130
+ chain.push(`chain(_value => ${this.propertyFromRdfResourceValueExpression({
131
+ variables: {
132
+ predicate: variables.predicate,
133
+ resource: variables.resource,
134
+ resourceValue: "_value",
135
+ },
136
+ })})`);
137
+ return chain.join(".");
138
+ }
139
+ hashStatements({ variables, }) {
140
+ return [
141
+ `${variables.hasher}.update(${variables.value}.termType);`,
142
+ `${variables.hasher}.update(${variables.value}.value);`,
143
+ ];
144
+ }
145
+ jsonZodSchema({ variables, }) {
146
+ invariant(this.nodeKinds.has("Literal") &&
147
+ (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
148
+ return `${variables.zod}.discriminatedUnion("termType", [${[
149
+ ...this.nodeKinds,
150
+ ]
151
+ .map((nodeKind) => {
152
+ switch (nodeKind) {
153
+ case "BlankNode":
154
+ case "NamedNode":
155
+ return `${variables.zod}.object({ "@id": ${variables.zod}.string().min(1), termType: ${variables.zod}.literal("${nodeKind}") })`;
156
+ case "Literal":
157
+ return `${variables.zod}.object({ "@language": ${variables.zod}.string().optional(), "@type": ${variables.zod}.string().optional(), "@value": ${variables.zod}.string(), termType: ${variables.zod}.literal("Literal") })`;
158
+ default:
159
+ throw new RangeError(nodeKind);
160
+ }
161
+ })
162
+ .join(", ")}])`;
163
+ }
164
+ sparqlWherePatterns(parameters) {
165
+ switch (parameters.context) {
166
+ case "property":
167
+ return this.defaultValue
168
+ .map(() => [
169
+ `{ patterns: [${super.sparqlWherePatterns(parameters).join(", ")}], type: "optional" }`,
170
+ ])
171
+ .orDefault(super.sparqlWherePatterns(parameters));
172
+ case "type":
173
+ return super.sparqlWherePatterns(parameters);
174
+ }
175
+ }
176
+ toJsonExpression({ variables, }) {
177
+ invariant(this.nodeKinds.has("Literal") &&
178
+ (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
179
+ return [...this.nodeKinds].reduce((expression, nodeKind) => {
180
+ let valueToNodeKind;
181
+ switch (nodeKind) {
182
+ case "BlankNode":
183
+ valueToNodeKind = `{ "@id": \`_:\${${variables.value}.value}\`, termType: "${nodeKind}" as const }`;
184
+ break;
185
+ case "Literal":
186
+ valueToNodeKind = `{ "@language": ${variables.value}.language.length > 0 ? ${variables.value}.language : undefined, "@type": ${variables.value}.datatype.value !== "${xsd.string.value}" ? ${variables.value}.datatype.value : undefined, "@value": ${variables.value}.value, termType: "${nodeKind}" as const }`;
187
+ break;
188
+ case "NamedNode":
189
+ valueToNodeKind = `{ "@id": ${variables.value}.value, termType: "${nodeKind}" as const }`;
190
+ break;
191
+ default:
192
+ throw new RangeError(nodeKind);
193
+ }
194
+ return expression.length === 0
195
+ ? valueToNodeKind
196
+ : `(${variables.value}.termType === "${nodeKind}") ? ${valueToNodeKind} : ${expression}`;
197
+ }, "");
198
+ }
199
+ toRdfExpression({ variables, }) {
200
+ return this.defaultValue
201
+ .map((defaultValue) => `!${variables.value}.equals(${this.rdfjsTermExpression(defaultValue)}) ? ${variables.value} : undefined`)
202
+ .orDefault(variables.value);
203
+ }
204
+ /**
205
+ * Filter the rdfjsResource.Resource.Values to those that are relevant to the type.
206
+ *
207
+ * This is done before
208
+ */
209
+ propertyFilterRdfResourceValuesExpression({ variables, }) {
210
+ return variables.resourceValues;
211
+ }
212
+ /**
213
+ * Convert an rdfjsResource.Resource.Value to a value of this type.
214
+ * @param variables
215
+ * @protected
216
+ */
217
+ propertyFromRdfResourceValueExpression({ variables, }) {
218
+ invariant(this.nodeKinds.has("Literal") &&
219
+ (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")), "IdentifierType and LiteralType should override");
220
+ let expression = `purify.Either.of(${variables.resourceValue}.toTerm())`;
221
+ if (this.nodeKinds.size < 3) {
222
+ expression = `${expression}.chain(term => {
223
+ switch (term.termType) {
224
+ ${[...this.nodeKinds].map((nodeKind) => `case "${nodeKind}":`).join("\n")} return purify.Either.of(term);
225
+ default: return purify.Left(new rdfjsResource.Resource.MistypedValueError(${objectInitializer({ actualValue: "term", expectedValueType: JSON.stringify(this.name), focusResource: variables.resource, predicate: variables.predicate })}));
226
+ }})`;
227
+ }
228
+ return expression;
229
+ }
230
+ }
231
+ __decorate([
232
+ Memoize()
233
+ ], TermType.prototype, "name", null);
234
+ //# sourceMappingURL=TermType.js.map
@@ -1,15 +1,7 @@
1
- import type * as ast from "../../ast";
2
- import { Configuration as GlobalConfiguration } from "./Configuration.js";
3
- export declare class TsGenerator {
4
- private ast;
5
- protected readonly configuration: TsGenerator.Configuration;
6
- constructor(ast: ast.Ast, configuration?: TsGenerator.Configuration);
7
- generate(): string;
1
+ import * as ast from "../../ast/index.js";
2
+ import type { Generator } from "../Generator.js";
3
+ export declare class TsGenerator implements Generator {
4
+ generate(ast_: ast.Ast): string;
8
5
  private addDeclarations;
9
- private addImportDeclarations;
10
- }
11
- export declare namespace TsGenerator {
12
- const Configuration: typeof GlobalConfiguration;
13
- type Configuration = GlobalConfiguration;
14
6
  }
15
7
  //# sourceMappingURL=TsGenerator.d.ts.map