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