@shaclmate/compiler 2.0.13 → 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 +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 +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,53 +1,72 @@
1
1
  import type { NamedNode } from "@rdfjs/types";
2
2
  import { Maybe } from "purify-ts";
3
- import { MintingStrategy } from "../../ast";
3
+ import { type ClassDeclarationStructure, type InterfaceDeclarationStructure, type ModuleDeclarationStructure } from "ts-morph";
4
+ import type { MintingStrategy, TsObjectDeclarationType } from "../../enums/index.js";
5
+ import { DeclaredType } from "./DeclaredType.js";
4
6
  import type { IdentifierType } from "./IdentifierType.js";
5
- import { Type } from "./Type.js";
6
- import * as _ObjectType from "./_ObjectType";
7
- export declare class ObjectType extends Type {
7
+ import { Import } from "./Import.js";
8
+ import type { Type } from "./Type.js";
9
+ import * as _ObjectType from "./_ObjectType/index.js";
10
+ export declare class ObjectType extends DeclaredType {
8
11
  readonly abstract: boolean;
9
- classDeclaration: typeof _ObjectType.classDeclaration;
10
- equalsFunctionDeclaration: typeof _ObjectType.equalsFunctionDeclaration;
11
- readonly export_: boolean;
12
- fromRdfFunctionDeclaration: typeof _ObjectType.fromRdfFunctionDeclaration;
13
- hashFunctionDeclaration: typeof _ObjectType.hashFunctionDeclaration;
14
- readonly identifierType: IdentifierType;
15
- interfaceDeclaration: typeof _ObjectType.interfaceDeclaration;
12
+ readonly declarationType: TsObjectDeclarationType;
16
13
  readonly kind = "ObjectType";
17
- readonly mintingStrategy: Maybe<MintingStrategy>;
18
- readonly name: string;
19
- readonly rdfType: Maybe<NamedNode>;
20
- sparqlGraphPatternsClassDeclaration: typeof _ObjectType.sparqlGraphPatternsClassDeclaration;
21
- toRdfFunctionDeclaration: typeof _ObjectType.toRdfFunctionDeclaration;
14
+ protected readonly comment: Maybe<string>;
15
+ protected readonly extern: boolean;
16
+ protected readonly fromRdfType: Maybe<NamedNode>;
17
+ protected readonly label: Maybe<string>;
18
+ protected readonly mintingStrategy: Maybe<MintingStrategy>;
19
+ protected readonly toRdfTypes: readonly NamedNode[];
20
+ private readonly imports;
22
21
  private readonly lazyAncestorObjectTypes;
23
22
  private readonly lazyDescendantObjectTypes;
24
23
  private readonly lazyParentObjectTypes;
25
24
  private readonly lazyProperties;
26
- constructor({ abstract, identifierType, export_, lazyAncestorObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, mintingStrategy, name, rdfType, ...superParameters }: {
25
+ constructor({ abstract, comment, declarationType, extern, fromRdfType, label, lazyAncestorObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, imports, mintingStrategy, toRdfTypes, ...superParameters }: {
27
26
  abstract: boolean;
28
- export_: boolean;
29
- identifierType: IdentifierType;
27
+ comment: Maybe<string>;
28
+ declarationType: TsObjectDeclarationType;
29
+ extern: boolean;
30
+ fromRdfType: Maybe<NamedNode>;
31
+ imports: readonly string[];
32
+ label: Maybe<string>;
30
33
  lazyAncestorObjectTypes: () => readonly ObjectType[];
31
34
  lazyDescendantObjectTypes: () => readonly ObjectType[];
32
35
  lazyParentObjectTypes: () => readonly ObjectType[];
33
36
  lazyProperties: () => readonly ObjectType.Property[];
34
37
  mintingStrategy: Maybe<MintingStrategy>;
35
- name: string;
36
- rdfType: Maybe<NamedNode>;
37
- } & ConstructorParameters<typeof Type>[0]);
38
+ toRdfTypes: readonly NamedNode[];
39
+ } & ConstructorParameters<typeof DeclaredType>[0]);
40
+ get _discriminatorProperty(): Type.DiscriminatorProperty;
38
41
  get ancestorObjectTypes(): readonly ObjectType[];
39
42
  get conversions(): readonly Type.Conversion[];
43
+ get declarationImports(): readonly Import[];
44
+ get declarations(): (ClassDeclarationStructure | InterfaceDeclarationStructure | ModuleDeclarationStructure)[];
40
45
  get descendantObjectTypes(): readonly ObjectType[];
41
46
  get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
42
47
  get discriminatorValue(): string;
48
+ get equalsFunction(): string;
43
49
  get hashFunctionName(): string;
44
- get importStatements(): readonly string[];
50
+ get identifierProperty(): ObjectType.IdentifierProperty;
51
+ get identifierType(): IdentifierType;
52
+ get jsonName(): string;
53
+ get jsonUiSchemaFunctionName(): string;
54
+ get jsonZodSchemaFunctionName(): string;
55
+ get mutable(): boolean;
56
+ get ownProperties(): readonly ObjectType.Property[];
45
57
  get parentObjectTypes(): readonly ObjectType[];
46
58
  get properties(): readonly ObjectType.Property[];
47
- chainSparqlGraphPatternExpression({ variables, }: Parameters<Type["chainSparqlGraphPatternExpression"]>[0]): Maybe<Type.SparqlGraphPatternsExpression>;
48
- equalsFunction(): string;
59
+ get useImports(): readonly Import[];
60
+ protected get thisVariable(): string;
61
+ fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
49
62
  fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
50
63
  hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
64
+ jsonUiSchemaElement({ variables, }: {
65
+ variables: {
66
+ scopePrefix: string;
67
+ };
68
+ }): Maybe<string>;
69
+ jsonZodSchema(_parameters: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
51
70
  rdfjsResourceType(options?: {
52
71
  mutable?: boolean;
53
72
  }): {
@@ -55,6 +74,9 @@ export declare class ObjectType extends Type {
55
74
  readonly name: string;
56
75
  readonly named: boolean;
57
76
  };
77
+ sparqlConstructTemplateTriples({ context, variables, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
78
+ sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
79
+ toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
58
80
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
59
81
  protected ensureAtMostOneSuperObjectType(): void;
60
82
  }
@@ -4,33 +4,42 @@ 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 { camelCase } from "change-case";
7
8
  import { Maybe } from "purify-ts";
9
+ import { invariant } from "ts-invariant";
10
+ import { StructureKind, } from "ts-morph";
8
11
  import { Memoize } from "typescript-memoize";
9
- import { MintingStrategy } from "../../ast";
10
- import { Type } from "./Type.js";
11
- import * as _ObjectType from "./_ObjectType";
12
- export class ObjectType extends Type {
13
- constructor({ abstract, identifierType, export_, lazyAncestorObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, mintingStrategy, name, rdfType, ...superParameters }) {
12
+ import { DeclaredType } from "./DeclaredType.js";
13
+ import { Import } from "./Import.js";
14
+ import * as _ObjectType from "./_ObjectType/index.js";
15
+ import { IdentifierProperty, TypeDiscriminatorProperty, } from "./_ObjectType/index.js";
16
+ import { objectInitializer } from "./objectInitializer.js";
17
+ export class ObjectType extends DeclaredType {
18
+ constructor({ abstract, comment, declarationType, extern, fromRdfType, label, lazyAncestorObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, imports, mintingStrategy, toRdfTypes, ...superParameters }) {
14
19
  super(superParameters);
15
- this.classDeclaration = _ObjectType.classDeclaration;
16
- this.equalsFunctionDeclaration = _ObjectType.equalsFunctionDeclaration;
17
- this.fromRdfFunctionDeclaration = _ObjectType.fromRdfFunctionDeclaration;
18
- this.hashFunctionDeclaration = _ObjectType.hashFunctionDeclaration;
19
- this.interfaceDeclaration = _ObjectType.interfaceDeclaration;
20
20
  this.kind = "ObjectType";
21
- this.sparqlGraphPatternsClassDeclaration = _ObjectType.sparqlGraphPatternsClassDeclaration;
22
- this.toRdfFunctionDeclaration = _ObjectType.toRdfFunctionDeclaration;
23
21
  this.abstract = abstract;
24
- this.export_ = export_;
22
+ this.comment = comment;
23
+ this.declarationType = declarationType;
24
+ this.extern = extern;
25
+ this.fromRdfType = fromRdfType;
26
+ this.imports = imports;
27
+ this.label = label;
25
28
  // Lazily initialize some members in getters to avoid recursive construction
26
29
  this.lazyAncestorObjectTypes = lazyAncestorObjectTypes;
27
30
  this.lazyDescendantObjectTypes = lazyDescendantObjectTypes;
28
31
  this.lazyParentObjectTypes = lazyParentObjectTypes;
29
32
  this.lazyProperties = lazyProperties;
30
- this.identifierType = identifierType;
31
33
  this.mintingStrategy = mintingStrategy;
32
- this.rdfType = rdfType;
33
- this.name = name;
34
+ this.toRdfTypes = toRdfTypes;
35
+ }
36
+ get _discriminatorProperty() {
37
+ const discriminatorProperty = this.properties.find((property) => property instanceof ObjectType.TypeDiscriminatorProperty);
38
+ invariant(discriminatorProperty);
39
+ return {
40
+ name: discriminatorProperty.name,
41
+ values: [this.discriminatorValue],
42
+ };
34
43
  }
35
44
  get ancestorObjectTypes() {
36
45
  return this.lazyAncestorObjectTypes();
@@ -44,39 +53,131 @@ export class ObjectType extends Type {
44
53
  },
45
54
  ];
46
55
  }
56
+ get declarationImports() {
57
+ if (this.extern) {
58
+ return [];
59
+ }
60
+ const imports = this.properties.flatMap((property) => property.declarationImports);
61
+ if (this.features.has("equals")) {
62
+ imports.push(Import.PURIFY_HELPERS);
63
+ }
64
+ if (this.features.has("fromJson") || this.features.has("jsonSchema")) {
65
+ imports.push(Import.ZOD);
66
+ }
67
+ if (this.features.has("jsonSchema")) {
68
+ imports.push(Import.ZOD_TO_JSON_SCHEMA);
69
+ }
70
+ if (this.features.has("fromRdf") || this.features.has("toRdf")) {
71
+ imports.push(Import.PURIFY);
72
+ imports.push(Import.PURIFY_HELPERS);
73
+ imports.push(Import.RDFJS_RESOURCE);
74
+ }
75
+ if (this.features.has("sparql")) {
76
+ imports.push(Import.SPARQLJS);
77
+ }
78
+ return imports;
79
+ }
80
+ get declarations() {
81
+ const declarations = [
82
+ ..._ObjectType.classDeclaration.bind(this)().toList(),
83
+ ..._ObjectType.interfaceDeclaration.bind(this)().toList(),
84
+ ];
85
+ const moduleStatements = [
86
+ ..._ObjectType.createFunctionDeclaration.bind(this)().toList(),
87
+ ..._ObjectType.equalsFunctionDeclaration.bind(this)().toList(),
88
+ ..._ObjectType.fromJsonFunctionDeclarations.bind(this)(),
89
+ ..._ObjectType.fromRdfFunctionDeclarations.bind(this)(),
90
+ ..._ObjectType.jsonSchemaFunctionDeclaration.bind(this)().toList(),
91
+ ..._ObjectType.jsonUiSchemaFunctionDeclaration.bind(this)().toList(),
92
+ ..._ObjectType.jsonZodSchemaFunctionDeclaration.bind(this)().toList(),
93
+ ..._ObjectType.hashFunctionDeclaration.bind(this)().toList(),
94
+ ..._ObjectType.sparqlFunctionDeclarations.bind(this)(),
95
+ ..._ObjectType.toJsonFunctionDeclaration.bind(this)().toList(),
96
+ ..._ObjectType.toRdfFunctionDeclaration.bind(this)().toList(),
97
+ ];
98
+ if (moduleStatements.length > 0) {
99
+ declarations.push({
100
+ isExported: this.export,
101
+ kind: StructureKind.Module,
102
+ name: this.name,
103
+ statements: moduleStatements,
104
+ });
105
+ }
106
+ return declarations;
107
+ }
47
108
  get descendantObjectTypes() {
48
109
  return this.lazyDescendantObjectTypes();
49
110
  }
50
111
  get discriminatorProperty() {
51
- return Maybe.of({
52
- name: this.configuration.objectTypeDiscriminatorPropertyName,
53
- type: "string",
54
- values: [this.discriminatorValue],
55
- });
112
+ return Maybe.of(this._discriminatorProperty);
56
113
  }
57
114
  get discriminatorValue() {
58
115
  return this.name;
59
116
  }
117
+ get equalsFunction() {
118
+ switch (this.declarationType) {
119
+ case "class":
120
+ return "purifyHelpers.Equatable.equals";
121
+ case "interface":
122
+ return `${this.name}.equals`;
123
+ default:
124
+ throw new RangeError(this.declarationType);
125
+ }
126
+ }
60
127
  get hashFunctionName() {
61
- if (this.lazyDescendantObjectTypes().length > 0 ||
62
- this.ancestorObjectTypes.length > 0) {
128
+ if (this.ancestorObjectTypes.length > 0 ||
129
+ this.descendantObjectTypes.length > 0)
63
130
  return `hash${this.name}`;
64
- }
65
131
  return "hash";
66
132
  }
67
- get importStatements() {
68
- const importStatements = this.properties.flatMap((property) => property.importStatements);
69
- this.mintingStrategy.ifJust((mintingStrategy) => {
70
- switch (mintingStrategy) {
71
- case MintingStrategy.SHA256:
72
- importStatements.push('import { sha256 } from "js-sha256";');
73
- break;
74
- case MintingStrategy.UUIDv4:
75
- importStatements.push('import * as uuid from "uuid";');
76
- break;
133
+ get identifierProperty() {
134
+ const identifierProperty = this.properties.find((property) => property instanceof ObjectType.IdentifierProperty);
135
+ invariant(identifierProperty);
136
+ return identifierProperty;
137
+ }
138
+ get identifierType() {
139
+ return this.identifierProperty.type;
140
+ }
141
+ get jsonName() {
142
+ if (this.features.has("toJson")) {
143
+ switch (this.declarationType) {
144
+ case "class":
145
+ return `ReturnType<${this.name}["toJson"]>`;
146
+ case "interface":
147
+ return `ReturnType<typeof ${this.name}.toJson>`;
148
+ default:
149
+ throw new RangeError(this.declarationType);
77
150
  }
78
- });
79
- return importStatements;
151
+ }
152
+ if (this.features.has("fromJson")) {
153
+ return `Parameters<typeof ${this.name}.fromJson>[0]`;
154
+ }
155
+ throw new RangeError("jsonName called when neither fromJson nor toJson features are enabled");
156
+ }
157
+ get jsonUiSchemaFunctionName() {
158
+ if (this.ancestorObjectTypes.length > 0 ||
159
+ this.descendantObjectTypes.length > 0)
160
+ return `${camelCase(this.name)}JsonUiSchema`;
161
+ return "jsonUiSchema";
162
+ }
163
+ get jsonZodSchemaFunctionName() {
164
+ if (this.ancestorObjectTypes.length > 0 ||
165
+ this.descendantObjectTypes.length > 0)
166
+ return `${camelCase(this.name)}JsonZodSchema`;
167
+ return "jsonZodSchema";
168
+ }
169
+ get mutable() {
170
+ return this.properties.some((property) => property.mutable);
171
+ }
172
+ get ownProperties() {
173
+ if (this.parentObjectTypes.length === 0) {
174
+ // Consider that a root of the object type hierarchy "owns" the identifier and type discriminator properties
175
+ // for all of its subtypes in the hierarchy.
176
+ invariant(this.properties.length >= 2, this.name);
177
+ return this.properties;
178
+ }
179
+ return this.properties.filter((property) => !(property instanceof IdentifierProperty) &&
180
+ !(property instanceof TypeDiscriminatorProperty));
80
181
  }
81
182
  get parentObjectTypes() {
82
183
  return this.lazyParentObjectTypes();
@@ -93,24 +194,45 @@ export class ObjectType extends Type {
93
194
  }
94
195
  return properties;
95
196
  }
96
- chainSparqlGraphPatternExpression({ variables, }) {
97
- return Maybe.of(new Type.SparqlGraphPatternsExpression(`new ${this.name}.SparqlGraphPatterns(${variables.subject})`));
197
+ get useImports() {
198
+ return this.imports;
98
199
  }
99
- equalsFunction() {
100
- switch (this.configuration.objectTypeDeclarationType) {
200
+ get thisVariable() {
201
+ switch (this.declarationType) {
101
202
  case "class":
102
- return "purifyHelpers.Equatable.equals";
203
+ return "this";
103
204
  case "interface":
104
- return `${this.name}.equals`;
205
+ return `_${camelCase(this.name)}`;
206
+ default:
207
+ throw new RangeError(this.declarationType);
105
208
  }
106
209
  }
210
+ fromJsonExpression({ variables, }) {
211
+ // Assumes the JSON object has been recursively validated already.
212
+ return `${this.name}.fromJson(${variables.value}).unsafeCoerce()`;
213
+ }
107
214
  fromRdfExpression({ variables, }) {
108
- return `${variables.resourceValues}.head().chain(value => value.to${this.rdfjsResourceType().named ? "Named" : ""}Resource()).chain(resource => ${this.name}.fromRdf(resource))`;
215
+ // Ignore the rdf:type if the instance of this type is the object of another property.
216
+ // Instead, assume the property has the correct range.
217
+ // This also accommodates the case where the object of a property is a dangling identifier that's not the
218
+ // subject of any statements.
219
+ return `${variables.resourceValues}.head().chain(value => value.to${this.rdfjsResourceType().named ? "Named" : ""}Resource()).chain(_resource => ${this.name}.fromRdf({ ...${variables.context}, ignoreRdfType: true, languageIn: ${variables.languageIn}, resource: _resource }))`;
109
220
  }
110
221
  hashStatements({ variables, }) {
111
- return [
112
- `${this.name}.${this.hashFunctionName}(${variables.value}, ${variables.hasher});`,
113
- ];
222
+ switch (this.declarationType) {
223
+ case "class":
224
+ return [`${variables.value}.hash(${variables.hasher});`];
225
+ case "interface":
226
+ return [
227
+ `${this.name}.${this.hashFunctionName}(${variables.value}, ${variables.hasher});`,
228
+ ];
229
+ }
230
+ }
231
+ jsonUiSchemaElement({ variables, }) {
232
+ return Maybe.of(`${this.name}.${this.jsonUiSchemaFunctionName}({ scopePrefix: ${variables.scopePrefix} })`);
233
+ }
234
+ jsonZodSchema(_parameters) {
235
+ return `${this.name}.${this.jsonZodSchemaFunctionName}()`;
114
236
  }
115
237
  rdfjsResourceType(options) {
116
238
  if (this.parentObjectTypes.length > 0) {
@@ -122,12 +244,48 @@ export class ObjectType extends Type {
122
244
  named: this.identifierType.isNamedNodeKind,
123
245
  };
124
246
  }
247
+ sparqlConstructTemplateTriples({ context, variables, }) {
248
+ switch (context) {
249
+ case "property":
250
+ return super.sparqlConstructTemplateTriples({ context, variables });
251
+ case "type":
252
+ return [
253
+ `...${this.name}.sparqlConstructTemplateTriples(${objectInitializer({
254
+ ignoreRdfType: true, // Can ignore the rdf:type when the object is nested
255
+ subject: variables.subject,
256
+ variablePrefix: variables.variablePrefix,
257
+ })})`,
258
+ ];
259
+ }
260
+ }
261
+ sparqlWherePatterns({ context, variables, }) {
262
+ switch (context) {
263
+ case "property":
264
+ return super.sparqlWherePatterns({ context, variables });
265
+ case "type":
266
+ return [
267
+ `...${this.name}.sparqlWherePatterns(${objectInitializer({
268
+ ignoreRdfType: true, // Can ignore the rdf:type when the object is nested
269
+ subject: variables.subject,
270
+ variablePrefix: variables.variablePrefix,
271
+ })})`,
272
+ ];
273
+ }
274
+ }
275
+ toJsonExpression({ variables, }) {
276
+ switch (this.declarationType) {
277
+ case "class":
278
+ return `${variables.value}.toJson()`;
279
+ case "interface":
280
+ return `${this.name}.toJson(${variables.value})`;
281
+ }
282
+ }
125
283
  toRdfExpression({ variables, }) {
126
- switch (this.configuration.objectTypeDeclarationType) {
284
+ switch (this.declarationType) {
127
285
  case "class":
128
- return `${variables.value}.toRdf({ mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }).identifier`;
286
+ return `${variables.value}.toRdf({ mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} })`;
129
287
  case "interface":
130
- return `${this.name}.toRdf(${variables.value}, { mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }).identifier`;
288
+ return `${this.name}.toRdf(${variables.value}, { mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} })`;
131
289
  }
132
290
  }
133
291
  ensureAtMostOneSuperObjectType() {
@@ -145,6 +303,21 @@ __decorate([
145
303
  __decorate([
146
304
  Memoize()
147
305
  ], ObjectType.prototype, "hashFunctionName", null);
306
+ __decorate([
307
+ Memoize()
308
+ ], ObjectType.prototype, "identifierProperty", null);
309
+ __decorate([
310
+ Memoize()
311
+ ], ObjectType.prototype, "identifierType", null);
312
+ __decorate([
313
+ Memoize()
314
+ ], ObjectType.prototype, "jsonUiSchemaFunctionName", null);
315
+ __decorate([
316
+ Memoize()
317
+ ], ObjectType.prototype, "jsonZodSchemaFunctionName", null);
318
+ __decorate([
319
+ Memoize()
320
+ ], ObjectType.prototype, "ownProperties", null);
148
321
  __decorate([
149
322
  Memoize()
150
323
  ], ObjectType.prototype, "parentObjectTypes", null);
@@ -0,0 +1,58 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { type ModuleDeclarationStructure, type TypeAliasDeclarationStructure } from "ts-morph";
3
+ import { DeclaredType } from "./DeclaredType.js";
4
+ import type { Import } from "./Import.js";
5
+ import type { ObjectType } from "./ObjectType.js";
6
+ import type { Type } from "./Type.js";
7
+ /**
8
+ * A union of object types, generated as a type alias
9
+ *
10
+ * type SomeUnion = Member1 | Member2 | ...
11
+ *
12
+ * with associated functions that switch on the type discriminator property and delegate to the appropriate
13
+ * member type code.
14
+ *
15
+ * It also generates SPARQL graph patterns that UNION the member object types.
16
+ */
17
+ export declare class ObjectUnionType extends DeclaredType {
18
+ readonly kind = "ObjectUnionType";
19
+ private readonly _discriminatorProperty;
20
+ private readonly comment;
21
+ private readonly label;
22
+ private readonly memberTypes;
23
+ constructor({ comment, label, memberTypes, ...superParameters }: ConstructorParameters<typeof DeclaredType>[0] & {
24
+ comment: Maybe<string>;
25
+ export_: boolean;
26
+ label: Maybe<string>;
27
+ memberTypes: readonly ObjectType[];
28
+ name: string;
29
+ });
30
+ get conversions(): readonly Type.Conversion[];
31
+ get declarationImports(): readonly Import[];
32
+ get declarations(): (ModuleDeclarationStructure | TypeAliasDeclarationStructure)[];
33
+ get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
34
+ get equalsFunction(): string;
35
+ get jsonName(): string;
36
+ get mutable(): boolean;
37
+ get useImports(): readonly Import[];
38
+ protected get thisVariable(): string;
39
+ private get equalsFunctionDeclaration();
40
+ private get fromJsonFunctionDeclaration();
41
+ private get fromRdfFunctionDeclaration();
42
+ private get hashFunctionDeclaration();
43
+ private get jsonZodSchemaFunctionDeclaration();
44
+ private get sparqlFunctionDeclarations();
45
+ private get toJsonFunctionDeclaration();
46
+ private get toRdfFunctionDeclaration();
47
+ private get typeAliasDeclaration();
48
+ fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
49
+ fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
50
+ hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
51
+ jsonZodSchema(): ReturnType<Type["jsonZodSchema"]>;
52
+ sparqlConstructTemplateTriples({ context, variables, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
53
+ sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
54
+ toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
55
+ toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
56
+ private rdfjsResourceType;
57
+ }
58
+ //# sourceMappingURL=ObjectUnionType.d.ts.map