@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.
- package/ShapesGraphToAstTransformer.d.ts +32 -0
- package/ShapesGraphToAstTransformer.js +461 -0
- package/ast/Ast.d.ts +5 -0
- package/ast/Ast.js +2 -0
- package/ast/IdentifierType.d.ts +13 -0
- package/ast/IdentifierType.js +2 -0
- package/ast/IntersectionType.d.ts +9 -0
- package/ast/IntersectionType.js +2 -0
- package/ast/LiteralType.d.ts +13 -0
- package/ast/LiteralType.js +2 -0
- package/ast/MintingStrategy.d.ts +8 -0
- package/ast/MintingStrategy.js +9 -0
- package/ast/Name.d.ts +13 -0
- package/ast/Name.js +2 -0
- package/ast/ObjectType.d.ts +88 -0
- package/ast/ObjectType.js +2 -0
- package/ast/OptionType.d.ts +11 -0
- package/ast/OptionType.js +2 -0
- package/ast/SetType.d.ts +12 -0
- package/ast/SetType.js +2 -0
- package/ast/Type.d.ts +9 -0
- package/ast/Type.js +2 -0
- package/ast/UnionType.d.ts +9 -0
- package/ast/UnionType.js +2 -0
- package/ast/index.d.ts +10 -0
- package/ast/index.js +10 -0
- package/generators/index.d.ts +3 -0
- package/generators/index.js +3 -0
- package/generators/json/AstJsonGenerator.d.ts +7 -0
- package/generators/json/AstJsonGenerator.js +103 -0
- package/generators/json/index.d.ts +2 -0
- package/generators/json/index.js +2 -0
- package/generators/ts/BooleanType.d.ts +7 -0
- package/generators/ts/BooleanType.js +13 -0
- package/generators/ts/Configuration.d.ts +29 -0
- package/generators/ts/Configuration.js +40 -0
- package/generators/ts/IdentifierType.d.ts +23 -0
- package/generators/ts/IdentifierType.js +83 -0
- package/generators/ts/IntersectionType.d.ts +14 -0
- package/generators/ts/IntersectionType.js +26 -0
- package/generators/ts/ListType.d.ts +26 -0
- package/generators/ts/ListType.js +119 -0
- package/generators/ts/LiteralType.d.ts +14 -0
- package/generators/ts/LiteralType.js +61 -0
- package/generators/ts/NumberType.d.ts +7 -0
- package/generators/ts/NumberType.js +13 -0
- package/generators/ts/ObjectType.d.ts +71 -0
- package/generators/ts/ObjectType.js +160 -0
- package/generators/ts/OptionType.d.ts +17 -0
- package/generators/ts/OptionType.js +83 -0
- package/generators/ts/PrimitiveType.d.ts +11 -0
- package/generators/ts/PrimitiveType.js +41 -0
- package/generators/ts/RdfjsTermType.d.ts +25 -0
- package/generators/ts/RdfjsTermType.js +46 -0
- package/generators/ts/SetType.d.ts +19 -0
- package/generators/ts/SetType.js +76 -0
- package/generators/ts/StringType.d.ts +7 -0
- package/generators/ts/StringType.js +13 -0
- package/generators/ts/TsGenerator.d.ts +15 -0
- package/generators/ts/TsGenerator.js +119 -0
- package/generators/ts/Type.d.ts +108 -0
- package/generators/ts/Type.js +83 -0
- package/generators/ts/TypeFactory.d.ts +15 -0
- package/generators/ts/TypeFactory.js +198 -0
- package/generators/ts/UnionType.d.ts +18 -0
- package/generators/ts/UnionType.js +165 -0
- package/generators/ts/_ObjectType/IdentifierProperty.d.ts +23 -0
- package/generators/ts/_ObjectType/IdentifierProperty.js +82 -0
- package/generators/ts/_ObjectType/Property.d.ts +38 -0
- package/generators/ts/_ObjectType/Property.js +11 -0
- package/generators/ts/_ObjectType/ShaclProperty.d.ts +25 -0
- package/generators/ts/_ObjectType/ShaclProperty.js +106 -0
- package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +29 -0
- package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +53 -0
- package/generators/ts/_ObjectType/classDeclaration.d.ts +4 -0
- package/generators/ts/_ObjectType/classDeclaration.js +154 -0
- package/generators/ts/_ObjectType/equalsFunctionDeclaration.d.ts +4 -0
- package/generators/ts/_ObjectType/equalsFunctionDeclaration.js +27 -0
- package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.d.ts +4 -0
- package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.js +72 -0
- package/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +5 -0
- package/generators/ts/_ObjectType/hashFunctionDeclaration.js +64 -0
- package/generators/ts/_ObjectType/index.d.ts +12 -0
- package/generators/ts/_ObjectType/index.js +12 -0
- package/generators/ts/_ObjectType/interfaceDeclaration.d.ts +4 -0
- package/generators/ts/_ObjectType/interfaceDeclaration.js +12 -0
- package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.d.ts +4 -0
- package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.js +46 -0
- package/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +4 -0
- package/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +53 -0
- package/generators/ts/index.d.ts +2 -0
- package/generators/ts/index.js +2 -0
- package/generators/ts/rdfjsTermExpression.d.ts +5 -0
- package/generators/ts/rdfjsTermExpression.js +15 -0
- package/generators/ts/stringToValidTsIdentifier.d.ts +2 -0
- package/generators/ts/stringToValidTsIdentifier.js +14 -0
- package/generators/ts/tsName.d.ts +3 -0
- package/generators/ts/tsName.js +22 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/logger.d.ts +2 -0
- package/logger.js +8 -0
- package/package.json +71 -0
- package/vocabularies/dashDataset.d.ts +3 -0
- package/vocabularies/dashDataset.js +2370 -0
- package/vocabularies/index.d.ts +2 -0
- package/vocabularies/index.js +2 -0
- package/vocabularies/shaclmate.d.ts +15 -0
- package/vocabularies/shaclmate.js +6 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import TermMap from "@rdfjs/term-map";
|
|
2
|
+
import { xsd } from "@tpluscode/rdf-ns-builders";
|
|
3
|
+
import { Maybe } from "purify-ts";
|
|
4
|
+
import { NodeKind } from "shacl-ast";
|
|
5
|
+
import { BooleanType } from "./BooleanType";
|
|
6
|
+
import { IdentifierType } from "./IdentifierType";
|
|
7
|
+
import { IntersectionType } from "./IntersectionType";
|
|
8
|
+
import { ListType } from "./ListType.js";
|
|
9
|
+
import { LiteralType } from "./LiteralType.js";
|
|
10
|
+
import { NumberType } from "./NumberType.js";
|
|
11
|
+
import { ObjectType } from "./ObjectType.js";
|
|
12
|
+
import { OptionType } from "./OptionType";
|
|
13
|
+
import { SetType } from "./SetType";
|
|
14
|
+
import { StringType } from "./StringType.js";
|
|
15
|
+
import { UnionType } from "./UnionType";
|
|
16
|
+
import { tsName } from "./tsName.js";
|
|
17
|
+
export class TypeFactory {
|
|
18
|
+
constructor({ configuration }) {
|
|
19
|
+
this.cachedObjectTypePropertiesByIdentifier = new TermMap();
|
|
20
|
+
this.cachedObjectTypesByIdentifier = new TermMap();
|
|
21
|
+
this.configuration = configuration;
|
|
22
|
+
}
|
|
23
|
+
createTypeFromAstType(astType) {
|
|
24
|
+
switch (astType.kind) {
|
|
25
|
+
case "IdentifierType":
|
|
26
|
+
return new IdentifierType({
|
|
27
|
+
configuration: this.configuration,
|
|
28
|
+
defaultValue: astType.defaultValue,
|
|
29
|
+
hasValue: astType.hasValue,
|
|
30
|
+
nodeKinds: astType.nodeKinds,
|
|
31
|
+
});
|
|
32
|
+
case "IntersectionType":
|
|
33
|
+
return new IntersectionType({
|
|
34
|
+
configuration: this.configuration,
|
|
35
|
+
memberTypes: astType.memberTypes.map((astType) => this.createTypeFromAstType(astType)),
|
|
36
|
+
});
|
|
37
|
+
case "LiteralType": {
|
|
38
|
+
const datatype = astType.datatype
|
|
39
|
+
.altLazy(() => astType.defaultValue.map((defaultValue) => defaultValue.datatype))
|
|
40
|
+
.altLazy(() => astType.hasValue.map((hasValue) => hasValue.datatype))
|
|
41
|
+
.extractNullable();
|
|
42
|
+
if (datatype !== null) {
|
|
43
|
+
if (datatype.equals(xsd.boolean)) {
|
|
44
|
+
return new BooleanType({
|
|
45
|
+
configuration: this.configuration,
|
|
46
|
+
defaultValue: astType.defaultValue,
|
|
47
|
+
hasValue: astType.hasValue,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (datatype.equals(xsd.integer)) {
|
|
51
|
+
return new NumberType({
|
|
52
|
+
configuration: this.configuration,
|
|
53
|
+
defaultValue: astType.defaultValue,
|
|
54
|
+
hasValue: astType.hasValue,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (datatype.equals(xsd.anyURI) || datatype.equals(xsd.string)) {
|
|
58
|
+
return new StringType({
|
|
59
|
+
configuration: this.configuration,
|
|
60
|
+
defaultValue: astType.defaultValue,
|
|
61
|
+
hasValue: astType.hasValue,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return new LiteralType({
|
|
66
|
+
configuration: this.configuration,
|
|
67
|
+
defaultValue: astType.defaultValue,
|
|
68
|
+
hasValue: astType.hasValue,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
case "ObjectType": {
|
|
72
|
+
if (astType.listItemType.isJust()) {
|
|
73
|
+
return new ListType({
|
|
74
|
+
configuration: this.configuration,
|
|
75
|
+
identifierNodeKind: astType.nodeKinds.has(NodeKind.BLANK_NODE)
|
|
76
|
+
? NodeKind.BLANK_NODE
|
|
77
|
+
: NodeKind.IRI,
|
|
78
|
+
itemType: this.createTypeFromAstType(astType.listItemType.unsafeCoerce()),
|
|
79
|
+
mintingStrategy: astType.mintingStrategy,
|
|
80
|
+
rdfType: astType.rdfType,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return this.createObjectTypeFromAstType(astType);
|
|
84
|
+
}
|
|
85
|
+
case "OptionType":
|
|
86
|
+
return new OptionType({
|
|
87
|
+
configuration: this.configuration,
|
|
88
|
+
itemType: this.createTypeFromAstType(astType.itemType),
|
|
89
|
+
});
|
|
90
|
+
case "SetType":
|
|
91
|
+
return new SetType({
|
|
92
|
+
configuration: this.configuration,
|
|
93
|
+
itemType: this.createTypeFromAstType(astType.itemType),
|
|
94
|
+
minCount: astType.minCount,
|
|
95
|
+
});
|
|
96
|
+
case "UnionType":
|
|
97
|
+
return new UnionType({
|
|
98
|
+
configuration: this.configuration,
|
|
99
|
+
memberTypes: astType.memberTypes.map((astType) => this.createTypeFromAstType(astType)),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
createObjectTypeFromAstType(astType) {
|
|
104
|
+
{
|
|
105
|
+
const cachedObjectType = this.cachedObjectTypesByIdentifier.get(astType.name.identifier);
|
|
106
|
+
if (cachedObjectType) {
|
|
107
|
+
return cachedObjectType;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const identifierType = new IdentifierType({
|
|
111
|
+
configuration: this.configuration,
|
|
112
|
+
defaultValue: Maybe.empty(),
|
|
113
|
+
hasValue: Maybe.empty(),
|
|
114
|
+
nodeKinds: astType.nodeKinds,
|
|
115
|
+
});
|
|
116
|
+
const objectType = new ObjectType({
|
|
117
|
+
abstract: astType.abstract,
|
|
118
|
+
configuration: this.configuration,
|
|
119
|
+
export_: astType.export,
|
|
120
|
+
identifierType,
|
|
121
|
+
lazyAncestorObjectTypes: () => astType.ancestorObjectTypes.map((astType) => this.createObjectTypeFromAstType(astType)),
|
|
122
|
+
lazyDescendantObjectTypes: () => astType.descendantObjectTypes.map((astType) => this.createObjectTypeFromAstType(astType)),
|
|
123
|
+
lazyParentObjectTypes: () => astType.parentObjectTypes.map((astType) => this.createObjectTypeFromAstType(astType)),
|
|
124
|
+
lazyProperties: () => {
|
|
125
|
+
const properties = astType.properties.map((astProperty) => this.createObjectTypePropertyFromAstProperty(astProperty));
|
|
126
|
+
if (astType.parentObjectTypes.length === 0) {
|
|
127
|
+
properties.push(new ObjectType.IdentifierProperty({
|
|
128
|
+
configuration: this.configuration,
|
|
129
|
+
mintingStrategy: astType.mintingStrategy,
|
|
130
|
+
name: this.configuration.objectTypeIdentifierPropertyName,
|
|
131
|
+
type: identifierType,
|
|
132
|
+
}));
|
|
133
|
+
} // Else parent will have the identifier property
|
|
134
|
+
// Type discriminator property
|
|
135
|
+
const typeDiscriminatorValues = new Set();
|
|
136
|
+
if (!astType.abstract) {
|
|
137
|
+
typeDiscriminatorValues.add(objectType.discriminatorValue);
|
|
138
|
+
}
|
|
139
|
+
for (const descendantObjectType of objectType.descendantObjectTypes) {
|
|
140
|
+
if (!descendantObjectType.abstract) {
|
|
141
|
+
typeDiscriminatorValues.add(descendantObjectType.discriminatorValue);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
properties.push(new ObjectType.TypeDiscriminatorProperty({
|
|
145
|
+
abstract: astType.abstract,
|
|
146
|
+
configuration: this.configuration,
|
|
147
|
+
name: this.configuration.objectTypeDiscriminatorPropertyName,
|
|
148
|
+
override: objectType.parentObjectTypes.length > 0 &&
|
|
149
|
+
!objectType.parentObjectTypes[0].abstract,
|
|
150
|
+
type: {
|
|
151
|
+
name: [...typeDiscriminatorValues]
|
|
152
|
+
.sort()
|
|
153
|
+
.map((name) => `"${name}"`)
|
|
154
|
+
.join("|"),
|
|
155
|
+
},
|
|
156
|
+
value: objectType.discriminatorValue,
|
|
157
|
+
}));
|
|
158
|
+
return properties.sort((left, right) => left.name.localeCompare(right.name));
|
|
159
|
+
},
|
|
160
|
+
mintingStrategy: astType.mintingStrategy,
|
|
161
|
+
name: tsName(astType.name),
|
|
162
|
+
rdfType: astType.rdfType,
|
|
163
|
+
});
|
|
164
|
+
this.cachedObjectTypesByIdentifier.set(astType.name.identifier, objectType);
|
|
165
|
+
return objectType;
|
|
166
|
+
}
|
|
167
|
+
createObjectTypePropertyFromAstProperty(astObjectTypeProperty) {
|
|
168
|
+
{
|
|
169
|
+
const cachedProperty = this.cachedObjectTypePropertiesByIdentifier.get(astObjectTypeProperty.name.identifier);
|
|
170
|
+
if (cachedProperty) {
|
|
171
|
+
return cachedProperty;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
let type;
|
|
175
|
+
if (astObjectTypeProperty.type.kind === "ObjectType" &&
|
|
176
|
+
!astObjectTypeProperty.inline) {
|
|
177
|
+
// Non-inlined object type = its identifier
|
|
178
|
+
type = new IdentifierType({
|
|
179
|
+
configuration: this.configuration,
|
|
180
|
+
defaultValue: Maybe.empty(),
|
|
181
|
+
hasValue: Maybe.empty(),
|
|
182
|
+
nodeKinds: astObjectTypeProperty.type.nodeKinds,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
type = this.createTypeFromAstType(astObjectTypeProperty.type);
|
|
187
|
+
}
|
|
188
|
+
const property = new ObjectType.ShaclProperty({
|
|
189
|
+
configuration: this.configuration,
|
|
190
|
+
name: tsName(astObjectTypeProperty.name),
|
|
191
|
+
path: astObjectTypeProperty.path.iri,
|
|
192
|
+
type,
|
|
193
|
+
});
|
|
194
|
+
this.cachedObjectTypePropertiesByIdentifier.set(astObjectTypeProperty.name.identifier, property);
|
|
195
|
+
return property;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=TypeFactory.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Maybe } from "purify-ts";
|
|
2
|
+
import { Type } from "./Type.js";
|
|
3
|
+
export declare class UnionType extends Type {
|
|
4
|
+
readonly kind = "UnionType";
|
|
5
|
+
readonly memberTypes: readonly Type[];
|
|
6
|
+
constructor({ memberTypes, ...superParameters }: ConstructorParameters<typeof Type>[0] & {
|
|
7
|
+
memberTypes: readonly Type[];
|
|
8
|
+
});
|
|
9
|
+
get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
|
|
10
|
+
get name(): string;
|
|
11
|
+
private get typesSharedDiscriminatorProperty();
|
|
12
|
+
equalsFunction(): string;
|
|
13
|
+
fromRdfExpression(parameters: Parameters<Type["fromRdfExpression"]>[0]): string;
|
|
14
|
+
hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
|
|
15
|
+
propertySparqlGraphPatternExpression(parameters: Parameters<Type["propertySparqlGraphPatternExpression"]>[0]): Type.SparqlGraphPatternExpression | Type.SparqlGraphPatternsExpression;
|
|
16
|
+
toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=UnionType.d.ts.map
|
|
@@ -0,0 +1,165 @@
|
|
|
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 { invariant } from "ts-invariant";
|
|
9
|
+
import { Memoize } from "typescript-memoize";
|
|
10
|
+
import { Type } from "./Type.js";
|
|
11
|
+
const syntheticTypeDiscriminatorPropertyName = "type";
|
|
12
|
+
function syntheticTypeDiscriminatorValue({ type, typeIndex, }) {
|
|
13
|
+
return `${typeIndex}-${type.name}`;
|
|
14
|
+
}
|
|
15
|
+
export class UnionType extends Type {
|
|
16
|
+
constructor({ memberTypes, ...superParameters }) {
|
|
17
|
+
super(superParameters);
|
|
18
|
+
this.kind = "UnionType";
|
|
19
|
+
invariant(memberTypes.length >= 2);
|
|
20
|
+
this.memberTypes = memberTypes;
|
|
21
|
+
}
|
|
22
|
+
get discriminatorProperty() {
|
|
23
|
+
return this.typesSharedDiscriminatorProperty.altLazy(() => Maybe.of({
|
|
24
|
+
name: syntheticTypeDiscriminatorPropertyName,
|
|
25
|
+
values: this.memberTypes.map((_, typeIndex) => typeIndex.toString()),
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
get name() {
|
|
29
|
+
if (this.typesSharedDiscriminatorProperty.isJust()) {
|
|
30
|
+
// If every type shares a discriminator (e.g., RDF/JS "termType" or generated ObjectType "type"),
|
|
31
|
+
// just join their names with "|"
|
|
32
|
+
return `(${this.memberTypes.map((type) => type.name).join(" | ")})`;
|
|
33
|
+
}
|
|
34
|
+
return `(${this.memberTypes.map((type, typeIndex) => `{ ${syntheticTypeDiscriminatorPropertyName}: "${syntheticTypeDiscriminatorValue({ type, typeIndex })}", value: ${type.name} }`).join(" | ")})`;
|
|
35
|
+
}
|
|
36
|
+
get typesSharedDiscriminatorProperty() {
|
|
37
|
+
// Do all the composed types share a single discriminator property?
|
|
38
|
+
let typesSharedDiscriminatorProperty;
|
|
39
|
+
for (const type of this.memberTypes) {
|
|
40
|
+
const typeDiscriminatorProperty = type.discriminatorProperty.extract();
|
|
41
|
+
if (!typeDiscriminatorProperty) {
|
|
42
|
+
return Maybe.empty();
|
|
43
|
+
}
|
|
44
|
+
if (!typesSharedDiscriminatorProperty) {
|
|
45
|
+
typesSharedDiscriminatorProperty = {
|
|
46
|
+
name: typeDiscriminatorProperty.name,
|
|
47
|
+
values: typeDiscriminatorProperty.values.concat(),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
else if (typeDiscriminatorProperty.name === typesSharedDiscriminatorProperty.name) {
|
|
51
|
+
typesSharedDiscriminatorProperty.values =
|
|
52
|
+
typesSharedDiscriminatorProperty.values.concat(typeDiscriminatorProperty.values);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return Maybe.empty();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return Maybe.fromNullable(typesSharedDiscriminatorProperty);
|
|
59
|
+
}
|
|
60
|
+
equalsFunction() {
|
|
61
|
+
return `
|
|
62
|
+
(left: ${this.name}, right: ${this.name}) => {
|
|
63
|
+
${this.memberTypes
|
|
64
|
+
.flatMap((type, typeIndex) => this.typesSharedDiscriminatorProperty
|
|
65
|
+
.map((typesSharedDiscriminatorProperty) => {
|
|
66
|
+
// Types share a discriminator property already, use it
|
|
67
|
+
return type.discriminatorProperty.unsafeCoerce().values.map((value) => `if (left.${typesSharedDiscriminatorProperty.name} === "${value}" && right.${typesSharedDiscriminatorProperty.name} === "${value}") {
|
|
68
|
+
return ${type.equalsFunction()}(left, right);
|
|
69
|
+
}`);
|
|
70
|
+
})
|
|
71
|
+
.orDefaultLazy(() => [
|
|
72
|
+
// Types don't share a discriminator property, have to use the one we synthesized
|
|
73
|
+
`if (left.${syntheticTypeDiscriminatorPropertyName} === "${syntheticTypeDiscriminatorValue({ type, typeIndex })}" && right.${syntheticTypeDiscriminatorPropertyName} === "${syntheticTypeDiscriminatorValue({ type, typeIndex })}") {
|
|
74
|
+
return ${type.equalsFunction()}(left.value, right.value);
|
|
75
|
+
}`,
|
|
76
|
+
]))
|
|
77
|
+
.join("\n")}
|
|
78
|
+
|
|
79
|
+
return purify.Left({ left, right, propertyName: "type", propertyValuesUnequal: { left: typeof left, right: typeof right, type: "BooleanEquals" }, type: "Property" });
|
|
80
|
+
}`;
|
|
81
|
+
}
|
|
82
|
+
fromRdfExpression(parameters) {
|
|
83
|
+
let expression = "";
|
|
84
|
+
this.memberTypes.forEach((type, typeIndex) => {
|
|
85
|
+
let typeExpression = type.fromRdfExpression(parameters);
|
|
86
|
+
if (!this.typesSharedDiscriminatorProperty.isJust()) {
|
|
87
|
+
typeExpression = `${typeExpression}.map(value => ({ type: "${typeIndex}-${type.name}" as const, value }) as (${this.name}))`;
|
|
88
|
+
}
|
|
89
|
+
typeExpression = `(${typeExpression} as purify.Either<rdfjsResource.Resource.ValueError, ${this.name}>)`;
|
|
90
|
+
expression =
|
|
91
|
+
expression.length > 0
|
|
92
|
+
? `${expression}.altLazy(() => ${typeExpression})`
|
|
93
|
+
: typeExpression;
|
|
94
|
+
});
|
|
95
|
+
return expression;
|
|
96
|
+
}
|
|
97
|
+
hashStatements({ variables, }) {
|
|
98
|
+
const caseBlocks = [];
|
|
99
|
+
this.memberTypes.forEach((type, typeIndex) => {
|
|
100
|
+
if (this.typesSharedDiscriminatorProperty.isJust()) {
|
|
101
|
+
for (const typeDiscriminatorPropertyValue of type.discriminatorProperty.unsafeCoerce()
|
|
102
|
+
.values) {
|
|
103
|
+
caseBlocks.push(`case "${typeDiscriminatorPropertyValue}": { ${type.hashStatements({
|
|
104
|
+
variables,
|
|
105
|
+
})}; break; }`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
caseBlocks.push(`case "${syntheticTypeDiscriminatorValue({
|
|
110
|
+
type,
|
|
111
|
+
typeIndex,
|
|
112
|
+
})}": { ${type.hashStatements({ variables: { hasher: variables.hasher, value: `${variables}.value` } })}; break; }`);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
const switchValue = this.typesSharedDiscriminatorProperty
|
|
116
|
+
.map((typeSharedDiscriminatorProperty) => `${variables.value}.${typeSharedDiscriminatorProperty.name}`)
|
|
117
|
+
.orDefaultLazy(() => `${variables.value}.${syntheticTypeDiscriminatorPropertyName}`);
|
|
118
|
+
return [`switch (${switchValue}) { ${caseBlocks.join("\n")} }`];
|
|
119
|
+
}
|
|
120
|
+
propertySparqlGraphPatternExpression(parameters) {
|
|
121
|
+
return new Type.SparqlGraphPatternExpression(`sparqlBuilder.GraphPattern.union(${this.memberTypes
|
|
122
|
+
.map((type) => type
|
|
123
|
+
.propertySparqlGraphPatternExpression(parameters)
|
|
124
|
+
.toSparqlGraphPatternExpression()
|
|
125
|
+
.toString())
|
|
126
|
+
.join(", ")})`);
|
|
127
|
+
}
|
|
128
|
+
toRdfExpression({ variables, }) {
|
|
129
|
+
let expression = "";
|
|
130
|
+
this.memberTypes.forEach((type, typeIndex) => {
|
|
131
|
+
if (this.typesSharedDiscriminatorProperty.isJust()) {
|
|
132
|
+
if (expression.length === 0) {
|
|
133
|
+
expression = type.toRdfExpression({
|
|
134
|
+
variables,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
expression = `(${type.discriminatorProperty
|
|
139
|
+
.unsafeCoerce()
|
|
140
|
+
.values.map((value) => `${variables.value}.${this.typesSharedDiscriminatorProperty.unsafeCoerce().name} === "${value}"`)
|
|
141
|
+
.join(" || ")}) ? ${type.toRdfExpression({ variables })} : ${expression}`;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (expression.length === 0) {
|
|
146
|
+
expression = type.toRdfExpression({
|
|
147
|
+
variables: { ...variables, value: `${variables.value}.value` },
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// No shared type discriminator between the types, use the one we synthesized
|
|
152
|
+
expression = `(${variables.value}.${syntheticTypeDiscriminatorPropertyName} === "${syntheticTypeDiscriminatorValue({ type, typeIndex })}") ? (${type.toRdfExpression({ variables: { ...variables, value: `${variables.value}.value` } })}) : (${expression})`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
return expression;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
__decorate([
|
|
160
|
+
Memoize()
|
|
161
|
+
], UnionType.prototype, "discriminatorProperty", null);
|
|
162
|
+
__decorate([
|
|
163
|
+
Memoize()
|
|
164
|
+
], UnionType.prototype, "typesSharedDiscriminatorProperty", null);
|
|
165
|
+
//# sourceMappingURL=UnionType.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Maybe } from "purify-ts";
|
|
2
|
+
import { type GetAccessorDeclarationStructure, type OptionalKind, type PropertyDeclarationStructure, type PropertySignatureStructure } from "ts-morph";
|
|
3
|
+
import { MintingStrategy } from "../../../ast";
|
|
4
|
+
import type { IdentifierType } from "../IdentifierType.js";
|
|
5
|
+
import { Property } from "./Property.js";
|
|
6
|
+
export declare class IdentifierProperty extends Property<IdentifierType> {
|
|
7
|
+
readonly equalsFunction = "purifyHelpers.Equatable.booleanEquals";
|
|
8
|
+
private readonly mintingStrategy;
|
|
9
|
+
constructor({ mintingStrategy, ...superParameters }: {
|
|
10
|
+
mintingStrategy: Maybe<MintingStrategy>;
|
|
11
|
+
type: IdentifierType;
|
|
12
|
+
} & ConstructorParameters<typeof Property>[0]);
|
|
13
|
+
get classConstructorParametersPropertySignature(): Maybe<OptionalKind<PropertySignatureStructure>>;
|
|
14
|
+
get classGetAccessorDeclaration(): Maybe<OptionalKind<GetAccessorDeclarationStructure>>;
|
|
15
|
+
get classPropertyDeclaration(): OptionalKind<PropertyDeclarationStructure>;
|
|
16
|
+
get interfacePropertySignature(): OptionalKind<PropertySignatureStructure>;
|
|
17
|
+
classConstructorStatements({ variables, }: Parameters<Property<IdentifierType>["classConstructorStatements"]>[0]): readonly string[];
|
|
18
|
+
fromRdfStatements({ variables, }: Parameters<Property<IdentifierType>["fromRdfStatements"]>[0]): readonly string[];
|
|
19
|
+
hashStatements(parameters: Parameters<Property<IdentifierType>["hashStatements"]>[0]): readonly string[];
|
|
20
|
+
sparqlGraphPatternExpression(): Maybe<string>;
|
|
21
|
+
toRdfStatements(): readonly string[];
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=IdentifierProperty.d.ts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Maybe } from "purify-ts";
|
|
2
|
+
import { Scope, } from "ts-morph";
|
|
3
|
+
import { MintingStrategy } from "../../../ast";
|
|
4
|
+
import { Property } from "./Property.js";
|
|
5
|
+
export class IdentifierProperty extends Property {
|
|
6
|
+
constructor({ mintingStrategy, ...superParameters }) {
|
|
7
|
+
super(superParameters);
|
|
8
|
+
this.equalsFunction = "purifyHelpers.Equatable.booleanEquals";
|
|
9
|
+
this.mintingStrategy = mintingStrategy;
|
|
10
|
+
}
|
|
11
|
+
get classConstructorParametersPropertySignature() {
|
|
12
|
+
return Maybe.of({
|
|
13
|
+
hasQuestionToken: this.mintingStrategy.isJust(),
|
|
14
|
+
isReadonly: true,
|
|
15
|
+
name: this.name,
|
|
16
|
+
type: this.type.name,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
get classGetAccessorDeclaration() {
|
|
20
|
+
if (!this.mintingStrategy.isJust()) {
|
|
21
|
+
return Maybe.empty();
|
|
22
|
+
}
|
|
23
|
+
let mintIdentifier;
|
|
24
|
+
switch (this.mintingStrategy.unsafeCoerce()) {
|
|
25
|
+
case MintingStrategy.SHA256:
|
|
26
|
+
mintIdentifier =
|
|
27
|
+
"dataFactory.namedNode(`urn:shaclmate:object:${this.type}:${this.hash(sha256.create())}`)";
|
|
28
|
+
break;
|
|
29
|
+
case MintingStrategy.UUIDv4:
|
|
30
|
+
mintIdentifier =
|
|
31
|
+
"dataFactory.namedNode(`urn:shaclmate:object:${this.type}:${uuid.v4()}`)";
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
return Maybe.of({
|
|
35
|
+
name: this.name,
|
|
36
|
+
returnType: this.type.name,
|
|
37
|
+
statements: [
|
|
38
|
+
`if (typeof this._${this.name} === "undefined") { this._${this.name} = ${mintIdentifier}; } return this._${this.name};`,
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
get classPropertyDeclaration() {
|
|
43
|
+
if (this.mintingStrategy.isJust()) {
|
|
44
|
+
// Mutable _identifier that will be lazily initialized by the getter
|
|
45
|
+
return {
|
|
46
|
+
name: `_${this.name}`,
|
|
47
|
+
scope: Scope.Private,
|
|
48
|
+
type: `${this.type.name} | undefined`,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
isReadonly: true,
|
|
53
|
+
name: this.name,
|
|
54
|
+
type: this.type.name,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
get interfacePropertySignature() {
|
|
58
|
+
return {
|
|
59
|
+
isReadonly: true,
|
|
60
|
+
name: this.name,
|
|
61
|
+
type: this.type.name,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
classConstructorStatements({ variables, }) {
|
|
65
|
+
return [
|
|
66
|
+
`this.${this.mintingStrategy.isJust() ? "_" : ""}${this.name} = ${variables.parameter};`,
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
fromRdfStatements({ variables, }) {
|
|
70
|
+
return [`const ${this.name} = ${variables.resource}.identifier`];
|
|
71
|
+
}
|
|
72
|
+
hashStatements(parameters) {
|
|
73
|
+
return this.type.hashStatements(parameters);
|
|
74
|
+
}
|
|
75
|
+
sparqlGraphPatternExpression() {
|
|
76
|
+
return Maybe.empty();
|
|
77
|
+
}
|
|
78
|
+
toRdfStatements() {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=IdentifierProperty.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Maybe } from "purify-ts";
|
|
2
|
+
import type { GetAccessorDeclarationStructure, OptionalKind, PropertyDeclarationStructure, PropertySignatureStructure } from "ts-morph";
|
|
3
|
+
import type { Configuration } from "../Configuration.js";
|
|
4
|
+
import type { Type } from "../Type.js";
|
|
5
|
+
export declare abstract class Property<TypeT extends {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
}> {
|
|
8
|
+
abstract readonly classConstructorParametersPropertySignature: Maybe<OptionalKind<PropertySignatureStructure>>;
|
|
9
|
+
abstract readonly classGetAccessorDeclaration: Maybe<OptionalKind<GetAccessorDeclarationStructure>>;
|
|
10
|
+
abstract readonly classPropertyDeclaration: OptionalKind<PropertyDeclarationStructure>;
|
|
11
|
+
abstract readonly equalsFunction: string;
|
|
12
|
+
abstract readonly interfacePropertySignature: OptionalKind<PropertySignatureStructure>;
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly type: TypeT;
|
|
15
|
+
protected readonly configuration: Configuration;
|
|
16
|
+
constructor({ configuration, name, type, }: {
|
|
17
|
+
configuration: Configuration;
|
|
18
|
+
name: string;
|
|
19
|
+
type: TypeT;
|
|
20
|
+
});
|
|
21
|
+
get importStatements(): readonly string[];
|
|
22
|
+
abstract classConstructorStatements(parameters: {
|
|
23
|
+
variables: {
|
|
24
|
+
parameter: string;
|
|
25
|
+
};
|
|
26
|
+
}): readonly string[];
|
|
27
|
+
abstract fromRdfStatements(parameters: {
|
|
28
|
+
variables: {
|
|
29
|
+
resource: string;
|
|
30
|
+
};
|
|
31
|
+
}): readonly string[];
|
|
32
|
+
abstract hashStatements(parameters: Parameters<Type["hashStatements"]>[0]): readonly string[];
|
|
33
|
+
abstract sparqlGraphPatternExpression(): Maybe<string>;
|
|
34
|
+
abstract toRdfStatements(parameters: {
|
|
35
|
+
variables: Omit<Parameters<Type["toRdfExpression"]>[0]["variables"], "predicate">;
|
|
36
|
+
}): readonly string[];
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=Property.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type * as rdfjs from "@rdfjs/types";
|
|
2
|
+
import { Maybe } from "purify-ts";
|
|
3
|
+
import type { GetAccessorDeclarationStructure, OptionalKind, PropertyDeclarationStructure, PropertySignatureStructure } from "ts-morph";
|
|
4
|
+
import type { Type } from "../Type.js";
|
|
5
|
+
import { Property } from "./Property.js";
|
|
6
|
+
export declare class ShaclProperty extends Property<Type> {
|
|
7
|
+
private readonly path;
|
|
8
|
+
constructor({ path, ...superParameters }: {
|
|
9
|
+
path: rdfjs.NamedNode;
|
|
10
|
+
type: Type;
|
|
11
|
+
} & ConstructorParameters<typeof Property>[0]);
|
|
12
|
+
get classConstructorParametersPropertySignature(): Maybe<OptionalKind<PropertySignatureStructure>>;
|
|
13
|
+
get classGetAccessorDeclaration(): Maybe<OptionalKind<GetAccessorDeclarationStructure>>;
|
|
14
|
+
get classPropertyDeclaration(): OptionalKind<PropertyDeclarationStructure>;
|
|
15
|
+
get equalsFunction(): string;
|
|
16
|
+
get importStatements(): readonly string[];
|
|
17
|
+
get interfacePropertySignature(): OptionalKind<PropertySignatureStructure>;
|
|
18
|
+
private get pathExpression();
|
|
19
|
+
classConstructorStatements({ variables, }: Parameters<Property<Type>["classConstructorStatements"]>[0]): readonly string[];
|
|
20
|
+
fromRdfStatements({ variables, }: Parameters<Property<Type>["fromRdfStatements"]>[0]): readonly string[];
|
|
21
|
+
hashStatements(parameters: Parameters<Property<Type>["hashStatements"]>[0]): readonly string[];
|
|
22
|
+
sparqlGraphPatternExpression(): Maybe<string>;
|
|
23
|
+
toRdfStatements({ variables, }: Parameters<Property<Type>["toRdfStatements"]>[0]): readonly string[];
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=ShaclProperty.d.ts.map
|
|
@@ -0,0 +1,106 @@
|
|
|
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 { pascalCase } from "change-case";
|
|
8
|
+
import { Maybe } from "purify-ts";
|
|
9
|
+
import { Memoize } from "typescript-memoize";
|
|
10
|
+
import { Property } from "./Property.js";
|
|
11
|
+
export class ShaclProperty extends Property {
|
|
12
|
+
constructor({ path, ...superParameters }) {
|
|
13
|
+
super(superParameters);
|
|
14
|
+
this.path = path;
|
|
15
|
+
}
|
|
16
|
+
get classConstructorParametersPropertySignature() {
|
|
17
|
+
let hasQuestionToken = false;
|
|
18
|
+
const typeNames = new Set(); // Remove duplicates with a set
|
|
19
|
+
for (const conversion of this.type.conversions) {
|
|
20
|
+
if (conversion.sourceTypeName === "undefined") {
|
|
21
|
+
hasQuestionToken = true;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
typeNames.add(conversion.sourceTypeName);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return Maybe.of({
|
|
28
|
+
hasQuestionToken,
|
|
29
|
+
isReadonly: true,
|
|
30
|
+
name: this.name,
|
|
31
|
+
type: [...typeNames].sort().join(" | "),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
get classGetAccessorDeclaration() {
|
|
35
|
+
return Maybe.empty();
|
|
36
|
+
}
|
|
37
|
+
get classPropertyDeclaration() {
|
|
38
|
+
return {
|
|
39
|
+
isReadonly: true,
|
|
40
|
+
name: this.name,
|
|
41
|
+
type: this.type.name,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
get equalsFunction() {
|
|
45
|
+
return this.type.equalsFunction();
|
|
46
|
+
}
|
|
47
|
+
get importStatements() {
|
|
48
|
+
return this.type.importStatements;
|
|
49
|
+
}
|
|
50
|
+
get interfacePropertySignature() {
|
|
51
|
+
return {
|
|
52
|
+
isReadonly: true,
|
|
53
|
+
name: this.name,
|
|
54
|
+
type: this.type.name,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
get pathExpression() {
|
|
58
|
+
return `${this.configuration.dataFactoryVariable}.namedNode("${this.path.value}")`;
|
|
59
|
+
}
|
|
60
|
+
classConstructorStatements({ variables, }) {
|
|
61
|
+
const typeConversions = this.type.conversions;
|
|
62
|
+
if (typeConversions.length === 1) {
|
|
63
|
+
return [`this.${this.name} = ${variables.parameter};`];
|
|
64
|
+
}
|
|
65
|
+
const statements = [];
|
|
66
|
+
for (const conversion of this.type.conversions) {
|
|
67
|
+
statements.push(`if (${conversion.sourceTypeCheckExpression ? conversion.sourceTypeCheckExpression(variables.parameter) : `typeof ${variables.parameter} === "${conversion.sourceTypeName}"`}) { this.${this.name} = ${conversion.conversionExpression(variables.parameter)}; }`);
|
|
68
|
+
}
|
|
69
|
+
// We shouldn't need this else, since the parameter now has the never type, but have to add it to appease the TypeScript compiler
|
|
70
|
+
statements.push(`{ this.${this.name} = ${variables.parameter}; // never\n }`);
|
|
71
|
+
return [statements.join(" else ")];
|
|
72
|
+
}
|
|
73
|
+
fromRdfStatements({ variables, }) {
|
|
74
|
+
return [
|
|
75
|
+
`const _${this.name}Either: purify.Either<rdfjsResource.Resource.ValueError, ${this.type.name}> = ${this.type.fromRdfExpression({ variables: { ...variables, predicate: this.pathExpression, resourceValues: `${variables.resource}.values(${this.pathExpression}, { unique: true })` } })};`,
|
|
76
|
+
`if (_${this.name}Either.isLeft()) { return _${this.name}Either; }`,
|
|
77
|
+
`const ${this.name} = _${this.name}Either.unsafeCoerce();`,
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
hashStatements(parameters) {
|
|
81
|
+
return this.type.hashStatements(parameters);
|
|
82
|
+
}
|
|
83
|
+
sparqlGraphPatternExpression() {
|
|
84
|
+
return Maybe.of(this.type
|
|
85
|
+
.propertySparqlGraphPatternExpression({
|
|
86
|
+
variables: {
|
|
87
|
+
object: `this.variable("${pascalCase(this.name)}")`,
|
|
88
|
+
predicate: this.pathExpression,
|
|
89
|
+
subject: "this.subject",
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
.toSparqlGraphPatternExpression()
|
|
93
|
+
.toString());
|
|
94
|
+
}
|
|
95
|
+
toRdfStatements({ variables, }) {
|
|
96
|
+
return [
|
|
97
|
+
`${variables.resource}.add(${this.pathExpression}, ${this.type.toRdfExpression({
|
|
98
|
+
variables: { ...variables, predicate: this.pathExpression },
|
|
99
|
+
})});`,
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
__decorate([
|
|
104
|
+
Memoize()
|
|
105
|
+
], ShaclProperty.prototype, "pathExpression", null);
|
|
106
|
+
//# sourceMappingURL=ShaclProperty.js.map
|