@shaclmate/compiler 2.0.23 → 2.0.24

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 (96) hide show
  1. package/dist/ShapesGraphToAstTransformer.d.ts +5 -5
  2. package/dist/ShapesGraphToAstTransformer.js +5 -5
  3. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.d.ts +2 -0
  4. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.js +2 -0
  5. package/dist/_ShapesGraphToAstTransformer/ShapeStack.d.ts +15 -0
  6. package/dist/_ShapesGraphToAstTransformer/ShapeStack.js +51 -0
  7. package/dist/_ShapesGraphToAstTransformer/index.d.ts +5 -5
  8. package/dist/_ShapesGraphToAstTransformer/index.js +5 -5
  9. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +77 -29
  10. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.d.ts +10 -0
  11. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.js +185 -0
  12. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.d.ts +10 -0
  13. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.js +32 -0
  14. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.d.ts +10 -0
  15. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.js +46 -0
  16. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.d.ts +12 -0
  17. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.js +24 -0
  18. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.d.ts +13 -0
  19. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.js +14 -0
  20. package/dist/ast/OptionType.d.ts +0 -2
  21. package/dist/ast/SetType.d.ts +1 -6
  22. package/dist/ast/index.d.ts +1 -0
  23. package/dist/ast/index.js +1 -0
  24. package/dist/generators/json/AstJsonGenerator.js +7 -1
  25. package/dist/generators/ts/BooleanType.d.ts +1 -1
  26. package/dist/generators/ts/BooleanType.js +1 -1
  27. package/dist/generators/ts/DateTimeType.d.ts +1 -1
  28. package/dist/generators/ts/DateTimeType.js +1 -1
  29. package/dist/generators/ts/IdentifierType.d.ts +6 -1
  30. package/dist/generators/ts/IdentifierType.js +20 -14
  31. package/dist/generators/ts/ListType.js +16 -5
  32. package/dist/generators/ts/LiteralType.d.ts +9 -2
  33. package/dist/generators/ts/LiteralType.js +79 -15
  34. package/dist/generators/ts/NumberType.d.ts +1 -1
  35. package/dist/generators/ts/NumberType.js +1 -1
  36. package/dist/generators/ts/ObjectType.d.ts +1 -2
  37. package/dist/generators/ts/ObjectType.js +4 -5
  38. package/dist/generators/ts/ObjectUnionType.d.ts +1 -0
  39. package/dist/generators/ts/ObjectUnionType.js +18 -9
  40. package/dist/generators/ts/OptionType.d.ts +1 -1
  41. package/dist/generators/ts/OptionType.js +6 -4
  42. package/dist/generators/ts/PrimitiveType.d.ts +19 -1
  43. package/dist/generators/ts/PrimitiveType.js +16 -3
  44. package/dist/generators/ts/SetType.d.ts +4 -4
  45. package/dist/generators/ts/SetType.js +15 -12
  46. package/dist/generators/ts/SnippetDeclarations.d.ts +1 -1
  47. package/dist/generators/ts/SnippetDeclarations.js +13 -2
  48. package/dist/generators/ts/StringType.d.ts +1 -1
  49. package/dist/generators/ts/StringType.js +8 -8
  50. package/dist/generators/ts/TermType.d.ts +7 -19
  51. package/dist/generators/ts/TermType.js +44 -51
  52. package/dist/generators/ts/TsGenerator.js +4 -1
  53. package/dist/generators/ts/Type.d.ts +12 -2
  54. package/dist/generators/ts/Type.js +1 -0
  55. package/dist/generators/ts/TypeFactory.d.ts +8 -0
  56. package/dist/generators/ts/TypeFactory.js +112 -22
  57. package/dist/generators/ts/UnionType.js +1 -1
  58. package/dist/generators/ts/_ObjectType/EagerShaclProperty.js +3 -2
  59. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.d.ts +1 -1
  60. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.js +15 -1
  61. package/dist/generators/ts/_ObjectType/IdentifierProperty.d.ts +6 -6
  62. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +59 -29
  63. package/dist/generators/ts/_ObjectType/LazyShaclProperty.d.ts +14 -1
  64. package/dist/generators/ts/_ObjectType/LazyShaclProperty.js +89 -11
  65. package/dist/generators/ts/_ObjectType/Property.d.ts +9 -3
  66. package/dist/generators/ts/_ObjectType/ShaclProperty.d.ts +1 -1
  67. package/dist/generators/ts/_ObjectType/ShaclProperty.js +14 -2
  68. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +1 -1
  69. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +1 -1
  70. package/dist/generators/ts/_ObjectType/classDeclaration.js +16 -12
  71. package/dist/generators/ts/_ObjectType/createFunctionDeclaration.js +22 -10
  72. package/dist/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +3 -1
  73. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.js +14 -2
  74. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.js +71 -79
  75. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +3 -3
  76. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +1 -1
  77. package/dist/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +6 -2
  78. package/dist/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +19 -12
  79. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.js +5 -5
  80. package/dist/generators/ts/sparqlObjectSetClassDeclaration.js +6 -6
  81. package/dist/input/PropertyPath.d.ts +1 -2
  82. package/dist/input/PropertyPath.js +6 -8
  83. package/dist/input/ShapesGraph.js +4 -8
  84. package/dist/input/generated.d.ts +96 -54
  85. package/dist/input/generated.js +1350 -744
  86. package/package.json +5 -5
  87. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +0 -12
  88. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +0 -193
  89. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +0 -12
  90. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +0 -29
  91. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +0 -12
  92. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +0 -43
  93. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +0 -14
  94. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +0 -21
  95. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +0 -15
  96. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +0 -60
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Try to convert a shape to a type using some heuristics.
3
+ *
4
+ * We don't try to handle exotic cases allowed by the SHACL spec, such as combinations of sh:in and sh:node. Instead we assume
5
+ * a shape has one type.
6
+ */
7
+ export function transformShapeToAstType(shape, shapeStack) {
8
+ // Try to transform the property shape into an AST type without cardinality constraints
9
+ return this.transformShapeToAstCompositeType(shape, shapeStack)
10
+ .altLazy(() => this.transformShapeToAstIdentifierType(shape, shapeStack))
11
+ .altLazy(() => this.transformShapeToAstLiteralType(shape, shapeStack))
12
+ .altLazy(() => this.transformShapeToAstTermType(shape, shapeStack));
13
+ }
14
+ //# sourceMappingURL=transformShapeToAstType.js.map
@@ -1,8 +1,6 @@
1
1
  import type { Type } from "./Type.js";
