@shaclmate/compiler 2.0.17 → 2.0.19

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 (48) hide show
  1. package/dist/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +10 -0
  2. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +1 -0
  3. package/dist/ast/ObjectType.d.ts +8 -0
  4. package/dist/enums/IdentifierMintingStrategy.d.ts +1 -1
  5. package/dist/generators/ts/ListType.js +3 -2
  6. package/dist/generators/ts/ObjectType.d.ts +4 -0
  7. package/dist/generators/ts/ObjectType.js +46 -7
  8. package/dist/generators/ts/ObjectUnionType.js +1 -1
  9. package/dist/generators/ts/TypeFactory.js +84 -35
  10. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.d.ts +34 -0
  11. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.js +98 -0
  12. package/dist/generators/ts/_ObjectType/IdentifierProperty.d.ts +4 -6
  13. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +53 -71
  14. package/dist/generators/ts/_ObjectType/Property.d.ts +7 -6
  15. package/dist/generators/ts/_ObjectType/Property.js +0 -6
  16. package/dist/generators/ts/_ObjectType/ShaclProperty.d.ts +3 -3
  17. package/dist/generators/ts/_ObjectType/ShaclProperty.js +7 -7
  18. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +6 -4
  19. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +12 -9
  20. package/dist/generators/ts/_ObjectType/classDeclaration.js +4 -9
  21. package/dist/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +5 -5
  22. package/dist/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +6 -6
  23. package/dist/generators/ts/_ObjectType/hashFunctionDeclarations.d.ts +4 -0
  24. package/dist/generators/ts/_ObjectType/hashFunctionDeclarations.js +18 -0
  25. package/dist/generators/ts/_ObjectType/hashFunctionOrMethodDeclarations.d.ts +13 -0
  26. package/dist/generators/ts/_ObjectType/hashFunctionOrMethodDeclarations.js +97 -0
  27. package/dist/generators/ts/_ObjectType/index.d.ts +2 -1
  28. package/dist/generators/ts/_ObjectType/index.js +2 -1
  29. package/dist/generators/ts/_ObjectType/interfaceDeclaration.js +1 -1
  30. package/dist/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +1 -1
  31. package/dist/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +5 -5
  32. package/dist/generators/ts/_ObjectType/toJsonReturnType.js +2 -4
  33. package/dist/input/NodeShape.d.ts +1 -0
  34. package/dist/input/NodeShape.js +11 -8
  35. package/dist/input/Ontology.d.ts +2 -0
  36. package/dist/input/Ontology.js +8 -2
  37. package/dist/input/PropertyPath.d.ts +41 -0
  38. package/dist/input/PropertyPath.js +105 -0
  39. package/dist/input/PropertyShape.js +3 -3
  40. package/dist/input/generated.d.ts +68 -64
  41. package/dist/input/generated.js +182 -160
  42. package/dist/input/tsFeatures.d.ts +1 -1
  43. package/dist/input/tsFeatures.js +14 -14
  44. package/package.json +7 -7
  45. package/dist/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +0 -5
  46. package/dist/generators/ts/_ObjectType/hashFunctionDeclaration.js +0 -20
  47. package/dist/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.d.ts +0 -11
  48. package/dist/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.js +0 -64
