@shaclmate/compiler 2.0.14 → 2.0.16

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 (67) hide show
  1. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +22 -7
  2. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +5 -3
  3. package/ast/ListType.d.ts +2 -2
  4. package/ast/ObjectType.d.ts +18 -12
  5. package/enums/IdentifierMintingStrategy.d.ts +5 -0
  6. package/enums/IdentifierMintingStrategy.js +2 -0
  7. package/enums/index.d.ts +1 -1
  8. package/enums/index.js +1 -1
  9. package/generators/json/AstJsonGenerator.js +5 -1
  10. package/generators/ts/DateTimeType.d.ts +3 -1
  11. package/generators/ts/DateTimeType.js +9 -1
  12. package/generators/ts/IdentifierType.d.ts +2 -0
  13. package/generators/ts/IdentifierType.js +11 -0
  14. package/generators/ts/Import.d.ts +0 -1
  15. package/generators/ts/Import.js +0 -5
  16. package/generators/ts/ListType.d.ts +4 -3
  17. package/generators/ts/ListType.js +27 -16
  18. package/generators/ts/ObjectType.d.ts +5 -4
  19. package/generators/ts/ObjectType.js +21 -9
  20. package/generators/ts/ObjectUnionType.d.ts +1 -1
  21. package/generators/ts/ObjectUnionType.js +22 -52
  22. package/generators/ts/OptionType.d.ts +3 -1
  23. package/generators/ts/OptionType.js +12 -11
  24. package/generators/ts/PrimitiveType.d.ts +2 -2
  25. package/generators/ts/PrimitiveType.js +9 -4
  26. package/generators/ts/SetType.d.ts +3 -1
  27. package/generators/ts/SetType.js +12 -8
  28. package/generators/ts/SnippetDeclarations.d.ts +11 -0
  29. package/generators/ts/SnippetDeclarations.js +215 -0
  30. package/generators/ts/TermType.d.ts +3 -1
  31. package/generators/ts/TermType.js +16 -8
  32. package/generators/ts/TsGenerator.js +15 -1
  33. package/generators/ts/Type.d.ts +15 -5
  34. package/generators/ts/Type.js +17 -0
  35. package/generators/ts/TypeFactory.js +18 -8
  36. package/generators/ts/UnionType.d.ts +2 -1
  37. package/generators/ts/UnionType.js +3 -3
  38. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +7 -8
  39. package/generators/ts/_ObjectType/IdentifierProperty.js +99 -25
  40. package/generators/ts/_ObjectType/Property.d.ts +17 -3
  41. package/generators/ts/_ObjectType/Property.js +2 -1
  42. package/generators/ts/_ObjectType/ShaclProperty.d.ts +1 -0
  43. package/generators/ts/_ObjectType/ShaclProperty.js +4 -1
  44. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +3 -5
  45. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +11 -4
  46. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +1 -1
  47. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +1 -1
  48. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +1 -1
  49. package/generators/ts/_ObjectType/fromRdfTypeVariableStatement.d.ts +5 -0
  50. package/generators/ts/_ObjectType/fromRdfTypeVariableStatement.js +25 -0
  51. package/generators/ts/_ObjectType/index.d.ts +1 -0
  52. package/generators/ts/_ObjectType/index.js +1 -0
  53. package/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.d.ts +5 -0
  54. package/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +21 -0
  55. package/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.d.ts +5 -0
  56. package/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +20 -0
  57. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +35 -58
  58. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +1 -1
  59. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +3 -3
  60. package/input/NodeShape.d.ts +2 -2
  61. package/input/generated.d.ts +24 -16
  62. package/input/generated.js +53 -8
  63. package/input/tsFeatures.d.ts +1 -1
  64. package/input/tsFeatures.js +21 -13
  65. package/package.json +4 -4
  66. package/enums/MintingStrategy.d.ts +0 -5
  67. package/enums/MintingStrategy.js +0 -2
@@ -1,7 +1,7 @@
1
1
  import { rdf } from "@tpluscode/rdf-ns-builders";
2
- import { Either, Left } from "purify-ts";
2
+ import { Either, Left, Maybe } from "purify-ts";
3
3
  import { invariant } from "ts-invariant";
4
- import { TsFeature } from "../enums/TsFeature.js";
4
+ import { TsFeature } from "../enums/index.js";
5
5
  import * as input from "../input/index.js";
6
6
  import { logger } from "../logger.js";
7
7
  import { pickLiteral } from "./pickLiteral.js";
