@shaclmate/compiler 2.0.12

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 (109) hide show
  1. package/ShapesGraphToAstTransformer.d.ts +32 -0
  2. package/ShapesGraphToAstTransformer.js +461 -0
  3. package/ast/Ast.d.ts +5 -0
  4. package/ast/Ast.js +2 -0
  5. package/ast/IdentifierType.d.ts +13 -0
  6. package/ast/IdentifierType.js +2 -0
  7. package/ast/IntersectionType.d.ts +9 -0
  8. package/ast/IntersectionType.js +2 -0
  9. package/ast/LiteralType.d.ts +13 -0
  10. package/ast/LiteralType.js +2 -0
  11. package/ast/MintingStrategy.d.ts +8 -0
  12. package/ast/MintingStrategy.js +9 -0
  13. package/ast/Name.d.ts +13 -0
  14. package/ast/Name.js +2 -0
  15. package/ast/ObjectType.d.ts +88 -0
  16. package/ast/ObjectType.js +2 -0
  17. package/ast/OptionType.d.ts +11 -0
  18. package/ast/OptionType.js +2 -0
  19. package/ast/SetType.d.ts +12 -0
  20. package/ast/SetType.js +2 -0
  21. package/ast/Type.d.ts +9 -0
  22. package/ast/Type.js +2 -0
  23. package/ast/UnionType.d.ts +9 -0
  24. package/ast/UnionType.js +2 -0
  25. package/ast/index.d.ts +10 -0
  26. package/ast/index.js +10 -0
  27. package/generators/index.d.ts +3 -0
  28. package/generators/index.js +3 -0
  29. package/generators/json/AstJsonGenerator.d.ts +7 -0
  30. package/generators/json/AstJsonGenerator.js +103 -0
  31. package/generators/json/index.d.ts +2 -0
  32. package/generators/json/index.js +2 -0
  33. package/generators/ts/BooleanType.d.ts +7 -0
  34. package/generators/ts/BooleanType.js +13 -0
  35. package/generators/ts/Configuration.d.ts +29 -0
  36. package/generators/ts/Configuration.js +40 -0
  37. package/generators/ts/IdentifierType.d.ts +23 -0
  38. package/generators/ts/IdentifierType.js +83 -0
  39. package/generators/ts/IntersectionType.d.ts +14 -0
  40. package/generators/ts/IntersectionType.js +26 -0
  41. package/generators/ts/ListType.d.ts +26 -0
  42. package/generators/ts/ListType.js +119 -0
  43. package/generators/ts/LiteralType.d.ts +14 -0
  44. package/generators/ts/LiteralType.js +61 -0
  45. package/generators/ts/NumberType.d.ts +7 -0
  46. package/generators/ts/NumberType.js +13 -0
  47. package/generators/ts/ObjectType.d.ts +71 -0
  48. package/generators/ts/ObjectType.js +160 -0
  49. package/generators/ts/OptionType.d.ts +17 -0
  50. package/generators/ts/OptionType.js +83 -0
  51. package/generators/ts/PrimitiveType.d.ts +11 -0
  52. package/generators/ts/PrimitiveType.js +41 -0
  53. package/generators/ts/RdfjsTermType.d.ts +25 -0
  54. package/generators/ts/RdfjsTermType.js +46 -0
  55. package/generators/ts/SetType.d.ts +19 -0
  56. package/generators/ts/SetType.js +76 -0
  57. package/generators/ts/StringType.d.ts +7 -0
  58. package/generators/ts/StringType.js +13 -0
  59. package/generators/ts/TsGenerator.d.ts +15 -0
  60. package/generators/ts/TsGenerator.js +119 -0
  61. package/generators/ts/Type.d.ts +108 -0
  62. package/generators/ts/Type.js +83 -0
  63. package/generators/ts/TypeFactory.d.ts +15 -0
  64. package/generators/ts/TypeFactory.js +198 -0
  65. package/generators/ts/UnionType.d.ts +18 -0
  66. package/generators/ts/UnionType.js +165 -0
  67. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +23 -0
  68. package/generators/ts/_ObjectType/IdentifierProperty.js +82 -0
  69. package/generators/ts/_ObjectType/Property.d.ts +38 -0
  70. package/generators/ts/_ObjectType/Property.js +11 -0
  71. package/generators/ts/_ObjectType/ShaclProperty.d.ts +25 -0
  72. package/generators/ts/_ObjectType/ShaclProperty.js +106 -0
  73. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +29 -0
  74. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +53 -0
  75. package/generators/ts/_ObjectType/classDeclaration.d.ts +4 -0
  76. package/generators/ts/_ObjectType/classDeclaration.js +154 -0
  77. package/generators/ts/_ObjectType/equalsFunctionDeclaration.d.ts +4 -0
  78. package/generators/ts/_ObjectType/equalsFunctionDeclaration.js +27 -0
  79. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.d.ts +4 -0
  80. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.js +72 -0
  81. package/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +5 -0
  82. package/generators/ts/_ObjectType/hashFunctionDeclaration.js +64 -0
  83. package/generators/ts/_ObjectType/index.d.ts +12 -0
  84. package/generators/ts/_ObjectType/index.js +12 -0
  85. package/generators/ts/_ObjectType/interfaceDeclaration.d.ts +4 -0
  86. package/generators/ts/_ObjectType/interfaceDeclaration.js +12 -0
  87. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.d.ts +4 -0
  88. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.js +46 -0
  89. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +4 -0
  90. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +53 -0
  91. package/generators/ts/index.d.ts +2 -0
  92. package/generators/ts/index.js +2 -0
  93. package/generators/ts/rdfjsTermExpression.d.ts +5 -0
  94. package/generators/ts/rdfjsTermExpression.js +15 -0
  95. package/generators/ts/stringToValidTsIdentifier.d.ts +2 -0
  96. package/generators/ts/stringToValidTsIdentifier.js +14 -0
  97. package/generators/ts/tsName.d.ts +3 -0
  98. package/generators/ts/tsName.js +22 -0
  99. package/index.d.ts +2 -0
  100. package/index.js +2 -0
  101. package/logger.d.ts +2 -0
  102. package/logger.js +8 -0
  103. package/package.json +71 -0
  104. package/vocabularies/dashDataset.d.ts +3 -0
  105. package/vocabularies/dashDataset.js +2370 -0
  106. package/vocabularies/index.d.ts +2 -0
  107. package/vocabularies/index.js +2 -0
  108. package/vocabularies/shaclmate.d.ts +15 -0
  109. package/vocabularies/shaclmate.js +6 -0