@@ -0,0 +1,97 @@
1
+ import { Scope, } from "ts-morph";
2
+ import { ObjectType } from "../ObjectType.js";
3
+ const hasherVariable = "_hasher";
4
+ export const hasherTypeConstraint = "{ update: (message: string | number[] | ArrayBuffer | Uint8Array) => void; }";
5
+ export function hashFunctionOrMethodDeclarations() {
6
+ if (!this.features.has("hash")) {
7
+ return [];
8
+ }
9
+ const hashOwnShaclPropertiesStatements = this.ownShaclProperties.flatMap((property) => property.hashStatements({
10
+ depth: 0,
11
+ variables: {
12
+ hasher: hasherVariable,
13
+ value: `${this.thisVariable}.${property.name}`,
14
+ },
15
+ }));
16
+ if (this.declarationType === "class" &&
17
+ this.parentObjectTypes.length > 0 &&
18
+ hashOwnShaclPropertiesStatements.length === 0) {
19
+ // If there's a parent class and no hash statements in this class, can skip overriding hash
20
+ return [];
21
+ }
22
+ const parameters = [];
23
+ if (this.declarationType === "interface") {
24
+ parameters.push({
25
+ name: this.thisVariable,
26
+ type: this.name,
27
+ });
28
+ }
29
+ parameters.push({
30
+ name: hasherVariable,
31
+ type: "HasherT",
32
+ });
33
+ const hashShaclPropertiesStatements = [];
34
+ let hasOverrideKeyword = false;
35
+ if (this.parentObjectTypes.length > 0) {
36
+ switch (this.declarationType) {
37
+ case "class": {
38
+ hashShaclPropertiesStatements.push(`super.hashShaclProperties(${hasherVariable});`);
39
+ hasOverrideKeyword = true;
40
+ break;
41
+ }
42
+ case "interface": {
43
+ for (const parentObjectType of this.parentObjectTypes) {
44
+ hashShaclPropertiesStatements.push(`${parentObjectType.name}.${parentObjectType.hashShaclPropertiesFunctionName}(${this.thisVariable}, ${hasherVariable});`);
45
+ }
46
+ break;
47
+ }
48
+ }
49
+ }
50
+ hashShaclPropertiesStatements.push(...hashOwnShaclPropertiesStatements);
51
+ const returnType = "HasherT";
52
+ const typeParameters = [
53
+ {
54
+ name: "HasherT",
55
+ constraint: hasherTypeConstraint,
56
+ },
57
+ ];
58
+ return [
59
+ {
60
+ hasOverrideKeyword,
61
+ name: this.declarationType === "class" ? "hash" : this.hashFunctionName,
62
+ parameters,
63
+ returnType,
64
+ statements: [
65
+ ...this.ownProperties
66
+ .filter((property) => !(property instanceof ObjectType.ShaclProperty))
67
+ .flatMap((property) => property.hashStatements({
68
+ depth: 0,
69
+ variables: {
70
+ hasher: hasherVariable,
71
+ value: `${this.thisVariable}.${property.name}`,
72
+ },
73
+ })),
74
+ this.declarationType === "class"
75
+ ? `this.hashShaclProperties(${hasherVariable});`
76
+ : `${this.name}.${this.hashShaclPropertiesFunctionName}(${this.thisVariable}, ${hasherVariable});`,
77
+ `return ${hasherVariable};`,
78
+ ],
79
+ typeParameters,
80
+ },
81
+ {
82
+ hasOverrideKeyword,
83
+ name: this.declarationType === "class"
84
+ ? "hashShaclProperties"
85
+ : this.hashShaclPropertiesFunctionName,
86
+ parameters,
87
+ returnType,
88
+ scope: this.declarationType === "class" ? Scope.Protected : undefined,
89
+ statements: [
90
+ ...hashShaclPropertiesStatements,
91
+ `return ${hasherVariable};`,
92
+ ],
93
+ typeParameters,
94
+ },
95
+ ];
96
+ }
97
+ //# sourceMappingURL=hashFunctionOrMethodDeclarations.js.map
@@ -4,7 +4,8 @@ export * from "./equalsFunctionDeclaration.js";
4
4
  export * from "./fromJsonFunctionDeclarations.js";
5
5
  export * from "./fromRdfFunctionDeclarations.js";
6
6
  export * from "./fromRdfTypeVariableStatement.js";
7
- export * from "./hashFunctionDeclaration.js";
7
+ export * from "./hashFunctionDeclarations.js";
8
+ export * from "./IdentifierPrefixProperty.js";
8
9
  export * from "./IdentifierProperty.js";
9
10
  export * from "./interfaceDeclaration.js";
10
11
  export * from "./jsonSchemaFunctionDeclaration.js";
@@ -4,7 +4,8 @@ export * from "./equalsFunctionDeclaration.js";
4
4
  export * from "./fromJsonFunctionDeclarations.js";