2
2
  /**
3
3
  * A type with zero or one values of an item type.
4
- *
5
- * This is a property with sh:minCount 0 and sh:maxCount 1.
6
4
  */
7
5
  export interface OptionType<ItemTypeT extends Type = Type> {
8
6
  readonly itemType: ItemTypeT;
@@ -2,15 +2,10 @@ import type { Maybe } from "purify-ts";
2
2
  import type { Type } from "./Type.js";
3
3
  /**
4
4
  * An unordered set of items of a specific type.
5
- *
6
- * This is a property with sh:maxCount != 1.
7
5
  */
8
6
  export interface SetType<ItemTypeT extends Type = Type> {
9
- /**
10
- * Set item type.
11
- */
12
- readonly itemType: ItemTypeT;
13
7
  readonly kind: "SetType";
8
+ readonly itemType: ItemTypeT;
14
9
  /**
15
10
  * Minimum number of items in the set.
16
11
  */
@@ -7,6 +7,7 @@ export * from "./Name.js";
7
7
  export * from "./ObjectIntersectionType.js";
8
8
  export * from "./ObjectType.js";
9
9
  export * from "./ObjectUnionType.js";
10
+ export * from "./SetType.js";
10
11
  export * from "./UnionType.js";
11
12
  export * from "./TermType.js";
12
13
  export * from "./Type.js";
package/dist/ast/index.js CHANGED
@@ -7,6 +7,7 @@ export * from "./Name.js";
7
7
  export * from "./ObjectIntersectionType.js";
8
8
  export * from "./ObjectType.js";
9
9
  export * from "./ObjectUnionType.js";
10
+ export * from "./SetType.js";
10
11
  export * from "./UnionType.js";
11
12
  export * from "./TermType.js";
12
13
  export * from "./Type.js";
@@ -119,11 +119,17 @@ export class AstJsonGenerator {
119
119
  kind: objectType.kind,
120
120
  name: nameToJson(objectType.name),
121
121
  properties: objectType.properties.map((property) => ({
122
- name: nameToJson(property.name),
122
+ comment: property.comment.extract(),
123
+ description: property.description.extract(),
124
+ label: property.label.extract(),
123
125
  mutable: property.mutable.extract(),
126
+ name: nameToJson(property.name),
127
+ order: property.order,
124
128
  path: property.path.iri.value,
125
129
  recursive: property.recursive ? true : undefined,
130
+ stubType: property.stubType.map(typeToJson).extract(),
126
131
  type: typeToJson(property.type),
132
+ visibility: property.visibility,
127
133
  })),
128
134
  })),
129
135
  }, undefined, 2);
@@ -7,7 +7,7 @@ export declare class BooleanType extends PrimitiveType<boolean> {
7
7
  get graphqlName(): Type.GraphqlName;
8
8
  get name(): string;
9
9
  jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
10
- propertyFromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<boolean>["propertyFromRdfResourceValueExpression"]>[0]): string;
10
+ fromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<boolean>["fromRdfResourceValueExpression"]>[0]): string;
11
11
  toRdfExpression({ variables, }: Parameters<PrimitiveType<string>["toRdfExpression"]>[0]): string;
12
12
  }
13
13
  //# sourceMappingURL=BooleanType.d.ts.map
@@ -43,7 +43,7 @@ export class BooleanType extends PrimitiveType {
43
43
  }
44
44
  return `${variables.zod}.boolean()`;
45
45
  }