@@ -106,12 +106,26 @@ export function transformNodeShapeToAstType(nodeShape) {
106
106
  tsFeatures: nodeShape.tsFeatures.orDefault(new Set(TsFeature.MEMBERS)),
107
107
  };
108
108
  this.nodeShapeAstTypesByIdentifier.set(nodeShape.identifier, compositeType);
109
- compositeType.memberTypes.push(...Either.rights(compositeTypeNodeShapes.map((nodeShape) => this.transformNodeShapeToAstType(nodeShape))).filter((nodeShapeAstType) => nodeShapeAstType.kind === "ObjectType"));
110
- if (compositeType.memberTypes.length < compositeTypeNodeShapes.length) {
111
- return Left(new Error(`${nodeShape} has one or more non-ObjectType node shapes in its logical constraint`));
109
+ for (const memberNodeShape of compositeTypeNodeShapes) {
110
+ const memberAstTypeEither = this.transformNodeShapeToAstType(memberNodeShape);
111
+ if (memberAstTypeEither.isLeft()) {
112
+ return memberAstTypeEither;
113
+ }
114
+ const memberAstType = memberAstTypeEither.unsafeCoerce();
115
+ switch (memberAstType.kind) {
116
+ case "ObjectType":
117
+ compositeType.memberTypes.push(memberAstType);
118
+ break;
119
+ case "ObjectUnionType":
120
+ compositeType.memberTypes.push(...memberAstType.memberTypes);
121
+ break;
122
+ default:
123
+ return Left(new Error(`${nodeShape} has one or more non-ObjectType node shapes in its logical constraint`));
124
+ }
112
125
  }
113
126
  return Either.of(compositeType);
114
127
  }
128
+ const identifierIn = nodeShape.constraints.in_.filter((term) => term.termType === "NamedNode");
115
129
  // Put a placeholder in the cache to deal with cyclic references
116
130
  // If this node shape's properties (directly or indirectly) refer to the node shape itself,
117
131
  // we'll return this placeholder.