5
5
  export * from "./fromRdfFunctionDeclarations.js";
6
6
  export * from "./fromRdfTypeVariableStatement.js";
7
- export * from "./hashFunctionDeclaration.js";
7
+ export * from "./hashFunctionDeclarations.js";
8
+ export * from "./IdentifierPrefixProperty.js";
8
9
  export * from "./IdentifierProperty.js";
9
10
  export * from "./interfaceDeclaration.js";
10
11
  export * from "./jsonSchemaFunctionDeclaration.js";
@@ -8,7 +8,7 @@ export function interfaceDeclaration() {
8
8
  if (this.extern) {
9
9
  return Maybe.empty();
10
10
  }
11
- const properties = this.properties.map((property) => property.interfacePropertySignature);
11
+ const properties = this.properties.flatMap((property) => property.interfacePropertySignature.toList());
12
12
  return Maybe.of({
13
13
  extends: this.parentObjectTypes.map((parentObjectType) => parentObjectType.name),
14
14
  isExported: true,
@@ -14,7 +14,7 @@ export function jsonZodSchemaFunctionDeclaration() {
14
14
  }
15
15
  if (this.properties.length > 0) {
16
16
  mergeZodObjectSchemas.push(`${variables.zod}.object({ ${this.properties
17
- .map((property) => property.jsonZodSchema({ variables }))
17
+ .flatMap((property) => property.jsonZodSchema({ variables }).toList())
18
18
  .map(({ key, schema }) => `"${key}": ${schema}`)
19
19
  .join(",")} })`);
20
20
  }
@@ -28,11 +28,11 @@ export function toJsonFunctionOrMethodDeclaration() {
28
28
  break;
29
29
  }
30
30
  if (this.ownProperties.length > 0) {
31
- for (const property of this.ownProperties) {
32
- jsonObjectMembers.push(property.toJsonObjectMember({
33
- variables: { value: `${this.thisVariable}.${property.name}` },
34
- }));
35
- }
31
+ jsonObjectMembers.push(...this.ownProperties.flatMap((property) => property
32
+ .toJsonObjectMember({
33
+ variables: { value: `${this.thisVariable}.${property.name}` },
34
+ })
35
+ .toList()));
36
36
  }
37
37
  // 20241220: don't add @type until we're doing JSON-LD
38
38
  // switch (this.toRdfTypes.length) {
@@ -2,10 +2,8 @@ export function toJsonReturnType() {
2
2
  const typeMembers = [];
3
3
  if (this.ownProperties.length > 0) {
4
4
  typeMembers.push(`{ ${this.ownProperties
5
- .map((property) => {
6
- const propertySignature = property.jsonPropertySignature;
7
- return `readonly "${propertySignature.name}": ${propertySignature.type}`;
8
- })
5
+ .flatMap((property) => property.jsonPropertySignature.toList())
6
+ .map((propertySignature) => `readonly "${propertySignature.name}": ${propertySignature.type}`)
9
7
  .join("; ")} }`);
10
8
  }
11
9
  for (const parentObjectType of this.parentObjectTypes) {
@@ -39,6 +39,7 @@ export declare class NodeShape extends ShaclCoreNodeShape<any, Ontology, Propert
39
39
  get tsFeatures(): Maybe<Set<TsFeature>>;
40
40
  get tsImports(): readonly string[];
41
41
  get tsObjectDeclarationType(): Maybe<TsObjectDeclarationType>;
42
+ get tsObjectIdentifierPrefixPropertyName(): Maybe<string>;
42
43
  get tsObjectIdentifierPropertyName(): Maybe<string>;
43
44
  get tsObjectTypeDiscriminatorPropertyName(): Maybe<string>;
44
45
  private get _mintingStrategy();
@@ -68,7 +68,7 @@ export class NodeShape extends ShaclCoreNodeShape {
68
68
  }
69
69
  }
70
70
  }
71
- return thisMintingStrategy;
71
+ return thisMintingStrategy.altLazy(() => this.nodeKinds.has("BlankNode") ? Maybe.of("blankNode") : Maybe.empty());
72
72
  }
73
73
  get mutable() {
74
74
  return this.generatedShaclmateNodeShape.mutable;
@@ -139,9 +139,9 @@ export class NodeShape extends ShaclCoreNodeShape {
139
139
  return this.generatedShaclmateNodeShape.tsObjectDeclarationType
140
140
  .map((iri) => {
141
141
  switch (iri.value) {
142
- case "http://minorg.github.io/shaclmate/ns#_TsObjectDeclarationType_Class":
142
+ case "http://purl.org/shaclmate/ontology#_TsObjectDeclarationType_Class":
143
143
  return "class";
144
- case "http://minorg.github.io/shaclmate/ns#_TsObjectDeclarationType_Interface":
144
+ case "http://purl.org/shaclmate/ontology#_TsObjectDeclarationType_Interface":
145
145
  return "interface";
146
146
  default:
147
147
  throw new RangeError(iri.value);
@@ -149,8 +149,11 @@ export class NodeShape extends ShaclCoreNodeShape {
149
149
  })
150
150
  .altLazy(() => this.isDefinedBy.chain((ontology) => ontology.tsObjectDeclarationType));
151
151
  }
152
+ get tsObjectIdentifierPrefixPropertyName() {
153
+ return this.generatedShaclmateNodeShape.tsObjectIdentifierPrefixPropertyName.altLazy(() => this.isDefinedBy.chain((ontology) => ontology.tsObjectIdentifierPrefixPropertyName));
154
+ }
152
155
  get tsObjectIdentifierPropertyName() {
153
- return this.generatedShaclmateNodeShape.tsObjectIdentifierPropertyName;
156
+ return this.generatedShaclmateNodeShape.tsObjectIdentifierPrefixPropertyName.altLazy(() => this.isDefinedBy.chain((ontology) => ontology.tsObjectIdentifierPropertyName));
154
157
  }
155
158
  get tsObjectTypeDiscriminatorPropertyName() {
156
159
  return this.generatedShaclmateNodeShape
@@ -159,12 +162,12 @@ export class NodeShape extends ShaclCoreNodeShape {
159
162
  get _mintingStrategy() {
160
163
  return this.generatedShaclmateNodeShape.identifierMintingStrategy.map((iri) => {
161
164
  switch (iri.value) {
162
- case "http://minorg.github.io/shaclmate/ns#_IdentifierMintingStrategy_SHA256":
165
+ case "http://purl.org/shaclmate/ontology#_IdentifierMintingStrategy_BlankNode":
166
+ return "blankNode";
167
+ case "http://purl.org/shaclmate/ontology#_IdentifierMintingStrategy_SHA256":
163
168
  return "sha256";
164
- case "http://minorg.github.io/shaclmate/ns#_IdentifierMintingStrategy_UUIDv4":
169
+ case "http://purl.org/shaclmate/ontology#_IdentifierMintingStrategy_UUIDv4":
165
170
  return "uuidv4";
166
- default:
167
- throw new RangeError(iri.value);
168
171
  }
169
172
  });
170
173
  }
@@ -9,5 +9,7 @@ export declare class Ontology extends OwlOntology {
9
9
  get tsFeatures(): Maybe<Set<TsFeature>>;
10
10
  get tsImports(): readonly string[];
11
11
  get tsObjectDeclarationType(): Maybe<TsObjectDeclarationType>;
12
+ get tsObjectIdentifierPrefixPropertyName(): Maybe<string>;
13
+ get tsObjectIdentifierPropertyName(): Maybe<string>;
12
14
  }
13
15
  //# sourceMappingURL=Ontology.d.ts.map
@@ -18,14 +18,20 @@ export class Ontology extends OwlOntology {
18
18
  get tsObjectDeclarationType() {
19
19
  return this.generatedShaclmateOntology.tsObjectDeclarationType.map((iri) => {
20
20
  switch (iri.value) {
21
- case "http://minorg.github.io/shaclmate/ns#_TsObjectDeclarationType_Class":
21
+ case "http://purl.org/shaclmate/ontology#_TsObjectDeclarationType_Class":
22
22
  return "class";
23
- case "http://minorg.github.io/shaclmate/ns#_TsObjectDeclarationType_Interface":
23
+ case "http://purl.org/shaclmate/ontology#_TsObjectDeclarationType_Interface":
24
24
  return "interface";
25
25
  default:
26
26
  throw new RangeError(iri.value);
27
27
  }
28
28
  });
29
29
  }
30
+ get tsObjectIdentifierPrefixPropertyName() {
31
+ return this.generatedShaclmateOntology.tsObjectIdentifierPrefixPropertyName;
32
+ }
33
+ get tsObjectIdentifierPropertyName() {
34
+ return this.generatedShaclmateOntology.tsObjectIdentifierPropertyName;
35
+ }
30
36
  }
31
37
  //# sourceMappingURL=Ontology.js.map
@@ -0,0 +1,41 @@
1
+ import type { NamedNode } from "@rdfjs/types";
2
+ import type * as purify from "purify-ts";
3
+ import type * as rdfjsResource from "rdfjs-resource";
4
+ export interface AlternativePath {
5
+ readonly kind: "AlternativePath";
6
+ readonly members: readonly PropertyPath[];
7
+ }
8
+ export interface InversePath {
9
+ readonly kind: "InversePath";
10
+ readonly path: PropertyPath;
11
+ }
12
+ export interface OneOrMorePath {
13
+ readonly kind: "OneOrMorePath";
14
+ readonly path: PropertyPath;
15
+ }
16
+ export interface PredicatePath {
17
+ readonly iri: NamedNode;
18
+ readonly kind: "PredicatePath";
19
+ }
20
+ export interface SequencePath {
21
+ readonly kind: "SequencePath";
22
+ readonly members: readonly PropertyPath[];
23
+ }
24
+ export interface ZeroOrMorePath {
25
+ readonly kind: "ZeroOrMorePath";
26
+ readonly path: PropertyPath;
27
+ }
28
+ export interface ZeroOrOnePath {
29
+ readonly kind: "ZeroOrOnePath";
30
+ readonly path: PropertyPath;
31
+ }
32
+ export type PropertyPath = AlternativePath | InversePath | OneOrMorePath | PredicatePath | SequencePath | ZeroOrMorePath | ZeroOrOnePath;
33
+ export declare namespace PropertyPath {
34
+ function fromRdf({ resource, }: {
35
+ [_index: string]: any;
36
+ ignoreRdfType?: boolean;
37
+ languageIn?: readonly string[];
38
+ resource: rdfjsResource.Resource;
39
+ }): purify.Either<rdfjsResource.Resource.ValueError, PropertyPath>;
40
+ }
41
+ //# sourceMappingURL=PropertyPath.d.ts.map
@@ -0,0 +1,105 @@
1
+ import { rdf, sh } from "@tpluscode/rdf-ns-builders";
2
+ import { Either, Left } from "purify-ts";
3
+ import { Resource } from "rdfjs-resource";
4
+ export var PropertyPath;
5
+ (function (PropertyPath) {
6
+ function fromRdf({ resource, }) {
7
+ // Predicate path
8
+ // sh:path ex:parent
9
+ if (resource.identifier.termType === "NamedNode") {
10
+ return Either.of({ iri: resource.identifier, kind: "PredicatePath" });
11
+ }
12
+ // The other property path types are BlankNodes
13
+ const getPropertyPathList = (listResource) => {
14
+ return listResource.toList().chain((values) => {
15
+ const members = [];
16
+ for (const value of values) {
17
+ const memberResource = value.toResource().toMaybe();
18
+ if (memberResource.isNothing()) {
19
+ return Left(new Resource.ValueError({
20
+ focusResource: resource,
21
+ message: "non-identifier in property path list",
22
+ predicate: rdf.subject,
23
+ }));
24
+ }
25
+ const member = PropertyPath.fromRdf({
26
+ resource: memberResource.unsafeCoerce(),
27
+ });
28
+ if (member.isLeft()) {
29
+ return member;
30
+ }
31
+ members.push(member.unsafeCoerce());
32
+ }
33
+ return Either.of(members);
34
+ });
35
+ };
36
+ for (const quad of resource.dataset.match(resource.identifier, null, null, null)) {
37
+ switch (quad.object.termType) {
38
+ case "BlankNode":
39
+ case "NamedNode":
40
+ break;
41
+ default:
42
+ return Left(new Resource.ValueError({
43
+ focusResource: resource,
44
+ message: `non-BlankNode/NamedNode property path object on path ${resource.identifier.value}: ${quad.object.termType} ${quad.object.value}`,
45
+ predicate: quad.predicate,
46
+ }));
47
+ }
48
+ const objectResource = new Resource({
49
+ dataset: resource.dataset,
50
+ identifier: quad.object,
51
+ });
52
+ // Alternative path
53
+ // sh:path: [ sh:alternativePath ( ex:father ex:mother ) ]
54
+ if (quad.predicate.equals(sh.alternativePath)) {
55
+ return getPropertyPathList(objectResource).map((members) => ({
56
+ kind: "AlternativePath",
57
+ members,
58
+ }));
59
+ }
60
+ // Inverse path
61
+ // sh:path: [ sh:inversePath ex:parent ]
62
+ if (quad.predicate.equals(sh.inversePath)) {
63
+ return PropertyPath.fromRdf({ resource: objectResource }).map((path) => ({
64
+ kind: "InversePath",
65
+ path,
66
+ }));
67
+ }
68
+ // One or more path
69
+ if (quad.predicate.equals(sh.oneOrMorePath)) {
70
+ return PropertyPath.fromRdf({ resource: objectResource }).map((path) => ({
71
+ kind: "OneOrMorePath",
72
+ path,
73
+ }));
74
+ }
75
+ // Sequence path
76
+ // sh:path ( ex:parent ex:firstName )
77
+ if (quad.predicate.equals(rdf.first)) {
78
+ return getPropertyPathList(objectResource).map((members) => ({
79
+ kind: "SequencePath",
80
+ members,
81
+ }));
82
+ }
83
+ // Zero or more path
84
+ if (quad.predicate.equals(sh.zeroOrMorePath)) {
85
+ return PropertyPath.fromRdf({ resource: objectResource }).map((path) => ({
86
+ kind: "ZeroOrMorePath",
87
+ path,
88
+ }));
89
+ }
90
+ if (quad.predicate.equals(sh.zeroOrOnePath)) {
91
+ return PropertyPath.fromRdf({ resource: objectResource }).map((path) => ({
92
+ kind: "ZeroOrOnePath",
93
+ path,
94
+ }));
95
+ }
96
+ }
97
+ return Left(new Resource.ValueError({
98
+ focusResource: resource,
99
+ message: `unrecognized or ill-formed SHACL property path ${resource.identifier.value}`,
100
+ predicate: rdf.subject,
101
+ }));
102
+ }
103
+ PropertyPath.fromRdf = fromRdf;
104
+ })(PropertyPath || (PropertyPath = {}));
105
+ //# sourceMappingURL=PropertyPath.js.map
@@ -19,11 +19,11 @@ export class PropertyShape extends ShaclCorePropertyShape {
19
19
  return this.generatedShaclmatePropertyShape.visibility
20
20
  .map((iri) => {
21
21
  switch (iri.value) {
22
- case "http://minorg.github.io/shaclmate/ns#_Visibility_Private":
22
+ case "http://purl.org/shaclmate/ontology#_Visibility_Private":
23
23
  return "private";
24
- case "http://minorg.github.io/shaclmate/ns#_Visibility_Protected":
24
+ case "http://purl.org/shaclmate/ontology#_Visibility_Protected":
25
25
  return "protected";
26
- case "http://minorg.github.io/shaclmate/ns#_Visibility_Public":
26
+ case "http://purl.org/shaclmate/ontology#_Visibility_Public":
27
27
  return "public";
28
28
  default:
29
29
  throw new RangeError(iri.value);