@@ -0,0 +1,119 @@
1
+ import { rdf } from "@tpluscode/rdf-ns-builders";
2
+ import { Maybe } from "purify-ts";
3
+ import { NodeKind } from "shacl-ast";
4
+ import { MintingStrategy } from "../../ast";
5
+ import { Type } from "./Type.js";
6
+ export class ListType extends Type {
7
+ constructor({ identifierNodeKind, itemType, mintingStrategy, rdfType, ...superParameters }) {
8
+ super(superParameters);
9
+ this.kind = "ListType";
10
+ this.identifierNodeKind = identifierNodeKind;
11
+ this.itemType = itemType;
12
+ this.mintingStrategy = mintingStrategy.orDefault(MintingStrategy.SHA256);
13
+ this.rdfType = rdfType;
14
+ }
15
+ get discriminatorProperty() {
16
+ return Maybe.empty();
17
+ }
18
+ get importStatements() {
19
+ if (this.identifierNodeKind === NodeKind.IRI) {
20
+ return ['import { sha256 } from "js-sha256";'];
21
+ }
22
+ return [];
23
+ }
24
+ get name() {
25
+ return `readonly ${this.itemType.name}[]`;
26
+ }
27
+ chainSparqlGraphPatternExpression({ variables, }) {
28
+ return Maybe.of(new Type.SparqlGraphPatternsExpression(`new sparqlBuilder.RdfListGraphPatterns({ ${this.itemType
29
+ .chainSparqlGraphPatternExpression({
30
+ variables: {
31
+ subject: "itemVariable",
32
+ },
33
+ })
34
+ .map((itemSparqlGraphPatternsExpression) => `itemGraphPatterns: (itemVariable) => ${itemSparqlGraphPatternsExpression.toSparqlGraphPatternsExpression()}, `)
35
+ .orDefault("")} rdfList: ${variables.subject} })`));
36
+ }
37
+ equalsFunction() {
38
+ return `(left, right) => purifyHelpers.Arrays.equals(left, right, ${this.itemType.equalsFunction()})`;
39
+ }
40
+ fromRdfExpression({ variables, }) {
41
+ return `${variables.resourceValues}.head().chain(value => value.toList()).map(values => values.flatMap(value => ${this.itemType.fromRdfExpression({ variables: { ...variables, resourceValues: "value.toValues()" } })}.toMaybe().toList()))`;
42
+ }
43
+ hashStatements({ variables, }) {
44
+ return [
45
+ `for (const element of ${variables.value}) { ${this.itemType.hashStatements({ variables: { ...variables, value: "element" } }).join("\n")} }`,
46
+ ];
47
+ }
48
+ toRdfExpression({ variables, }) {
49
+ let listIdentifier;
50
+ let mutableResourceTypeName;
51
+ let resourceSetMethodName;
52
+ let subListIdentifier;
53
+ switch (this.identifierNodeKind) {
54
+ case NodeKind.BLANK_NODE: {
55
+ listIdentifier = subListIdentifier = "dataFactory.blankNode()";
56
+ mutableResourceTypeName = "rdfjsResource.MutableResource";
57
+ resourceSetMethodName = "mutableResource";
58
+ break;
59
+ }
60
+ case NodeKind.IRI: {
61
+ switch (this.mintingStrategy) {
62
+ case MintingStrategy.SHA256:
63
+ listIdentifier = `dataFactory.namedNode(\`urn:shaclmate:list:\${${variables.value}.reduce(
64
+ (hasher, item) => {
65
+ ${this.itemType.hashStatements({ variables: { hasher: "hasher", value: "item" } })}
66
+ return hasher;
67
+ },
68
+ sha256.create(),
69
+ )}\`)`;
70
+ break;
71
+ case MintingStrategy.UUIDv4:
72
+ listIdentifier =
73
+ "dataFactory.namedNode(`urn:shaclmate:list:${uuid.v4()}`)";
74
+ break;
75
+ }
76
+ mutableResourceTypeName =
77
+ "rdfjsResource.MutableResource<rdfjs.NamedNode>";
78
+ resourceSetMethodName = "mutableNamedResource";
79
+ subListIdentifier =
80
+ "dataFactory.namedNode(`${listResource.identifier.value}:${itemIndex}`)";
81
+ break;
82
+ }
83
+ }
84
+ return `${variables.value}.reduce(({ currentSubListResource, listResource }, item, itemIndex) => {
85
+ if (itemIndex === 0) {
86
+ currentSubListResource = listResource;
87
+ } else {
88
+ const newSubListResource = ${variables.resourceSet}.${resourceSetMethodName}({
89
+ identifier: ${subListIdentifier},
90
+ mutateGraph: ${variables.mutateGraph},
91
+ });
92
+ currentSubListResource!.add(dataFactory.namedNode("${rdf.rest.value}"), newSubListResource.identifier);
93
+ currentSubListResource = newSubListResource;
94
+ }
95
+
96
+ ${this.rdfType.map((rdfType) => `currentSubListResource.add(dataFactory.namedNode("${rdf.type.value}"), dataFactory.namedNode("${rdfType.value}"))`).orDefault("")}
97
+
98
+ currentSubListResource.add(dataFactory.namedNode("${rdf.first.value}"), ${this.itemType.toRdfExpression({ variables: { mutateGraph: variables.mutateGraph, predicate: `dataFactory.namedNode("${rdf.first.value}")`, resource: "currentSubListResource", resourceSet: variables.resourceSet, value: "item" } })});
99
+
100
+ if (itemIndex + 1 === ${variables.value}.length) {
101
+ currentSubListResource.add(dataFactory.namedNode("${rdf.rest.value}"), dataFactory.namedNode("${rdf.nil.value}"));
102
+ }
103
+
104
+ return { currentSubListResource, listResource };
105
+ },
106
+ {
107
+ currentSubListResource: null,
108
+ listResource: resourceSet.${resourceSetMethodName}({
109
+ identifier: ${listIdentifier},
110
+ mutateGraph: ${variables.mutateGraph}
111
+ }),
112
+ } as {
113
+ currentSubListResource: ${mutableResourceTypeName} | null;
114
+ listResource: ${mutableResourceTypeName};
115
+ },
116
+ ).listResource.identifier`;
117
+ }
118
+ }
119
+ //# sourceMappingURL=ListType.js.map
@@ -0,0 +1,14 @@
1
+ import type { Literal } from "@rdfjs/types";
2
+ import { Maybe } from "purify-ts";
3
+ import { RdfjsTermType } from "./RdfjsTermType.js";
4
+ import type { Type } from "./Type";
5
+ export declare class LiteralType extends RdfjsTermType<Literal> {
6
+ readonly kind = "LiteralType";
7
+ get conversions(): readonly Type.Conversion[];
8
+ get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
9
+ get importStatements(): readonly string[];
10
+ get name(): string;
11
+ fromRdfResourceValueExpression({ variables, }: Parameters<RdfjsTermType<Literal>["fromRdfResourceValueExpression"]>[0]): string;
12
+ hashStatements({ variables, }: Parameters<RdfjsTermType<Literal>["hashStatements"]>[0]): readonly string[];
13
+ }
14
+ //# sourceMappingURL=LiteralType.d.ts.map
@@ -0,0 +1,61 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { RdfjsTermType } from "./RdfjsTermType.js";
3
+ import { rdfjsTermExpression } from "./rdfjsTermExpression";
4
+ export class LiteralType extends RdfjsTermType {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.kind = "LiteralType";
8
+ }
9
+ get conversions() {
10
+ const conversions = [];
11
+ conversions.push({
12
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value})`,
13
+ sourceTypeName: "boolean",
14
+ });
15
+ conversions.push({
16
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value})`,
17
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object" && ${value} instanceof Date`,
18
+ sourceTypeName: "Date",
19
+ });
20
+ conversions.push({
21
+ conversionExpression: (value) => `rdfLiteral.toRdf(${value})`,
22
+ sourceTypeName: "number",
23
+ });
24
+ conversions.push({
25
+ conversionExpression: (value) => `${this.configuration.dataFactoryVariable}.literal(${value})`,
26
+ sourceTypeName: "string",
27
+ });
28
+ this.defaultValue.ifJust((defaultValue) => {
29
+ conversions.push({
30
+ conversionExpression: () => rdfjsTermExpression(defaultValue, this.configuration),
31
+ sourceTypeName: "undefined",
32
+ });
33
+ });
34
+ conversions.push({
35
+ conversionExpression: (value) => value,
36
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
37
+ sourceTypeName: this.name,
38
+ });
39
+ return conversions;
40
+ }
41
+ get discriminatorProperty() {
42
+ return Maybe.of({
43
+ name: "termType",
44
+ type: "string",
45
+ values: ["Literal"],
46
+ });
47
+ }
48
+ get importStatements() {
49
+ return ["// @ts-ignore", 'import * as rdfLiteral from "rdf-literal";'];
50
+ }
51
+ get name() {
52
+ return "rdfjs.Literal";
53
+ }
54
+ fromRdfResourceValueExpression({ variables, }) {
55
+ return `${variables.resourceValue}.toLiteral()`;
56
+ }
57
+ hashStatements({ variables, }) {
58
+ return [`${variables.hasher}.update(${variables.value}.value);`];
59
+ }
60
+ }
61
+ //# sourceMappingURL=LiteralType.js.map
@@ -0,0 +1,7 @@
1
+ import { PrimitiveType } from "./PrimitiveType.js";
2
+ export declare class NumberType extends PrimitiveType {
3
+ get name(): string;
4
+ fromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType["fromRdfResourceValueExpression"]>[0]): string;
5
+ hashStatements({ variables, }: Parameters<PrimitiveType["hashStatements"]>[0]): readonly string[];
6
+ }
7
+ //# sourceMappingURL=NumberType.d.ts.map
@@ -0,0 +1,13 @@
1
+ import { PrimitiveType } from "./PrimitiveType.js";
2
+ export class NumberType extends PrimitiveType {
3
+ get name() {
4
+ return "number";
5
+ }
6
+ fromRdfResourceValueExpression({ variables, }) {
7
+ return `${variables.resourceValue}.toNumber()`;
8
+ }
9
+ hashStatements({ variables, }) {
10
+ return [`${variables.hasher}.update(${variables.value}.toString());`];
11
+ }
12
+ }
13
+ //# sourceMappingURL=NumberType.js.map
@@ -0,0 +1,71 @@
1
+ import type { NamedNode } from "@rdfjs/types";
2
+ import { Maybe } from "purify-ts";
3
+ import { MintingStrategy } from "../../ast";
4
+ 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 {
8
+ 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;
16
+ 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;
22
+ private readonly lazyAncestorObjectTypes;
23
+ private readonly lazyDescendantObjectTypes;
24
+ private readonly lazyParentObjectTypes;
25
+ private readonly lazyProperties;
26
+ constructor({ abstract, identifierType, export_, lazyAncestorObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, mintingStrategy, name, rdfType, ...superParameters }: {
27
+ abstract: boolean;
28
+ export_: boolean;
29
+ identifierType: IdentifierType;
30
+ lazyAncestorObjectTypes: () => readonly ObjectType[];
31
+ lazyDescendantObjectTypes: () => readonly ObjectType[];
32
+ lazyParentObjectTypes: () => readonly ObjectType[];
33
+ lazyProperties: () => readonly ObjectType.Property[];
34
+ mintingStrategy: Maybe<MintingStrategy>;
35
+ name: string;
36
+ rdfType: Maybe<NamedNode>;
37
+ } & ConstructorParameters<typeof Type>[0]);
38
+ get ancestorObjectTypes(): readonly ObjectType[];
39
+ get conversions(): readonly Type.Conversion[];
40
+ get descendantObjectTypes(): readonly ObjectType[];
41
+ get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
42
+ get discriminatorValue(): string;
43
+ get hashFunctionName(): string;
44
+ get importStatements(): readonly string[];
45
+ get parentObjectTypes(): readonly ObjectType[];
46
+ get properties(): readonly ObjectType.Property[];
47
+ chainSparqlGraphPatternExpression({ variables, }: Parameters<Type["chainSparqlGraphPatternExpression"]>[0]): Maybe<Type.SparqlGraphPatternsExpression>;
48
+ equalsFunction(): string;
49
+ fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
50
+ hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
51
+ rdfjsResourceType(options?: {
52
+ mutable?: boolean;
53
+ }): {
54
+ readonly mutable: boolean;
55
+ readonly name: string;
56
+ readonly named: boolean;
57
+ };
58
+ toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
59
+ protected ensureAtMostOneSuperObjectType(): void;
60
+ }
61
+ export declare namespace ObjectType {
62
+ const IdentifierProperty: typeof _ObjectType.IdentifierProperty;
63
+ type IdentifierProperty = _ObjectType.IdentifierProperty;
64
+ const Property: typeof _ObjectType.Property;
65
+ type Property = _ObjectType.Property<any>;
66
+ const ShaclProperty: typeof _ObjectType.ShaclProperty;
67
+ type ShaclProperty = _ObjectType.ShaclProperty;
68
+ const TypeDiscriminatorProperty: typeof _ObjectType.TypeDiscriminatorProperty;
69
+ type TypeDiscriminatorProperty = _ObjectType.TypeDiscriminatorProperty;
70
+ }
71
+ //# sourceMappingURL=ObjectType.d.ts.map
@@ -0,0 +1,160 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Maybe } from "purify-ts";
8
+ 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 }) {
14
+ 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
+ this.kind = "ObjectType";
21
+ this.sparqlGraphPatternsClassDeclaration = _ObjectType.sparqlGraphPatternsClassDeclaration;
22
+ this.toRdfFunctionDeclaration = _ObjectType.toRdfFunctionDeclaration;
23
+ this.abstract = abstract;
24
+ this.export_ = export_;
25
+ // Lazily initialize some members in getters to avoid recursive construction
26
+ this.lazyAncestorObjectTypes = lazyAncestorObjectTypes;
27
+ this.lazyDescendantObjectTypes = lazyDescendantObjectTypes;
28
+ this.lazyParentObjectTypes = lazyParentObjectTypes;
29
+ this.lazyProperties = lazyProperties;
30
+ this.identifierType = identifierType;
31
+ this.mintingStrategy = mintingStrategy;
32
+ this.rdfType = rdfType;
33
+ this.name = name;
34
+ }
35
+ get ancestorObjectTypes() {
36
+ return this.lazyAncestorObjectTypes();
37
+ }
38
+ get conversions() {
39
+ return [
40
+ {
41
+ conversionExpression: (value) => value,
42
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object" && ${value} instanceof ${this.name}`,
43
+ sourceTypeName: this.name,
44
+ },
45
+ ];
46
+ }
47
+ get descendantObjectTypes() {
48
+ return this.lazyDescendantObjectTypes();
49
+ }
50
+ get discriminatorProperty() {
51
+ return Maybe.of({
52
+ name: this.configuration.objectTypeDiscriminatorPropertyName,
53
+ type: "string",
54
+ values: [this.discriminatorValue],
55
+ });
56
+ }
57
+ get discriminatorValue() {
58
+ return this.name;
59
+ }
60
+ get hashFunctionName() {
61
+ if (this.lazyDescendantObjectTypes().length > 0 ||
62
+ this.ancestorObjectTypes.length > 0) {
63
+ return `hash${this.name}`;
64
+ }
65
+ return "hash";
66
+ }
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;
77
+ }
78
+ });
79
+ return importStatements;
80
+ }
81
+ get parentObjectTypes() {
82
+ return this.lazyParentObjectTypes();
83
+ }
84
+ get properties() {
85
+ const properties = this.lazyProperties()
86
+ .concat()
87
+ .sort((left, right) => left.name.localeCompare(right.name));
88
+ const propertyNames = new Set();
89
+ for (const property of properties) {
90
+ if (propertyNames.has(property.name)) {
91
+ throw new Error(`duplicate property '${property.name}'`);
92
+ }
93
+ }
94
+ return properties;
95
+ }
96
+ chainSparqlGraphPatternExpression({ variables, }) {
97
+ return Maybe.of(new Type.SparqlGraphPatternsExpression(`new ${this.name}.SparqlGraphPatterns(${variables.subject})`));
98
+ }
99
+ equalsFunction() {
100
+ switch (this.configuration.objectTypeDeclarationType) {
101
+ case "class":
102
+ return "purifyHelpers.Equatable.equals";
103
+ case "interface":
104
+ return `${this.name}.equals`;
105
+ }
106
+ }
107
+ fromRdfExpression({ variables, }) {
108
+ return `${variables.resourceValues}.head().chain(value => value.to${this.rdfjsResourceType().named ? "Named" : ""}Resource()).chain(resource => ${this.name}.fromRdf(resource))`;
109
+ }
110
+ hashStatements({ variables, }) {
111
+ return [
112
+ `${this.name}.${this.hashFunctionName}(${variables.value}, ${variables.hasher});`,
113
+ ];
114
+ }
115
+ rdfjsResourceType(options) {
116
+ if (this.parentObjectTypes.length > 0) {
117
+ return this.parentObjectTypes[0].rdfjsResourceType(options);
118
+ }
119
+ return {
120
+ mutable: !!options?.mutable,
121
+ name: `rdfjsResource.${options?.mutable ? "Mutable" : ""}Resource${this.identifierType.isNamedNodeKind ? "<rdfjs.NamedNode>" : ""}`,
122
+ named: this.identifierType.isNamedNodeKind,
123
+ };
124
+ }
125
+ toRdfExpression({ variables, }) {
126
+ switch (this.configuration.objectTypeDeclarationType) {
127
+ case "class":
128
+ return `${variables.value}.toRdf({ mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }).identifier`;
129
+ case "interface":
130
+ return `${this.name}.toRdf(${variables.value}, { mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }).identifier`;
131
+ }
132
+ }
133
+ ensureAtMostOneSuperObjectType() {
134
+ if (this.parentObjectTypes.length > 1) {
135
+ throw new RangeError(`object type '${this.name}' has multiple super object types`);
136
+ }
137
+ }
138
+ }
139
+ __decorate([
140
+ Memoize()
141
+ ], ObjectType.prototype, "ancestorObjectTypes", null);
142
+ __decorate([
143
+ Memoize()
144
+ ], ObjectType.prototype, "descendantObjectTypes", null);
145
+ __decorate([
146
+ Memoize()
147
+ ], ObjectType.prototype, "hashFunctionName", null);
148
+ __decorate([
149
+ Memoize()
150
+ ], ObjectType.prototype, "parentObjectTypes", null);
151
+ __decorate([
152
+ Memoize()
153
+ ], ObjectType.prototype, "properties", null);
154
+ (function (ObjectType) {
155
+ ObjectType.IdentifierProperty = _ObjectType.IdentifierProperty;
156
+ ObjectType.Property = _ObjectType.Property;
157
+ ObjectType.ShaclProperty = _ObjectType.ShaclProperty;
158
+ ObjectType.TypeDiscriminatorProperty = _ObjectType.TypeDiscriminatorProperty;
159
+ })(ObjectType || (ObjectType = {}));
160
+ //# sourceMappingURL=ObjectType.js.map
@@ -0,0 +1,17 @@
1
+ import { Type } from "./Type.js";
2
+ export declare class OptionType extends Type {
3
+ readonly itemType: Type;
4
+ readonly kind = "OptionType";
5
+ constructor({ itemType, ...superParameters }: ConstructorParameters<typeof Type>[0] & {
6
+ itemType: Type;
7
+ });
8
+ get conversions(): readonly Type.Conversion[];
9
+ get name(): string;
10
+ chainSparqlGraphPatternExpression(parameters: Parameters<Type["chainSparqlGraphPatternExpression"]>[0]): ReturnType<Type["chainSparqlGraphPatternExpression"]>;
11
+ equalsFunction(): string;
12
+ fromRdfExpression(parameters: Parameters<Type["fromRdfExpression"]>[0]): string;
13
+ hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
14
+ propertySparqlGraphPatternExpression(parameters: Parameters<Type["propertySparqlGraphPatternExpression"]>[0]): Type.SparqlGraphPatternExpression;
15
+ toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
16
+ }
17
+ //# sourceMappingURL=OptionType.d.ts.map
@@ -0,0 +1,83 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Memoize } from "typescript-memoize";
8
+ import { Type } from "./Type.js";
9
+ export class OptionType extends Type {
10
+ constructor({ itemType, ...superParameters }) {
11
+ super(superParameters);
12
+ this.kind = "OptionType";
13
+ this.itemType = itemType;
14
+ }
15
+ get conversions() {
16
+ const conversions = [];
17
+ conversions.push({
18
+ conversionExpression: (value) => value,
19
+ sourceTypeCheckExpression: (value) => `purify.Maybe.isMaybe(${value})`,
20
+ sourceTypeName: this.name,
21
+ });
22
+ for (const itemTypeConversion of this.itemType.conversions) {
23
+ conversions.push({
24
+ ...itemTypeConversion,
25
+ conversionExpression: (value) => `purify.Maybe.of(${itemTypeConversion.conversionExpression(value)})`,
26
+ });
27
+ }
28
+ if (!conversions.some((conversion) => conversion.sourceTypeName === "undefined")) {
29
+ conversions.push({
30
+ conversionExpression: () => "purify.Maybe.empty()",
31
+ sourceTypeName: "undefined",
32
+ });
33
+ }
34
+ return conversions;
35
+ }
36
+ get name() {
37
+ return `purify.Maybe<${this.itemType.name}>`;
38
+ }
39
+ chainSparqlGraphPatternExpression(parameters) {
40
+ return this.itemType.chainSparqlGraphPatternExpression(parameters);
41
+ }
42
+ equalsFunction() {
43
+ const itemTypeEqualsFunction = this.itemType.equalsFunction();
44
+ if (itemTypeEqualsFunction === "purifyHelpers.Equatable.equals") {
45
+ return "purifyHelpers.Equatable.maybeEquals";
46
+ }
47
+ if (itemTypeEqualsFunction === "purifyHelpers.Equatable.strictEquals") {
48
+ return "purifyHelpers.Equatable.booleanEquals"; // Use Maybe.equals
49
+ }
50
+ return `(left, right) => purifyHelpers.Maybes.equals(left, right, ${itemTypeEqualsFunction})`;
51
+ }
52
+ fromRdfExpression(parameters) {
53
+ return `purify.Either.of(${this.itemType.fromRdfExpression(parameters)}.toMaybe())`;
54
+ }
55
+ hashStatements({ variables, }) {
56
+ return [
57
+ `${variables.value}.ifJust((value) => { ${this.itemType
58
+ .hashStatements({
59
+ variables: {
60
+ hasher: variables.hasher,
61
+ value: "value",
62
+ },
63
+ })
64
+ .join("\n")} })`,
65
+ ];
66
+ }
67
+ propertySparqlGraphPatternExpression(parameters) {
68
+ return new Type.SparqlGraphPatternExpression(`sparqlBuilder.GraphPattern.optional(${this.itemType.propertySparqlGraphPatternExpression(parameters).toSparqlGraphPatternExpression()})`);
69
+ }
70
+ toRdfExpression({ variables, }) {
71
+ const itemTypeToRdfExpression = this.itemType.toRdfExpression({
72
+ variables: { ...variables, value: "value" },
73
+ });
74
+ if (itemTypeToRdfExpression === "value") {
75
+ return variables.value;
76
+ }
77
+ return `${variables.value}.map((value) => ${itemTypeToRdfExpression})`;
78
+ }
79
+ }
80
+ __decorate([
81
+ Memoize()
82
+ ], OptionType.prototype, "name", null);
83
+ //# sourceMappingURL=OptionType.js.map
@@ -0,0 +1,11 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { LiteralType } from "./LiteralType.js";
3
+ import type { Type } from "./Type.js";
4
+ export declare abstract class PrimitiveType extends LiteralType {
5
+ get conversions(): readonly Type.Conversion[];
6
+ get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
7
+ get importStatements(): readonly string[];
8
+ equalsFunction(): string;
9
+ toRdfExpression({ variables, }: Parameters<LiteralType["toRdfExpression"]>[0]): string;
10
+ }
11
+ //# sourceMappingURL=PrimitiveType.d.ts.map
@@ -0,0 +1,41 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { fromRdf } from "rdf-literal";
3
+ import { LiteralType } from "./LiteralType.js";
4
+ export class PrimitiveType extends LiteralType {
5
+ get conversions() {
6
+ const conversions = [
7
+ {
8
+ conversionExpression: (value) => value,
9
+ sourceTypeName: this.name,
10
+ },
11
+ ];
12
+ this.defaultValue.ifJust((defaultValue) => {
13
+ conversions.push({
14
+ conversionExpression: () => fromRdf(defaultValue, true),
15
+ sourceTypeName: "undefined",
16
+ });
17
+ });
18
+ return conversions;
19
+ }
20
+ get discriminatorProperty() {
21
+ return Maybe.empty();
22
+ }
23
+ get importStatements() {
24
+ return [];
25
+ }
26
+ equalsFunction() {
27
+ return "purifyHelpers.Equatable.strictEquals";
28
+ }
29
+ toRdfExpression({ variables, }) {
30
+ return this.defaultValue
31
+ .map((defaultValue) => {
32
+ let primitiveDefaultValue = fromRdf(defaultValue, true);
33
+ if (typeof primitiveDefaultValue === "string") {
34
+ primitiveDefaultValue = `"${primitiveDefaultValue}"`;
35
+ }
36
+ return `${variables.value} !== ${primitiveDefaultValue} ? ${variables.value} : undefined`;
37
+ })
38
+ .orDefault(variables.value);
39
+ }
40
+ }
41
+ //# sourceMappingURL=PrimitiveType.js.map
@@ -0,0 +1,25 @@
1
+ import type { BlankNode, Literal, NamedNode } from "@rdfjs/types";
2
+ import type { Maybe } from "purify-ts";
3
+ import { Type } from "./Type.js";
4
+ /**
5
+ * Abstract base class for IdentifierType and LiteralType.
6
+ */
7
+ export declare abstract class RdfjsTermType<RdfjsTermT extends BlankNode | Literal | NamedNode> extends Type {
8
+ readonly defaultValue: Maybe<RdfjsTermT>;
9
+ readonly hasValue: Maybe<RdfjsTermT>;
10
+ abstract readonly kind: "IdentifierType" | "LiteralType";
11
+ constructor({ defaultValue, hasValue, ...superParameters }: {
12
+ defaultValue: Maybe<RdfjsTermT>;
13
+ hasValue: Maybe<RdfjsTermT>;
14
+ } & ConstructorParameters<typeof Type>[0]);
15
+ equalsFunction(): string;
16
+ fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
17
+ propertySparqlGraphPatternExpression({ variables, }: Parameters<Type["propertySparqlGraphPatternExpression"]>[0]): Type.SparqlGraphPatternExpression;
18
+ toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
19
+ protected abstract fromRdfResourceValueExpression({ variables, }: {
20
+ variables: {
21
+ resourceValue: string;
22
+ };
23
+ }): string;
24
+ }
25
+ //# sourceMappingURL=RdfjsTermType.d.ts.map