46
- propertyFromRdfResourceValueExpression({ variables, }) {
46
+ fromRdfResourceValueExpression({ variables, }) {
47
47
  let expression = `${variables.resourceValue}.toBoolean()`;
48
48
  if (this.primitiveIn.length === 1) {
49
49
  const eitherTypeParameters = `<Error, ${this.name}>`;
@@ -15,7 +15,7 @@ export declare class DateTimeType extends PrimitiveType<Date> {
15
15
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
16
16
  hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
17
17
  jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
18
- propertyFromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<number>["propertyFromRdfResourceValueExpression"]>[0]): string;
18
+ fromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<number>["fromRdfResourceValueExpression"]>[0]): string;
19
19
  snippetDeclarations({ features, }: Parameters<PrimitiveType<Date>["snippetDeclarations"]>[0]): readonly string[];
20
20
  toJsonExpression({ variables, }: Parameters<PrimitiveType<Date>["toJsonExpression"]>[0]): string;
21
21
  toRdfExpression({ variables, }: Parameters<PrimitiveType<Date>["toRdfExpression"]>[0]): string;
@@ -54,7 +54,7 @@ export class DateTimeType extends PrimitiveType {
54
54
  jsonZodSchema({ variables, }) {
55
55
  return `${variables.zod}.string().${this.zodDatatype}()`;
56
56
  }
57
- propertyFromRdfResourceValueExpression({ variables, }) {
57
+ fromRdfResourceValueExpression({ variables, }) {
58
58
  let expression = `${variables.resourceValue}.toDate()`;
59
59
  if (this.primitiveIn.length > 0) {
60
60
  const eitherTypeParameters = `<Error, ${this.name}>`;
@@ -12,9 +12,14 @@ export declare class IdentifierType extends TermType<NamedNode, BlankNode | Name
12
12
  get name(): string;
13
13
  get toStringFunctionDeclaration(): VariableStatementStructure;
14
14
  fromJsonExpression({ variables, }: Parameters<TermType<NamedNode, BlankNode | NamedNode>["fromJsonExpression"]>[0]): string;
15
+ protected fromRdfExpressionChain({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): {
16
+ defaultValue?: string;
17
+ hasValues?: string;
18
+ languageIn?: string;
19
+ valueTo?: string;
20
+ };
15
21
  graphqlResolveExpression({ variables: { value }, }: Parameters<Type["graphqlResolveExpression"]>[0]): string;
16
22
  jsonZodSchema({ variables, }: Parameters<TermType<NamedNode, BlankNode | NamedNode>["jsonZodSchema"]>[0]): ReturnType<TermType<NamedNode, BlankNode | NamedNode>["jsonZodSchema"]>;
17
23
  toJsonExpression({ variables, }: Parameters<TermType<NamedNode, BlankNode | NamedNode>["toJsonExpression"]>[0]): string;
18
- protected propertyFromRdfResourceValueExpression({ variables, }: Parameters<TermType<NamedNode, BlankNode | NamedNode>["propertyFromRdfResourceValueExpression"]>[0]): string;
19
24
  }
20
25
  //# sourceMappingURL=IdentifierType.d.ts.map
@@ -121,6 +121,26 @@ export class IdentifierType extends TermType {
121
121
  return valueToNamedNode;
122
122
  }
123
123
  }
124
+ fromRdfExpressionChain({ variables, }) {
125
+ let valueToExpression;
126
+ if (this.nodeKinds.size === 2) {
127
+ valueToExpression = "value.toIdentifier()";
128
+ }
129
+ else if (this.isNamedNodeKind) {
130
+ valueToExpression = "value.toIri()";
131
+ if (this.in_.length > 0) {
132
+ const eitherTypeParameters = `<Error, ${this.name}>`;
133
+ valueToExpression = `${valueToExpression}.chain(iri => { switch (iri.value) { ${this.in_.map((iri) => `case "${iri.value}": return purify.Either.of${eitherTypeParameters}(iri as rdfjs.NamedNode<"${iri.value}">);`).join(" ")} default: return purify.Left${eitherTypeParameters}(new rdfjsResource.Resource.MistypedValueError({ actualValue: iri, expectedValueType: ${JSON.stringify(this.name)}, focusResource: ${variables.resource}, predicate: ${variables.predicate} })); } } )`;
134
+ }
135
+ }
136
+ else {
137
+ throw new Error("not implemented");
138
+ }
139
+ return {
140
+ ...super.fromRdfExpressionChain,
141
+ valueTo: `chain(values => values.chainMap(value => ${valueToExpression}))`,
142
+ };
143
+ }
124
144
  graphqlResolveExpression({ variables: { value }, }) {
125
145
  return `rdfjsResource.Resource.Identifier.toString(${value})`;
126
146
  }
@@ -149,20 +169,6 @@ export class IdentifierType extends TermType {
149
169
  return valueToNamedNode;
150
170
  }
151
171
  }
152
- propertyFromRdfResourceValueExpression({ variables, }) {
153
- if (this.nodeKinds.size === 2) {
154
- return `${variables.resourceValue}.toIdentifier()`;
155
- }
156
- if (this.isNamedNodeKind) {
157
- let expression = `${variables.resourceValue}.toIri()`;
158
- if (this.in_.length > 0) {
159
- const eitherTypeParameters = `<Error, ${this.name}>`;
160
- expression = `${expression}.chain(iri => { switch (iri.value) { ${this.in_.map((iri) => `case "${iri.value}": return purify.Either.of${eitherTypeParameters}(iri as rdfjs.NamedNode<"${iri.value}">);`).join(" ")} default: return purify.Left${eitherTypeParameters}(new rdfjsResource.Resource.MistypedValueError({ actualValue: iri, expectedValueType: ${JSON.stringify(this.name)}, focusResource: ${variables.resource}, predicate: ${variables.predicate} })); } } )`;
161
- }
162
- return expression;
163
- }
164
- throw new Error(`not implemented: ${this.name}`);
165
- }
166
172
  }
167
173
  __decorate([
168
174
  Memoize()
@@ -63,11 +63,20 @@ export class ListType extends Type {
63
63
  return `${variables.value}.map(item => (${this.itemType.fromJsonExpression({ variables: { value: "item" } })}))`;
64
64
  }
65
65
  fromRdfExpression({ variables, }) {
66
- const chain = [variables.resourceValues];
67
- chain.push("head()");
68
- chain.push("chain(value => value.toList())");
69
- chain.push(`chain(values => purify.Either.sequence(values.map(value => ${this.itemType.fromRdfExpression({ variables: { ...variables, resourceValues: "value.toValues()" } })})))`);
70
- return chain.join(".");
66
+ return [
67
+ variables.resourceValues,
68
+ "chain(values => values.chainMap(value => value.toList()))", // Resource.Values<Resource.Value> to Resource.Values<Resource.Value[]>
69
+ `chain(valueLists =>
70
+ valueLists.chainMap(
71
+ valueList => ${this.itemType.fromRdfExpression({
72
+ variables: {
73
+ ...variables,
74
+ resourceValues: `purify.Either.of<Error, rdfjsResource.Resource.Values<rdfjsResource.Resource.Value>>(rdfjsResource.Resource.Values.fromArray({ objects: valueList, predicate: ${variables.predicate}, subject: ${variables.resource} }))`,
75
+ },
76
+ })}
77
+ ))`, // Resource.Values<Resource.Value[]> to Resource.Values<item type arrays>
78
+ `map(valueLists => valueLists.map(valueList => valueList.toArray()${this.mutable ? ".concat()" : ""}))`, // Convert inner Resource.Values to arrays
79
+ ].join(".");
71
80
  }
72
81
  graphqlResolveExpression({ variables, }) {
73
82
  return variables.value;
@@ -180,6 +189,7 @@ export class ListType extends Type {
180
189
  allowIgnoreRdfType: true,
181
190
  context: "subject",
182
191
  variables: {
192
+ languageIn: parameters.variables.languageIn,
183
193
  subject: item0Variable,
184
194
  variablePrefix: variablePrefix("Item0"),
185
195
  },
@@ -213,6 +223,7 @@ export class ListType extends Type {
213
223
  allowIgnoreRdfType: true,
214
224
  context: "subject",
215
225
  variables: {
226
+ languageIn: parameters.variables.languageIn,
216
227
  subject: itemNVariable,
217
228
  variablePrefix: variablePrefix("ItemN"),
218
229
  },
@@ -8,10 +8,17 @@ export declare class LiteralType extends TermType<Literal, Literal> {
8
8
  } & Omit<ConstructorParameters<typeof TermType<Literal, Literal>>[0], "nodeKinds">);
9
9
  get jsonName(): Type.JsonName;
10
10
  fromJsonExpression({ variables, }: Parameters<TermType<Literal, Literal>["fromJsonExpression"]>[0]): string;
11
+ protected fromRdfExpressionChain({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): {
12
+ defaultValue?: string;
13
+ hasValues?: string;
14
+ languageIn?: string;
15
+ valueTo?: string;
16
+ };
11
17
  hashStatements({ depth, variables, }: Parameters<TermType<Literal, Literal>["hashStatements"]>[0]): readonly string[];
12
18
  jsonZodSchema({ variables, }: Parameters<TermType<Literal, Literal>["jsonZodSchema"]>[0]): ReturnType<TermType<Literal, Literal>["jsonZodSchema"]>;
13
- propertyFromRdfResourceValueExpression({ variables, }: Parameters<TermType<Literal, Literal>["propertyFromRdfResourceValueExpression"]>[0]): string;
19
+ sparqlWherePatterns(parameters: Parameters<Type["sparqlWherePatterns"]>[0] & {
20
+ ignoreLanguageIn?: boolean;
21
+ }): readonly string[];
14
22
  toJsonExpression({ variables, }: Parameters<TermType<Literal, Literal>["toJsonExpression"]>[0]): string;
15
- protected propertyFilterRdfResourceValuesExpression({ variables, }: Parameters<TermType<Literal, Literal>["propertyFilterRdfResourceValuesExpression"]>[0]): string;
16
23
  }
17
24
  //# sourceMappingURL=LiteralType.d.ts.map
@@ -5,6 +5,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { xsd } from "@tpluscode/rdf-ns-builders";
8
+ import { invariant } from "ts-invariant";
8
9
  import { Memoize } from "typescript-memoize";
9
10
  import { TermType } from "./TermType.js";
10
11
  import { Type } from "./Type.js";
@@ -23,6 +24,49 @@ export class LiteralType extends TermType {
23
24
  fromJsonExpression({ variables, }) {
24
25
  return `dataFactory.literal(${variables.value}["@value"], typeof ${variables.value}["@language"] !== "undefined" ? ${variables.value}["@language"] : (typeof ${variables.value}["@type"] !== "undefined" ? dataFactory.namedNode(${variables.value}["@type"]) : undefined))`;
25
26
  }
27
+ fromRdfExpressionChain({ variables, }) {
28
+ return {
29
+ ...super.fromRdfExpressionChain({ variables }),
30
+ languageIn: `chain(values => {
31
+ const literalValuesEither = values.chainMap(value => value.toLiteral());
32
+ if (literalValuesEither.isLeft()) {
33
+ return literalValuesEither;
34
+ }
35
+ const literalValues = literalValuesEither.unsafeCoerce();
36
+
37
+ const nonUniqueLanguageIn = ${variables.languageIn} ?? ${JSON.stringify(this.languageIn)};
38
+ if (nonUniqueLanguageIn.length === 0) {
39
+ return purify.Either.of<Error, rdfjsResource.Resource.Values<rdfjs.Literal>>(literalValues);
40
+ }
41
+
42
+ let uniqueLanguageIn: string[];
43
+ if (nonUniqueLanguageIn.length === 1) {
44
+ uniqueLanguageIn = [nonUniqueLanguageIn[0]];
45
+ } else {
46
+ uniqueLanguageIn = [];
47
+ for (const languageIn of nonUniqueLanguageIn) {
48
+ if (uniqueLanguageIn.indexOf(languageIn) === -1) {
49
+ uniqueLanguageIn.push(languageIn);
50
+ }
51
+ }
52
+ }
53
+
54
+ // Return all literals for the first languageIn, then all literals for the second languageIn, etc.
55
+ // Within a languageIn the literals may be in any order.
56
+ let filteredLiteralValues: rdfjsResource.Resource.Values<rdfjs.Literal> | undefined;
57
+ for (const languageIn of uniqueLanguageIn) {
58
+ if (!filteredLiteralValues) {
59
+ filteredLiteralValues = literalValues.filter(value => value.language === languageIn);
60
+ } else {
61
+ filteredLiteralValues = filteredLiteralValues.concat(...literalValues.filter(value => value.language === languageIn).toArray());
62
+ }
63
+ }
64
+
65
+ return purify.Either.of<Error, rdfjsResource.Resource.Values<rdfjs.Literal>>(filteredLiteralValues!);
66
+ })`,
67
+ valueTo: undefined,
68
+ };
69
+ }
26
70
  hashStatements({ depth, variables, }) {
27
71
  return [
28
72
  `${variables.hasher}.update(${variables.value}.datatype.value);`,
@@ -32,25 +76,45 @@ export class LiteralType extends TermType {
32
76
  jsonZodSchema({ variables, }) {
33
77
  return `${variables.zod}.object({ "@language": ${variables.zod}.string().optional(), "@type": ${variables.zod}.string().optional(), "@value": ${variables.zod}.string() })`;
34
78
  }
35
- propertyFromRdfResourceValueExpression({ variables, }) {
36
- return `${variables.resourceValue}.toLiteral()`;
79
+ sparqlWherePatterns(parameters) {
80
+ const { context, ignoreLanguageIn, variables } = parameters;
81
+ const superPatterns = super.sparqlWherePatterns(parameters);
82
+ if (ignoreLanguageIn || context === "subject") {
83
+ return superPatterns;
84
+ }
85
+ invariant(this.name.indexOf("rdfjs.Literal") !== -1, this.name);
86
+ return superPatterns.concat(`...[(${variables.languageIn} && ${variables.languageIn}.length > 0) ? ${variables.languageIn} : ${JSON.stringify(this.languageIn)}]
87
+ .filter(languagesIn => languagesIn.length > 0)
88
+ .map(languagesIn =>
89
+ languagesIn.map(languageIn =>
90
+ ({
91
+ type: "operation" as const,
92
+ operator: "=",
93
+ args: [
94
+ { type: "operation" as const, operator: "lang", args: [${variables.object}] },
95
+ dataFactory.literal(languageIn)
96
+ ]
97
+ })
98
+ )
99
+ )
100
+ .map(langEqualsExpressions =>
101
+ ({
102
+ type: "filter" as const,
103
+ expression:
104
+ langEqualsExpressions.length === 1
105
+ ? langEqualsExpressions[0]
106
+ :
107
+ {
108
+ type: "operation" as const,
109
+ operator: "||",
110
+ args: langEqualsExpressions
111
+ },
112
+ })
113
+ )`);
37
114
  }
38
115
  toJsonExpression({ variables, }) {
39
116
  return `{ "@language": ${variables.value}.language.length > 0 ? ${variables.value}.language : undefined, "@type": ${variables.value}.datatype.value !== "${xsd.string.value}" ? ${variables.value}.datatype.value : undefined, "@value": ${variables.value}.value }`;
40
117
  }
41
- propertyFilterRdfResourceValuesExpression({ variables, }) {
42
- return `${variables.resourceValues}.filter(_value => {
43
- const _languageInOrDefault = ${variables.languageIn} ?? ${JSON.stringify(this.languageIn)};
44
- if (_languageInOrDefault.length === 0) {
45
- return true;
46
- }
47
- const _valueLiteral = _value.toLiteral().toMaybe().extract();
48
- if (typeof _valueLiteral === "undefined") {
49
- return false;
50
- }
51
- return _languageInOrDefault.some(_languageIn => _languageIn === _valueLiteral.language);
52
- })`;
53
- }
54
118
  }
55
119
  __decorate([
56
120
  Memoize()
@@ -6,7 +6,7 @@ export declare abstract class NumberType extends PrimitiveType<number> {
6
6
  get conversions(): readonly Type.Conversion[];
7
7
  get name(): string;
8
8
  jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
9
- propertyFromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<number>["propertyFromRdfResourceValueExpression"]>[0]): string;
9
+ protected fromRdfResourceValueExpression({ variables, }: Parameters<PrimitiveType<number>["fromRdfResourceValueExpression"]>[0]): string;
10
10
  toRdfExpression({ variables, }: Parameters<PrimitiveType<string>["toRdfExpression"]>[0]): string;
11
11
  }
12
12
  //# sourceMappingURL=NumberType.d.ts.map
@@ -43,7 +43,7 @@ export class NumberType extends PrimitiveType {
43
43
  return `${variables.zod}.union([${this.primitiveIn.map((value) => `${variables.zod}.literal(${value})`).join(", ")}])`;
44
44
  }
45
45
  }
46
- propertyFromRdfResourceValueExpression({ variables, }) {
46
+ fromRdfResourceValueExpression({ variables, }) {
47
47
  let expression = `${variables.resourceValue}.toNumber()`;
48
48
  if (this.primitiveIn.length > 0) {
49
49
  const eitherTypeParameters = `<Error, ${this.name}>`;
@@ -10,7 +10,6 @@ import { Type } from "./Type.js";
10
10
  export declare class ObjectType extends DeclaredType {
11
11
  private readonly imports;
12
12
  protected readonly comment: Maybe<string>;
13
- protected readonly identifierMintingStrategy: Maybe<IdentifierMintingStrategy>;
14
13
  protected readonly label: Maybe<string>;
15
14
  protected readonly toRdfTypes: readonly NamedNode[];
16
15
  readonly abstract: boolean;
@@ -21,7 +20,7 @@ export declare class ObjectType extends DeclaredType {
21
20
  readonly staticModuleName: string;
22
21
  readonly synthetic: boolean;
23
22
  readonly typeof = "object";
24
- constructor({ abstract, comment, declarationType, extern, fromRdfType, imports, identifierMintingStrategy, label, lazyAncestorObjectTypes, lazyChildObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, staticModuleName, synthetic, toRdfTypes, ...superParameters }: {
23
+ constructor({ abstract, comment, declarationType, extern, fromRdfType, imports, label, lazyAncestorObjectTypes, lazyChildObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, staticModuleName, synthetic, toRdfTypes, ...superParameters }: {
25
24
  abstract: boolean;
26
25
  comment: Maybe<string>;
27
26
  declarationType: TsObjectDeclarationType;
@@ -19,7 +19,6 @@ import { syntheticNamePrefix } from "./syntheticNamePrefix.js";
19
19
  export class ObjectType extends DeclaredType {
20
20
  imports;
21
21
  comment;
22
- identifierMintingStrategy;
23
22
  label;
24
23
  toRdfTypes;
25
24
  abstract;
@@ -30,14 +29,13 @@ export class ObjectType extends DeclaredType {
30
29
  staticModuleName;
31
30
  synthetic;
32
31
  typeof = "object";
33
- constructor({ abstract, comment, declarationType, extern, fromRdfType, imports, identifierMintingStrategy, label, lazyAncestorObjectTypes, lazyChildObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, staticModuleName, synthetic, toRdfTypes, ...superParameters }) {
32
+ constructor({ abstract, comment, declarationType, extern, fromRdfType, imports, label, lazyAncestorObjectTypes, lazyChildObjectTypes, lazyDescendantObjectTypes, lazyParentObjectTypes, lazyProperties, staticModuleName, synthetic, toRdfTypes, ...superParameters }) {
34
33
  super(superParameters);
35
34
  this.abstract = abstract;
36
35
  this.comment = comment;
37
36
  this.declarationType = declarationType;
38
37
  this.extern = extern;
39
38
  this.fromRdfType = fromRdfType;
40
- this.identifierMintingStrategy = identifierMintingStrategy;
41
39
  this.imports = imports;
42
40
  this.label = label;
43
41
  // Lazily initialize some members in getters to avoid recursive construction
@@ -171,7 +169,7 @@ export class ObjectType extends DeclaredType {
171
169
  case "class":
172
170
  return `new ${this.name}(${parameters})`;
173
171
  case "interface":
174
- return `${this.staticModuleName}.create(${parameters})`;
172
+ return `${this.staticModuleName}.${syntheticNamePrefix}create(${parameters})`;
175
173
  }
176
174
  }
177
175
  get objectSetMethodNames() {
@@ -223,7 +221,7 @@ export class ObjectType extends DeclaredType {
223
221
  return `${this.staticModuleName}.${syntheticNamePrefix}fromJson(${variables.value}).unsafeCoerce()`;
224
222
  }
225
223
  fromRdfExpression({ variables, }) {
226
- return `${variables.resourceValues}.head().chain(value => value.toResource()).chain(_resource => ${this.staticModuleName}.${syntheticNamePrefix}fromRdf({ ...${variables.context}, ${variables.ignoreRdfType ? "ignoreRdfType: true, " : ""}languageIn: ${variables.languageIn}, objectSet: ${variables.objectSet}, resource: _resource }))`;
224
+ return `${variables.resourceValues}.chain(values => values.chainMap(value => value.toResource().chain(resource => ${this.staticModuleName}.${syntheticNamePrefix}fromRdf(resource, { ...${variables.context}, ${variables.ignoreRdfType ? "ignoreRdfType: true, " : ""}languageIn: ${variables.languageIn}, objectSet: ${variables.objectSet} }))))`;
227
225
  }
228
226
  graphqlResolveExpression({ variables, }) {
229
227
  return variables.value;
@@ -301,6 +299,7 @@ export class ObjectType extends DeclaredType {
301
299
  return [
302
300
  `...${this.staticModuleName}.${syntheticNamePrefix}sparqlWherePatterns(${objectInitializer({
303
301
  ignoreRdfType: parameters.allowIgnoreRdfType ? true : undefined, // Can ignore the rdf:type when the object is nested
302
+ languageIn: parameters.variables.languageIn,
304
303
  subject: parameters.variables.subject,
305
304
  variablePrefix: parameters.variables.variablePrefix,
306
305
  })})`,
@@ -25,6 +25,7 @@ declare class MemberType {
25
25
  get staticModuleName(): string;
26
26
  get toRdfjsResourceType(): string;
27
27
  jsonZodSchema(parameters: Parameters<DeclaredType["jsonZodSchema"]>[0]): string;
28
+ newExpression(parameters: Parameters<ObjectType["newExpression"]>[0]): string;
28
29
  snippetDeclarations(parameters: Parameters<DeclaredType["snippetDeclarations"]>[0]): readonly string[];
29
30
  useImports(): readonly Import[];
30
31
  }
@@ -81,6 +81,9 @@ class MemberType {
81
81
  jsonZodSchema(parameters) {
82
82
  return this.delegate.jsonZodSchema(parameters);
83
83
  }
84
+ newExpression(parameters) {
85
+ return this.delegate.newExpression(parameters);
86
+ }
84
87
  snippetDeclarations(parameters) {
85
88
  return this.delegate.snippetDeclarations(parameters);
86
89
  }
@@ -270,14 +273,19 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
270
273
  name: `${syntheticNamePrefix}fromRdf`,
271
274
  parameters: [
272
275
  {
273
- name: "{ ignoreRdfType, objectSet, resource, ...context }",
274
- type: `{ [_index: string]: any; ignoreRdfType?: boolean; objectSet?: ${syntheticNamePrefix}ObjectSet, resource: rdfjsResource.Resource; }`,
276
+ name: "resource",
277
+ type: "rdfjsResource.Resource",
278
+ },
279
+ {
280
+ hasQuestionToken: true,
281
+ name: "options",
282
+ type: `{ [_index: string]: any; ignoreRdfType?: boolean; languageIn?: readonly string[]; objectSet?: ${syntheticNamePrefix}ObjectSet }`,
275
283
  },
276
284
  ],
277
285
  returnType: `purify.Either<Error, ${this.name}>`,
278
286
  statements: [
279
287
  `return ${this.memberTypes.reduce((expression, memberType) => {
280
- const memberTypeExpression = `(${memberType.staticModuleName}.${syntheticNamePrefix}fromRdf({ ...context, objectSet, resource }) as purify.Either<Error, ${this.name}>)`;
288
+ const memberTypeExpression = `(${memberType.staticModuleName}.${syntheticNamePrefix}fromRdf(resource, { ...options, ignoreRdfType: false }) as purify.Either<Error, ${this.name}>)`;
281
289
  return expression.length > 0
282
290
  ? `${expression}.altLazy(() => ${memberTypeExpression})`
283
291
  : memberTypeExpression;
@@ -298,7 +306,7 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
298
306
  initializer: `new graphql.GraphQLUnionType(${objectInitializer({
299
307
  description: this.comment.map(JSON.stringify).extract(),
300
308
  name: `"${this.name}"`,
301
- resolveType: `function (value: ${this.name}) { return value.${syntheticNamePrefix}type; }`,
309
+ resolveType: `(value: ${this.name}) => value.${syntheticNamePrefix}type`,
302
310
  types: `[${this.memberTypes.map((memberType) => memberType.graphqlName.nullableName).join(", ")}]`,
303
311
  })})`,
304
312
  },
@@ -427,7 +435,7 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
427
435
  {
428
436
  hasQuestionToken: true,
429
437
  name: "parameters",
430
- type: '{ ignoreRdfType?: boolean; subject?: sparqljs.Triple["subject"], variablePrefix?: string }',
438
+ type: '{ ignoreRdfType?: boolean; languageIn?: readonly string[]; subject?: sparqljs.Triple["subject"], variablePrefix?: string }',
431
439
  },
432
440
  ],
433
441
  returnType: "readonly sparqljs.Pattern[]",
@@ -501,8 +509,9 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
501
509
  type: this.name,
502
510
  },
503
511
  {
512
+ hasQuestionToken: true,
504
513
  name: parametersVariable,
505
- type: "{ mutateGraph: rdfjsResource.MutableResource.MutateGraph, resourceSet: rdfjsResource.MutableResourceSet }",
514
+ type: "{ mutateGraph?: rdfjsResource.MutableResource.MutateGraph, resourceSet?: rdfjsResource.MutableResourceSet }",
506
515
  },
507
516
  ],
508
517
  returnType: (() => {
@@ -537,7 +546,7 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
537
546
  }
538
547
  fromRdfExpression({ variables, }) {
539
548
  // Don't ignoreRdfType, we may need it to distinguish the union members
540
- return `${variables.resourceValues}.head().chain(value => value.toResource()).chain(_resource => ${this.staticModuleName}.${syntheticNamePrefix}fromRdf({ ...${variables.context}, languageIn: ${variables.languageIn}, objectSet: ${variables.objectSet}, resource: _resource }))`;
549
+ return `${variables.resourceValues}.chain(values => values.chainMap(value => value.toResource().chain(resource => ${this.staticModuleName}.${syntheticNamePrefix}fromRdf(resource, { ...${variables.context}, ignoreRdfType: false, languageIn: ${variables.languageIn}, objectSet: ${variables.objectSet} }))))`;
541
550
  }
542
551
  graphqlResolveExpression({ variables, }) {
543
552
  return variables.value;
@@ -590,6 +599,7 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
590
599
  case "subject":
591
600
  return [
592
601
  `...${this.staticModuleName}.${syntheticNamePrefix}sparqlWherePatterns(${objectInitializer({
602
+ languageIn: parameters.variables.languageIn,
593
603
  subject: parameters.variables.subject,
594
604
  variablePrefix: parameters.variables.variablePrefix,
595
605
  })})`,
@@ -601,8 +611,7 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
601
611
  case "class":
602
612
  return `${variables.value}.${syntheticNamePrefix}toJson()`;
603
613
  case "interface":
604
- throw new Error("not implemented: need a freestanding toJson function like the toRdf function");
605
- // return `${this.name}.toJson(${variables.value})`;
614
+ return `${this.staticModuleName}.${syntheticNamePrefix}toJson(${variables.value})`;
606
615
  }
607
616
  }
608
617
  toRdfExpression({ variables, }) {
@@ -1,7 +1,7 @@
1
1
  import { Maybe } from "purify-ts";
2
2
  import { Import } from "./Import.js";
3
3
  import { Type } from "./Type.js";
4
- export declare class OptionType<ItemTypeT extends Type = Type> extends Type {
4
+ export declare class OptionType<ItemTypeT extends Type> extends Type {
5
5
  readonly discriminatorProperty: Maybe<Type.DiscriminatorProperty>;
6
6
  readonly itemType: ItemTypeT;
7
7
  readonly kind = "OptionType";
@@ -4,8 +4,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
- import { Maybe } from "purify-ts";
8
7
  import { Memoize } from "typescript-memoize";
8
+ import { Maybe } from "purify-ts";
9
9
  import { invariant } from "ts-invariant";
10
10
  import { Import } from "./Import.js";
11
11
  import { SnippetDeclarations } from "./SnippetDeclarations.js";
@@ -73,7 +73,8 @@ export class OptionType extends Type {
73
73
  : `${expression}.map(item => (${itemFromJsonExpression}))`;
74
74
  }
75
75
  fromRdfExpression(parameters) {
76
- return `${this.itemType.fromRdfExpression(parameters)}.map(value => purify.Maybe.of(value)).chainLeft(error => error instanceof rdfjsResource.Resource.MissingValueError ? purify.Right(purify.Maybe.empty()) : purify.Left(error))`;
76
+ const { variables } = parameters;
77
+ return `${this.itemType.fromRdfExpression(parameters)}.map(values => values.length > 0 ? values.map(value => purify.Maybe.of(value)) : rdfjsResource.Resource.Values.fromValue<purify.Maybe<${this.itemType.name}>>({ object: purify.Maybe.empty(), predicate: ${variables.predicate}, subject: ${variables.resource} }))`;
77
78
  }
78
79
  graphqlResolveExpression(parameters) {
79
80
  return `${this.itemType.graphqlResolveExpression(parameters)}.extractNullable()`;
@@ -117,14 +118,15 @@ export class OptionType extends Type {
117
118
  sparqlWherePatterns(parameters) {
118
119
  switch (parameters.context) {
119
120
  case "object": {
120
- const patterns = super.sparqlWherePatterns(parameters);
121
+ const patterns = this.itemType.sparqlWherePatterns(parameters);
121
122
  if (patterns.length === 0) {
122
123
  return [];
123
124
  }
124
125
  return [`{ patterns: [${patterns.join(", ")}], type: "optional" }`];
125
126
  }
126
127
  case "subject": {
127
- return this.itemType.sparqlWherePatterns(parameters);
128
+ throw new Error("should never be called");
129
+ // return this.itemType.sparqlWherePatterns(parameters);
128
130
  }
129
131
  }
130
132
  }
@@ -12,10 +12,28 @@ export declare abstract class PrimitiveType<ValueT extends boolean | Date | stri
12
12
  get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
13
13
  get jsonName(): Type.JsonName;
14
14
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
15
+ protected fromRdfExpressionChain({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): {
16
+ defaultValue?: string;
17
+ hasValues?: string;
18
+ languageIn?: string;
19
+ valueTo?: string;
20
+ };
15
21
  graphqlResolveExpression({ variables, }: Parameters<Type["graphqlResolveExpression"]>[0]): string;
16
22
  hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
17
23
  snippetDeclarations({ features, }: Parameters<Type["snippetDeclarations"]>[0]): readonly string[];
24
+ sparqlWherePatterns(parameters: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
18
25
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
19
- protected propertyFilterRdfResourceValuesExpression({ variables, }: Parameters<LiteralType["propertyFilterRdfResourceValuesExpression"]>[0]): string;
26
+ /**
27
+ * Convert an rdfjsResource.Resource.Value to a value of this type.
28
+ * @param variables
29
+ * @protected
30
+ */
31
+ protected abstract fromRdfResourceValueExpression({ variables, }: {
32
+ variables: {
33
+ predicate: string;
34
+ resource: string;
35
+ resourceValue: string;
36
+ };
37
+ }): string;
20
38
  }
21
39
  //# sourceMappingURL=PrimitiveType.d.ts.map