@@ -126,9 +140,10 @@ export function transformNodeShapeToAstType(nodeShape) {
126
140
  fromRdfType: nodeShape.fromRdfType,
127
141
  label: pickLiteral(nodeShape.labels).map((literal) => literal.value),
128
142
  kind: "ObjectType",
129
- mintingStrategy: nodeShape.mintingStrategy,
143
+ identifierIn,
144
+ identifierMintingStrategy: identifierIn.length === 0 ? nodeShape.mintingStrategy : Maybe.empty(),
145
+ identifierKinds: identifierIn.length === 0 ? nodeShape.nodeKinds : new Set(["NamedNode"]),
130
146
  name: this.shapeAstName(nodeShape),
131
- nodeKinds: nodeShape.nodeKinds,
132
147
  properties: [], // This is mutable, we'll populate it below.
133
148
  parentObjectTypes: [], // This is mutable, we'll populate it below
134
149
  toRdfTypes: nodeShape.toRdfTypes,
@@ -27,13 +27,13 @@ export function transformPropertyShapeToAstCompositeType(shape, inherited) {
27
27
  nodeKinds.add(astType.identifierNodeKind);
28
28
  break;
29
29
  case "ObjectType":
30
- nodeKinds = astType.nodeKinds;
30
+ nodeKinds = astType.identifierKinds;
31
31
  break;
32
32
  case "ObjectIntersectionType":
33
33
  case "ObjectUnionType":
34
34
  nodeKinds = new Set();
35
35
  for (const memberType of astType.memberTypes) {
36
- for (const nodeKind of memberType.nodeKinds) {
36
+ for (const nodeKind of memberType.identifierKinds) {
37
37
  nodeKinds.add(nodeKind);
38
38
  }
39
39
  }
@@ -95,7 +95,9 @@ export function transformPropertyShapeToAstCompositeType(shape, inherited) {
95
95
  invariant(memberTypeEithers.length > 0);
96
96
  const memberTypes = Either.rights(memberTypeEithers);
97
97
  if (memberTypes.length !== memberTypeEithers.length) {
98
- logger.warn("shape %s composition did not map all member types successfully", shape);
98
+ logger.warn("shape %s composition did not map all member types successfully: %s", shape, Either.lefts(memberTypeEithers)
99
+ .map((left) => left.message)
100
+ .join("; "));
99
101
  return memberTypeEithers[0];
100
102
  }
101
103
  invariant(memberTypes.length > 0);
package/ast/ListType.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { NamedNode } from "@rdfjs/types";
2
2
  import type { Maybe } from "purify-ts";
3
- import type { MintingStrategy } from "../enums/MintingStrategy.js";
3
+ import type { IdentifierMintingStrategy } from "../enums/IdentifierMintingStrategy.js";
4
4
  import type { Name } from "./Name.js";
5
5
  import type { Type } from "./Type.js";
6
6
  /**
@@ -33,7 +33,7 @@ export interface ListType {
33
33
  /**
34
34
  * Strategy for minting new list and sub-list identifiers.
35
35
  */
36
- readonly mintingStrategy: Maybe<MintingStrategy>;
36
+ readonly mintingStrategy: Maybe<IdentifierMintingStrategy>;
37
37
  /**
38
38
  * The list should be mutable in generated code.
39
39
  */
@@ -1,7 +1,7 @@
1
1
  import type { NamedNode } from "@rdfjs/types";
2
- import type { PredicatePath } from "@shaclmate/shacl-ast";
2
+ import type { NodeKind, PredicatePath } from "@shaclmate/shacl-ast";
3
3
  import type { Maybe } from "purify-ts";
4
- import type { MintingStrategy, PropertyVisibility, TsFeature, TsObjectDeclarationType } from "../enums/index.js";
4
+ import type { IdentifierMintingStrategy, PropertyVisibility, TsFeature, TsObjectDeclarationType } from "../enums/index.js";
5
5
  import type { Name } from "./Name.js";
6
6
  import type { Type } from "./Type.js";
7
7
  export interface ObjectType {
@@ -52,6 +52,22 @@ export interface ObjectType {
52
52
  * class targets).
53
53
  */
54
54
  readonly fromRdfType: Maybe<NamedNode>;
55
+ /**
56
+ * Instances of this ObjectType must have explicit identifiers and the identifiers must be in this list of IRIs.
57
+ *
58
+ * Mutually exclusive with minting strategies
59
+ */
60
+ readonly identifierIn: readonly NamedNode[];
61
+ /**
62
+ * The RDF node kinds this ObjectType may be identified by.
63
+ *
64
+ * Used to associate instances with an RDF identifier.
65
+ */
66
+ readonly identifierKinds: Set<Exclude<NodeKind, "Literal">>;
67
+ /**
68
+ * Strategy for minting new object identifiers.
69
+ */
70
+ readonly identifierMintingStrategy: Maybe<IdentifierMintingStrategy>;
55
71
  /**
56
72
  * Type discriminator.
57
73
  */
@@ -60,20 +76,10 @@ export interface ObjectType {
60
76
  * Human-readable label from rdfs:label.
61
77
  */
62
78
  readonly label: Maybe<string>;
63
- /**
64
- * Strategy for minting new object identifiers.
65
- */
66
- readonly mintingStrategy: Maybe<MintingStrategy>;
67
79
  /**
68
80
  * Name of this type, usually derived from sh:name or shaclmate:name.
69
81
  */
70
82
  readonly name: Name;
71
- /**
72
- * The RDF node kinds this ObjectType may be identified by.
73
- *
74
- * Used to associate instances with an RDF identifier.
75
- */
76
- readonly nodeKinds: Set<"BlankNode" | "NamedNode">;
77
83
  /**
78
84
  * Immediate parent ObjectTypes of this Object types.
79
85
  *
@@ -0,0 +1,5 @@
1
+ /**
2
+ * TypeScript enum corresponding to shaclmate:mintingStrategy, for simpler manipulation.
3
+ */
4
+ export type IdentifierMintingStrategy = "sha256" | "uuidv4";
5
+ //# sourceMappingURL=IdentifierMintingStrategy.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=IdentifierMintingStrategy.js.map
package/enums/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./MintingStrategy.js";
1
+ export * from "./IdentifierMintingStrategy.js";
2
2
  export * from "./PropertyVisibility.js";
3
3
  export * from "./TsFeature.js";
4
4
  export * from "./TsObjectDeclarationType.js";
package/enums/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./MintingStrategy.js";
1
+ export * from "./IdentifierMintingStrategy.js";
2
2
  export * from "./PropertyVisibility.js";
3
3
  export * from "./TsFeature.js";
4
4
  export * from "./TsObjectDeclarationType.js";
@@ -70,7 +70,11 @@ function typeToJson(type) {
70
70
  parentObjectTypes: type.parentObjectTypes.length > 0
71
71
  ? type.parentObjectTypes.map((type) => nameToJson(type.name))
72
72
  : undefined,
73
- nodeKinds: [...type.nodeKinds],
73
+ identifierIn: type.identifierIn.length > 0
74
+ ? type.identifierIn.map(termToJson)
75
+ : undefined,
76
+ identifierKinds: [...type.identifierKinds],
77
+ identifierMintingStrategy: type.identifierMintingStrategy.extract(),
74
78
  toRdfTypes: type.toRdfTypes.length > 0
75
79
  ? type.toRdfTypes.map(termToJson)
76
80
  : undefined,
@@ -1,7 +1,8 @@
1
+ import type { TsFeature } from "../../enums/index.js";
1
2
  import { PrimitiveType } from "./PrimitiveType.js";
2
3
  import type { Type } from "./Type.js";
3
4
  export declare class DateTimeType extends PrimitiveType<Date> {
4
- readonly equalsFunction = "(left, right) => purifyHelpers.Equatable.EqualsResult.fromBooleanEqualsResult(left, right, left.getTime() === right.getTime())";
5
+ readonly equalsFunction = "dateEquals";
5
6
  readonly kind = "DateTimeType";
6
7
  readonly mutable = true;
7
8
  get conversions(): readonly Type.Conversion[];
@@ -11,6 +12,7 @@ export declare class DateTimeType extends PrimitiveType<Date> {
11
12
  hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
12
13
  jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
13
14
  propertyFromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<number>["propertyFromRdfResourceValueExpression"]>[0]): string;
15
+ snippetDeclarations(features: Set<TsFeature>): readonly string[];
14
16
  toJsonExpression({ variables, }: Parameters<PrimitiveType<Date>["toJsonExpression"]>[0]): string;
15
17
  toRdfExpression({ variables, }: Parameters<PrimitiveType<Date>["toRdfExpression"]>[0]): string;
16
18
  }
@@ -1,9 +1,10 @@
1
1
  import { PrimitiveType } from "./PrimitiveType.js";
2
+ import { SnippetDeclarations } from "./SnippetDeclarations.js";
2
3
  import { objectInitializer } from "./objectInitializer.js";
3
4
  export class DateTimeType extends PrimitiveType {
4
5
  constructor() {
5
6
  super(...arguments);
6
- this.equalsFunction = "(left, right) => purifyHelpers.Equatable.EqualsResult.fromBooleanEqualsResult(left, right, left.getTime() === right.getTime())";
7
+ this.equalsFunction = "dateEquals";
7
8
  this.kind = "DateTimeType";
8
9
  this.mutable = true;
9
10
  }
@@ -46,6 +47,13 @@ export class DateTimeType extends PrimitiveType {
46
47
  }
47
48
  return expression;
48
49
  }
50
+ snippetDeclarations(features) {
51
+ const snippetDeclarations = [];
52
+ if (features.has("equals")) {
53
+ snippetDeclarations.push(SnippetDeclarations.dateEquals);
54
+ }
55
+ return snippetDeclarations;
56
+ }
49
57
  toJsonExpression({ variables, }) {
50
58
  return `${variables.value}.toISOString()`;
51
59
  }
@@ -1,7 +1,9 @@
1
1
  import type { BlankNode, NamedNode } from "@rdfjs/types";
2
2
  import { TermType } from "./TermType.js";
3
+ import type { Type } from "./Type.js";
3
4
  export declare class IdentifierType extends TermType<BlankNode | NamedNode> {
4
5
  readonly kind = "IdentifierType";
6
+ get conversions(): readonly Type.Conversion[];
5
7
  get isNamedNodeKind(): boolean;
6
8
  get jsonName(): string;
7
9
  get name(): string;
@@ -11,6 +11,17 @@ export class IdentifierType extends TermType {
11
11
  super(...arguments);
12
12
  this.kind = "IdentifierType";
13
13
  }
14
+ get conversions() {
15
+ return super.conversions.concat([
16
+ {
17
+ conversionExpression: (value) => `${this.dataFactoryVariable}.namedNode(${value})`,
18
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "string"`,
19
+ sourceTypeName: this.in_.length > 0
20
+ ? this.in_.map((iri) => `"${iri.value}"`).join(" | ")
21
+ : "string",
22
+ },
23
+ ]);
24
+ }
14
25
  get isNamedNodeKind() {
15
26
  return this.nodeKinds.size === 1 && this.nodeKinds.has("NamedNode");
16
27
  }
@@ -5,7 +5,6 @@ export type Import = ImportDeclarationStructure | string;
5
5
  */
6
6
  export declare namespace Import {
7
7
  const PURIFY: Import;
8
- const PURIFY_HELPERS: Import;
9
8
  const RDF_LITERAL: Import;
10
9
  const RDFJS_RESOURCE: Import;
11
10
  const RDFJS_TYPES: Import;
@@ -9,11 +9,6 @@ export var Import;
9
9
  moduleSpecifier: "purify-ts",
10
10
  namespaceImport: "purify",
11
11
  };
12
- Import.PURIFY_HELPERS = {
13
- kind: StructureKind.ImportDeclaration,
14
- moduleSpecifier: "purify-ts-helpers",
15
- namespaceImport: "purifyHelpers",
16
- };
17
12
  Import.RDF_LITERAL = {
18
13
  kind: StructureKind.ImportDeclaration,
19
14
  moduleSpecifier: "rdf-literal",
@@ -1,6 +1,6 @@
1
1
  import type { NamedNode } from "@rdfjs/types";
2
2
  import { Maybe } from "purify-ts";
3
- import type { MintingStrategy } from "../../enums/index.js";
3
+ import type { IdentifierMintingStrategy, TsFeature } from "../../enums/index.js";
4
4
  import { Import } from "./Import.js";
5
5
  import { Type } from "./Type.js";
6
6
  export declare class ListType extends Type {
@@ -13,7 +13,7 @@ export declare class ListType extends Type {
13
13
  constructor({ identifierNodeKind, itemType, mintingStrategy, mutable, toRdfTypes, ...superParameters }: {
14
14
  identifierNodeKind: ListType["identifierNodeKind"];
15
15
  itemType: Type;
16
- mintingStrategy: Maybe<MintingStrategy>;
16
+ mintingStrategy: Maybe<IdentifierMintingStrategy>;
17
17
  mutable: boolean;
18
18
  toRdfTypes: readonly NamedNode[];
19
19
  } & ConstructorParameters<typeof Type>[0]);
@@ -22,15 +22,16 @@ export declare class ListType extends Type {
22
22
  get equalsFunction(): string;
23
23
  get jsonName(): string;
24
24
  get name(): string;
25
- get useImports(): readonly Import[];
26
25
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
27
26
  fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
28
27
  hashStatements({ depth, variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
29
28
  jsonUiSchemaElement(parameters: Parameters<Type["jsonUiSchemaElement"]>[0]): ReturnType<Type["jsonUiSchemaElement"]>;
30
29
  jsonZodSchema(parameters: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
30
+ snippetDeclarations(features: Set<TsFeature>): readonly string[];
31
31
  sparqlConstructTemplateTriples({ variables, context, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
32
32
  sparqlWherePatterns({ variables, context, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
33
33
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
34
34
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
35
+ useImports(features: Set<TsFeature>): readonly Import[];
35
36
  }
36
37
  //# sourceMappingURL=ListType.d.ts.map
@@ -1,6 +1,7 @@
1
1
  import { rdf } from "@tpluscode/rdf-ns-builders";
2
2
  import { Maybe } from "purify-ts";
3
3
  import { Import } from "./Import.js";
4
+ import { SnippetDeclarations } from "./SnippetDeclarations.js";
4
5
  import { Type } from "./Type.js";
5
6
  import { objectInitializer } from "./objectInitializer.js";
6
7
  export class ListType extends Type {
@@ -26,7 +27,7 @@ export class ListType extends Type {
26
27
  return Maybe.empty();
27
28
  }
28
29
  get equalsFunction() {
29
- return `((left, right) => purifyHelpers.Arrays.equals(left, right, ${this.itemType.equalsFunction}))`;
30
+ return `((left, right) => arrayEquals(left, right, ${this.itemType.equalsFunction}))`;
30
31
  }
31
32
  get jsonName() {
32
33
  return `readonly (${this.itemType.jsonName})[]`;
@@ -34,13 +35,6 @@ export class ListType extends Type {
34
35
  get name() {
35
36
  return `${this.mutable ? "" : "readonly "}${this.itemType.name}[]`;
36
37
  }
37
- get useImports() {
38
- const imports = this.itemType.useImports.concat();
39
- if (this.identifierNodeKind === "NamedNode") {
40
- imports.push(Import.SHA256);
41
- }
42
- return imports;
43
- }
44
38
  fromJsonExpression({ variables, }) {
45
39
  return `${variables.value}.map(_item => (${this.itemType.fromJsonExpression({ variables: { value: "_item" } })}))`;
46
40
  }
@@ -62,6 +56,13 @@ export class ListType extends Type {
62
56
  jsonZodSchema(parameters) {
63
57
  return `${this.itemType.jsonZodSchema(parameters)}.array()`;
64
58
  }
59
+ snippetDeclarations(features) {
60
+ const snippetDeclarations = [];
61
+ if (features.has("equals")) {
62
+ snippetDeclarations.push(SnippetDeclarations.arrayEquals);
63
+ }
64
+ return snippetDeclarations;
65
+ }
65
66
  sparqlConstructTemplateTriples({ variables, context, }) {
66
67
  switch (context) {
67
68
  case "property":
@@ -123,6 +124,10 @@ export class ListType extends Type {
123
124
  }
124
125
  }
125
126
  sparqlWherePatterns({ variables, context, }) {
127
+ // Need to handle two cases:
128
+ // (1) (?s, ?p, ?list) where ?list binds to rdf:nil
129
+ // (2) (?s, ?p, ?list) (?list, rdf:first, "element") (?list, rdf:rest, rdf:nil) etc. where list binds to the head of a list
130
+ // Case (2) is case (1) with OPTIONAL graph patterns to handle actual list elements.
126
131
  switch (context) {
127
132
  case "property":
128
133
  return super.sparqlWherePatterns({ context, variables });
@@ -170,7 +175,7 @@ export class ListType extends Type {
170
175
  subject: restNVariable,
171
176
  predicate: this.rdfjsTermExpression(rdf.first),
172
177
  object: itemNVariable,
173
- })}] }`, ...this.itemType.sparqlConstructTemplateTriples({
178
+ })}] }`, ...this.itemType.sparqlWherePatterns({
174
179
  context: "type",
175
180
  variables: {
176
181
  subject: itemNVariable,
@@ -185,7 +190,8 @@ export class ListType extends Type {
185
190
  object: variable("RestNBasic"),
186
191
  })}] }`);
187
192
  patterns.push(`{ type: "optional", patterns: [${optionalPatterns.join(", ")}] }`);
188
- return patterns;
193
+ // Having an optional around everything handles the rdf:nil case
194
+ return [`{ type: "optional", patterns: [${patterns.join(", ")}] }`];
189
195
  }
190
196
  }
191
197
  }
@@ -235,12 +241,11 @@ export class ListType extends Type {
235
241
  break;
236
242
  }
237
243
  }
238
- return `${variables.value}.reduce(({ currentSubListResource, listResource }, item, itemIndex, list) => {
244
+ return `${variables.value}.length > 0 ? ${variables.value}.reduce(({ currentSubListResource, listResource }, item, itemIndex, list) => {
239
245
  if (itemIndex === 0) {
240
246
  currentSubListResource = listResource;
241
247
  } else {
242
- const newSubListResource = ${variables.resourceSet}.${resourceSetMethodName}(${objectInitializer({
243
- identifier: subListIdentifier,
248
+ const newSubListResource = ${variables.resourceSet}.${resourceSetMethodName}(${subListIdentifier}, ${objectInitializer({
244
249
  mutateGraph: variables.mutateGraph,
245
250
  })});
246
251
  currentSubListResource!.add(dataFactory.namedNode("${rdf.rest.value}"), newSubListResource.identifier);
@@ -259,15 +264,21 @@ export class ListType extends Type {
259
264
  },
260
265
  {
261
266
  currentSubListResource: null,
262
- listResource: resourceSet.${resourceSetMethodName}(${objectInitializer({
263
- identifier: listIdentifier,
267
+ listResource: resourceSet.${resourceSetMethodName}(${listIdentifier}, ${objectInitializer({
264
268
  mutateGraph: variables.mutateGraph,
265
269
  })}),
266
270
  } as {
267
271
  currentSubListResource: ${mutableResourceTypeName} | null;
268
272
  listResource: ${mutableResourceTypeName};
269
273
  },
270
- ).listResource.identifier`;
274
+ ).listResource.identifier : dataFactory.namedNode("${rdf.nil.value}")`;
275
+ }
276
+ useImports(features) {
277
+ const imports = this.itemType.useImports(features).concat();
278
+ if (features.has("hash") && this.identifierNodeKind === "NamedNode") {
279
+ imports.push(Import.SHA256);
280
+ }
281
+ return imports;
271
282
  }
272
283
  }
273
284
  //# sourceMappingURL=ListType.js.map
@@ -1,7 +1,7 @@
1
1
  import type { NamedNode } from "@rdfjs/types";
2
2
  import { Maybe } from "purify-ts";
3
3
  import { type ClassDeclarationStructure, type InterfaceDeclarationStructure, type ModuleDeclarationStructure } from "ts-morph";
4
- import type { MintingStrategy, TsObjectDeclarationType } from "../../enums/index.js";
4
+ import type { IdentifierMintingStrategy, TsFeature, TsObjectDeclarationType } from "../../enums/index.js";
5
5
  import { DeclaredType } from "./DeclaredType.js";
6
6
  import type { IdentifierType } from "./IdentifierType.js";
7
7
  import { Import } from "./Import.js";
@@ -15,7 +15,7 @@ export declare class ObjectType extends DeclaredType {
15
15
  protected readonly extern: boolean;
16
16
  protected readonly fromRdfType: Maybe<NamedNode>;
17
17
  protected readonly label: Maybe<string>;
18
- protected readonly mintingStrategy: Maybe<MintingStrategy>;
18
+ protected readonly mintingStrategy: Maybe<IdentifierMintingStrategy>;
19
19
  protected readonly toRdfTypes: readonly NamedNode[];
20
20
  private readonly imports;
21
21
  private readonly lazyAncestorObjectTypes;
@@ -34,7 +34,7 @@ export declare class ObjectType extends DeclaredType {
34
34
  lazyDescendantObjectTypes: () => readonly ObjectType[];
35
35
  lazyParentObjectTypes: () => readonly ObjectType[];
36
36
  lazyProperties: () => readonly ObjectType.Property[];
37
- mintingStrategy: Maybe<MintingStrategy>;
37
+ mintingStrategy: Maybe<IdentifierMintingStrategy>;
38
38
  toRdfTypes: readonly NamedNode[];
39
39
  } & ConstructorParameters<typeof DeclaredType>[0]);
40
40
  get _discriminatorProperty(): Type.DiscriminatorProperty;
@@ -56,7 +56,6 @@ export declare class ObjectType extends DeclaredType {
56
56
  get ownProperties(): readonly ObjectType.Property[];
57
57
  get parentObjectTypes(): readonly ObjectType[];
58
58
  get properties(): readonly ObjectType.Property[];
59
- get useImports(): readonly Import[];
60
59
  protected get thisVariable(): string;
61
60
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
62
61
  fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
@@ -74,10 +73,12 @@ export declare class ObjectType extends DeclaredType {
74
73
  readonly name: string;
75
74
  readonly named: boolean;
76
75
  };
76
+ snippetDeclarations(): readonly string[];
77
77
  sparqlConstructTemplateTriples({ context, variables, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
78
78
  sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
79
79
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
80
80
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
81
+ useImports(_features: Set<TsFeature>): readonly Import[];
81
82
  protected ensureAtMostOneSuperObjectType(): void;
82
83
  }
83
84
  export declare namespace ObjectType {
@@ -11,6 +11,7 @@ import { StructureKind, } from "ts-morph";
11
11
  import { Memoize } from "typescript-memoize";
12
12
  import { DeclaredType } from "./DeclaredType.js";
13
13
  import { Import } from "./Import.js";
14
+ import { SnippetDeclarations } from "./SnippetDeclarations.js";
14
15
  import * as _ObjectType from "./_ObjectType/index.js";
15
16
  import { IdentifierProperty, TypeDiscriminatorProperty, } from "./_ObjectType/index.js";
16
17
  import { objectInitializer } from "./objectInitializer.js";
@@ -58,9 +59,6 @@ export class ObjectType extends DeclaredType {
58
59
  return [];
59
60
  }
60
61
  const imports = this.properties.flatMap((property) => property.declarationImports);
61
- if (this.features.has("equals")) {
62
- imports.push(Import.PURIFY_HELPERS);
63
- }
64
62
  if (this.features.has("fromJson") || this.features.has("jsonSchema")) {
65
63
  imports.push(Import.ZOD);
66
64
  }
@@ -69,7 +67,6 @@ export class ObjectType extends DeclaredType {
69
67
  }
70
68
  if (this.features.has("fromRdf") || this.features.has("toRdf")) {
71
69
  imports.push(Import.PURIFY);
72
- imports.push(Import.PURIFY_HELPERS);
73
70
  imports.push(Import.RDFJS_RESOURCE);
74
71
  }
75
72
  if (this.features.has("sparql")) {
@@ -87,6 +84,7 @@ export class ObjectType extends DeclaredType {
87
84
  ..._ObjectType.equalsFunctionDeclaration.bind(this)().toList(),
88
85
  ..._ObjectType.fromJsonFunctionDeclarations.bind(this)(),
89
86
  ..._ObjectType.fromRdfFunctionDeclarations.bind(this)(),
87
+ ..._ObjectType.fromRdfTypeVariableDeclaration.bind(this)().toList(),
90
88
  ..._ObjectType.jsonSchemaFunctionDeclaration.bind(this)().toList(),
91
89
  ..._ObjectType.jsonUiSchemaFunctionDeclaration.bind(this)().toList(),
92
90
  ..._ObjectType.jsonZodSchemaFunctionDeclaration.bind(this)().toList(),
@@ -117,7 +115,7 @@ export class ObjectType extends DeclaredType {
117
115
  get equalsFunction() {
118
116
  switch (this.declarationType) {
119
117
  case "class":
120
- return "purifyHelpers.Equatable.equals";
118
+ return "((left, right) => left.equals(right))";
121
119
  case "interface":
122
120
  return `${this.name}.equals`;
123
121
  default:
@@ -152,7 +150,7 @@ export class ObjectType extends DeclaredType {
152
150
  if (this.features.has("fromJson")) {
153
151
  return `Parameters<typeof ${this.name}.fromJson>[0]`;
154
152
  }
155
- throw new RangeError("jsonName called when neither fromJson nor toJson features are enabled");
153
+ throw new RangeError(`${this.name}: jsonName called when neither fromJson nor toJson features are enabled`);
156
154
  }
157
155
  get jsonUiSchemaFunctionName() {
158
156
  if (this.ancestorObjectTypes.length > 0 ||
@@ -194,9 +192,6 @@ export class ObjectType extends DeclaredType {
194
192
  }
195
193
  return properties;
196
194
  }
197
- get useImports() {
198
- return this.imports;
199
- }
200
195
  get thisVariable() {
201
196
  switch (this.declarationType) {
202
197
  case "class":
@@ -244,6 +239,20 @@ export class ObjectType extends DeclaredType {
244
239
  named: this.identifierType.isNamedNodeKind,
245
240
  };
246
241
  }
242
+ snippetDeclarations() {
243
+ const snippetDeclarations = [];
244
+ if (this.features.has("equals")) {
245
+ snippetDeclarations.push(SnippetDeclarations.EqualsResult);
246
+ }
247
+ if ((this.features.has("fromJson") || this.features.has("fromRdf")) &&
248
+ this.parentObjectTypes.length > 0) {
249
+ snippetDeclarations.push(SnippetDeclarations.UnwrapR);
250
+ }
251
+ for (const property of this.ownProperties) {
252
+ snippetDeclarations.push(...property.snippetDeclarations);
253
+ }
254
+ return snippetDeclarations;
255
+ }
247
256
  sparqlConstructTemplateTriples({ context, variables, }) {
248
257
  switch (context) {
249
258
  case "property":
@@ -288,6 +297,9 @@ export class ObjectType extends DeclaredType {
288
297
  return `${this.name}.toRdf(${variables.value}, { mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} })`;
289
298
  }
290
299
  }
300
+ useImports(_features) {
301
+ return this.imports;
302
+ }
291
303
  ensureAtMostOneSuperObjectType() {
292
304
  if (this.parentObjectTypes.length > 1) {
293
305
  throw new RangeError(`object type '${this.name}' has multiple super object types`);
@@ -34,7 +34,6 @@ export declare class ObjectUnionType extends DeclaredType {
34
34
  get equalsFunction(): string;
35
35
  get jsonName(): string;
36
36
  get mutable(): boolean;
37
- get useImports(): readonly Import[];
38
37
  protected get thisVariable(): string;
39
38
  private get equalsFunctionDeclaration();
40
39
  private get fromJsonFunctionDeclaration();
@@ -53,6 +52,7 @@ export declare class ObjectUnionType extends DeclaredType {
53
52
  sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
54
53
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
55
54
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
55
+ useImports(): readonly Import[];
56
56
  private rdfjsResourceType;
57
57
  }
58
58
  //# sourceMappingURL=ObjectUnionType.d.ts.map