@shaclmate/compiler 4.0.30 → 4.0.32

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 (40) hide show
  1. package/dist/generators/ts/AbstractTermType.js +2 -1
  2. package/dist/generators/ts/AbstractType.d.ts +1 -0
  3. package/dist/generators/ts/AbstractUnionType.d.ts +3 -0
  4. package/dist/generators/ts/AbstractUnionType.js +20 -9
  5. package/dist/generators/ts/ListType.js +1 -1
  6. package/dist/generators/ts/NamedObjectType.js +2 -3
  7. package/dist/generators/ts/NamedObjectUnionType.js +2 -2
  8. package/dist/generators/ts/Snippets.d.ts +5 -1
  9. package/dist/generators/ts/Snippets.js +35 -7
  10. package/dist/generators/ts/_NamedObjectType/AbstractProperty.d.ts +3 -3
  11. package/dist/generators/ts/_NamedObjectType/DiscriminantProperty.d.ts +2 -2
  12. package/dist/generators/ts/_NamedObjectType/DiscriminantProperty.js +15 -21
  13. package/dist/generators/ts/_NamedObjectType/IdentifierProperty.d.ts +1 -1
  14. package/dist/generators/ts/_NamedObjectType/IdentifierProperty.js +4 -4
  15. package/dist/generators/ts/_NamedObjectType/NamedObjectType_fromJsonFunctionDeclaration.js +22 -3
  16. package/dist/generators/ts/_NamedObjectType/NamedObjectType_fromRdfResourceFunctionDeclaration.js +100 -7
  17. package/dist/generators/ts/_NamedObjectType/NamedObjectType_toRdfResourceFunctionDeclaration.js +14 -20
  18. package/dist/generators/ts/_NamedObjectType/NamedObjectType_toStringFunctionDeclarations.js +3 -3
  19. package/dist/generators/ts/_NamedObjectType/ShaclProperty.d.ts +1 -1
  20. package/dist/generators/ts/_NamedObjectType/ShaclProperty.js +4 -4
  21. package/dist/generators/ts/_snippets/snippets_ToRdfResourceFunction.js +8 -2
  22. package/dist/generators/ts/_snippets/snippets__FromRdfResourceFunction.d.ts +3 -0
  23. package/dist/generators/ts/_snippets/snippets__FromRdfResourceFunction.js +13 -0
  24. package/dist/generators/ts/_snippets/snippets__ToRdfResourceFunction.d.ts +3 -0
  25. package/dist/generators/ts/_snippets/snippets__ToRdfResourceFunction.js +12 -0
  26. package/dist/generators/ts/_snippets/snippets_sequenceRecord.d.ts +3 -0
  27. package/dist/generators/ts/_snippets/snippets_sequenceRecord.js +18 -0
  28. package/dist/generators/ts/_snippets/snippets_wrap_FromRdfResourceFunction.d.ts +3 -0
  29. package/dist/generators/ts/_snippets/snippets_wrap_FromRdfResourceFunction.js +10 -0
  30. package/dist/generators/ts/_snippets/snippets_wrap_ToRdfResourceFunction.d.ts +3 -0
  31. package/dist/generators/ts/_snippets/snippets_wrap_ToRdfResourceFunction.js +14 -0
  32. package/dist/input/generated.d.ts +34 -116
  33. package/dist/input/generated.js +1698 -2069
  34. package/package.json +2 -2
  35. package/dist/generators/ts/_NamedObjectType/NamedObjectType_propertiesFromJsonFunctionDeclaration.d.ts +0 -5
  36. package/dist/generators/ts/_NamedObjectType/NamedObjectType_propertiesFromJsonFunctionDeclaration.js +0 -38
  37. package/dist/generators/ts/_NamedObjectType/NamedObjectType_propertiesFromRdfResourceFunctionDeclaration.d.ts +0 -5
  38. package/dist/generators/ts/_NamedObjectType/NamedObjectType_propertiesFromRdfResourceFunctionDeclaration.js +0 -98
  39. package/dist/generators/ts/_snippets/snippets_PropertiesFromRdfResourceFunction.d.ts +0 -3
  40. package/dist/generators/ts/_snippets/snippets_PropertiesFromRdfResourceFunction.js +0 -13
@@ -77,9 +77,10 @@ export class AbstractTermType extends AbstractType {
77
77
  }
78
78
  get discriminantProperty() {
79
79
  return Maybe.of({
80
+ descendantValues: [],
81
+ jsonName: "termType",
80
82
  name: "termType",
81
83
  ownValues: [...this.nodeKinds].map(NodeKind.toTermType),
82
- descendantValues: [],
83
84
  type: "string",
84
85
  });
85
86
  }
@@ -260,6 +260,7 @@ export declare namespace AbstractType {
260
260
  }
261
261
  interface DiscriminantProperty {
262
262
  readonly descendantValues: readonly DiscriminantProperty.Value[];
263
+ readonly jsonName: string;
263
264
  readonly name: string;
264
265
  readonly ownValues: readonly DiscriminantProperty.Value[];
265
266
  }
@@ -48,11 +48,13 @@ export declare abstract class AbstractUnionType<MemberTypeT extends Type> extend
48
48
  }
49
49
  type Discriminant = ExtrinsicDiscriminant | HybridDiscriminant | IntrinsicDiscriminant | TypeofDiscriminant;
50
50
  type ExtrinsicDiscriminant = {
51
+ readonly jsonName: string;
51
52
  readonly kind: "extrinsic";
52
53
  readonly memberValues: readonly AbstractType.DiscriminantProperty.Value[];
53
54
  readonly name: string;
54
55
  };
55
56
  type HybridDiscriminant = {
57
+ readonly jsonName: string;
56
58
  readonly kind: "hybrid";
57
59
  readonly memberValues: readonly {
58
60
  readonly kind: "extrinsic" | "intrinsic";
@@ -61,6 +63,7 @@ type HybridDiscriminant = {
61
63
  readonly name: string;
62
64
  };
63
65
  type IntrinsicDiscriminant = {
66
+ readonly jsonName: string;
64
67
  readonly kind: "intrinsic";
65
68
  readonly memberValues: readonly {
66
69
  readonly descendantValues: readonly AbstractType.DiscriminantProperty.Value[];
@@ -77,7 +77,10 @@ export class AbstractUnionType extends AbstractType {
77
77
  return code `${member.type.name}.is${member.type.name}(${instance})`;
78
78
  }
79
79
  }
80
- return code `(${joinCode(discriminantValues.map((discriminantValue) => code `${instance}.${discriminant.name} === ${literalOf(discriminantValue)}`), { on: " || " })})`;
80
+ const discriminantName = json
81
+ ? discriminant.jsonName
82
+ : discriminant.name;
83
+ return code `(${joinCode(discriminantValues.map((discriminantValue) => code `${instance}["${discriminantName}"] === ${literalOf(discriminantValue)}`), { on: " || " })})`;
81
84
  };
82
85
  return {
83
86
  discriminantValues,
@@ -156,18 +159,21 @@ export class AbstractUnionType extends AbstractType {
156
159
  case "extrinsic":
157
160
  return Maybe.of({
158
161
  descendantValues: [],
162
+ jsonName: this.discriminant.jsonName,
159
163
  ownValues: this.discriminant.memberValues,
160
164
  name: this.discriminant.name,
161
165
  });
162
166
  case "hybrid":
163
167
  return Maybe.of({
164
168
  descendantValues: [],
169
+ jsonName: this.discriminant.jsonName,
165
170
  ownValues: this.discriminant.memberValues.flatMap((_) => _.ownValues),
166
171
  name: "termType",
167
172
  });
168
173
  case "intrinsic":
169
174
  return Maybe.of({
170
175
  descendantValues: this.discriminant.memberValues.flatMap((_) => _.descendantValues),
176
+ jsonName: this.discriminant.jsonName,
171
177
  name: this.discriminant.name,
172
178
  ownValues: this.discriminant.memberValues.flatMap((_) => _.ownValues),
173
179
  });
@@ -537,6 +543,7 @@ export var Discriminant;
537
543
  // extrinsic with user-specified values
538
544
  if (members.some((member) => member.discriminantValue.isJust())) {
539
545
  return {
546
+ jsonName: "type",
540
547
  kind: "extrinsic",
541
548
  memberValues: members.map((member, memberI) => member.discriminantValue.orDefault(memberI)),
542
549
  name: "type",
@@ -545,19 +552,20 @@ export var Discriminant;
545
552
  const memberTypes = members.map((member) => member.type);
546
553
  // intrinsic
547
554
  {
548
- let inlineDiscriminantPropertyName;
555
+ let inlineDiscriminantProperty;
549
556
  const memberValues = [];
550
557
  for (const memberType of memberTypes) {
551
558
  const memberTypeDiscriminantProperty = memberType.discriminantProperty.extract();
552
559
  if (!memberTypeDiscriminantProperty) {
553
- inlineDiscriminantPropertyName = undefined;
560
+ inlineDiscriminantProperty = undefined;
554
561
  break;
555
562
  }
556
- if (!inlineDiscriminantPropertyName) {
557
- inlineDiscriminantPropertyName = memberTypeDiscriminantProperty.name;
563
+ if (!inlineDiscriminantProperty) {
564
+ inlineDiscriminantProperty = memberTypeDiscriminantProperty;
558
565
  }
559
- else if (memberTypeDiscriminantProperty.name !== inlineDiscriminantPropertyName) {
560
- inlineDiscriminantPropertyName = undefined;
566
+ else if (memberTypeDiscriminantProperty.name !==
567
+ inlineDiscriminantProperty.name) {
568
+ inlineDiscriminantProperty = undefined;
561
569
  break;
562
570
  }
563
571
  memberValues.push({
@@ -565,11 +573,12 @@ export var Discriminant;
565
573
  ownValues: memberTypeDiscriminantProperty.ownValues,
566
574
  });
567
575
  }
568
- if (inlineDiscriminantPropertyName) {
576
+ if (inlineDiscriminantProperty) {
569
577
  return {
578
+ jsonName: inlineDiscriminantProperty.jsonName,
570
579
  kind: "intrinsic",
571
580
  memberValues,
572
- name: inlineDiscriminantPropertyName,
581
+ name: inlineDiscriminantProperty.name,
573
582
  };
574
583
  }
575
584
  }
@@ -606,6 +615,7 @@ export var Discriminant;
606
615
  }
607
616
  }
608
617
  return {
618
+ jsonName: "termType",
609
619
  kind: "hybrid",
610
620
  memberValues: memberTypes.map((memberType, memberTypeI) => {
611
621
  const memberTermTypes = termTypes(memberType);
@@ -648,6 +658,7 @@ export var Discriminant;
648
658
  }
649
659
  invariant(memberValues.length === memberTypes.length);
650
660
  return {
661
+ jsonName: "type",
651
662
  kind: "extrinsic",
652
663
  name: "type",
653
664
  memberValues: memberValues,
@@ -79,7 +79,7 @@ export class ListType extends AbstractCollectionType {
79
79
  },
80
80
  {
81
81
  currentSubListResource: null,
82
- listResource: resourceSet.resource(${mintListIdentifierFunction}()),
82
+ listResource: ${variables.resourceSet}.resource(${mintListIdentifierFunction}()),
83
83
  } as {
84
84
  currentSubListResource: ${resourceTypeName} | null;
85
85
  listResource: ${resourceTypeName};
@@ -30,8 +30,6 @@ import { NamedObjectType_jsonSchemaFunctionDeclaration } from "./_NamedObjectTyp
30
30
  import { NamedObjectType_jsonTypeAliasDeclaration } from "./_NamedObjectType/NamedObjectType_jsonTypeAliasDeclaration.js";
31
31
  import { NamedObjectType_jsonUiSchemaFunctionDeclaration } from "./_NamedObjectType/NamedObjectType_jsonUiSchemaFunctionDeclaration.js";
32
32
  import { NamedObjectType_objectSetMethodNames } from "./_NamedObjectType/NamedObjectType_objectSetMethodNames.js";
33
- import { NamedObjectType_propertiesFromJsonFunctionDeclaration } from "./_NamedObjectType/NamedObjectType_propertiesFromJsonFunctionDeclaration.js";
34
- import { NamedObjectType_propertiesFromRdfResourceFunctionDeclaration } from "./_NamedObjectType/NamedObjectType_propertiesFromRdfResourceFunctionDeclaration.js";
35
33
  import { NamedObjectType_schemaVariableStatement } from "./_NamedObjectType/NamedObjectType_schemaVariableStatement.js";
36
34
  import { NamedObjectType_sparqlConstructQueryFunctionDeclaration } from "./_NamedObjectType/NamedObjectType_sparqlConstructQueryFunctionDeclaration.js";
37
35
  import { NamedObjectType_sparqlConstructQueryStringFunctionDeclaration } from "./_NamedObjectType/NamedObjectType_sparqlConstructQueryStringFunctionDeclaration.js";
@@ -108,7 +106,7 @@ export class NamedObjectType extends AbstractType {
108
106
  ? [
109
107
  code `export namespace Json { ${joinCode(jsonModuleDeclarations, { on: "\n\n" })} }`,
110
108
  ]
111
- : []), NamedObjectType_filterFunctionDeclaration.call(this), NamedObjectType_filterTypeDeclaration.call(this), ...NamedObjectType_focusSparqlConstructTriplesFunctionDeclaration.bind(this)().toList(), ...NamedObjectType_focusSparqlWherePatternsFunctionDeclaration.bind(this)().toList(), ...NamedObjectType_fromJsonFunctionDeclaration.call(this).toList(), ...NamedObjectType_fromRdfResourceFunctionDeclaration.call(this).toList(), ...NamedObjectType_fromRdfResourceValuesFunctionDeclaration.call(this).toList(), ...NamedObjectType_fromRdfTypeVariableStatement.call(this).toList(), NamedObjectType_isTypeFunctionDeclaration.call(this), ...NamedObjectType_propertiesFromJsonFunctionDeclaration.bind(this)().toList(), ...NamedObjectType_propertiesFromRdfResourceFunctionDeclaration.bind(this)().toList(), NamedObjectType_schemaVariableStatement.call(this), ...NamedObjectType_sparqlConstructQueryFunctionDeclaration.bind({
109
+ : []), NamedObjectType_filterFunctionDeclaration.call(this), NamedObjectType_filterTypeDeclaration.call(this), ...NamedObjectType_focusSparqlConstructTriplesFunctionDeclaration.bind(this)().toList(), ...NamedObjectType_focusSparqlWherePatternsFunctionDeclaration.bind(this)().toList(), ...NamedObjectType_fromJsonFunctionDeclaration.call(this).toList(), ...NamedObjectType_fromRdfResourceFunctionDeclaration.call(this).toList(), ...NamedObjectType_fromRdfResourceValuesFunctionDeclaration.call(this).toList(), ...NamedObjectType_fromRdfTypeVariableStatement.call(this).toList(), NamedObjectType_isTypeFunctionDeclaration.call(this), NamedObjectType_schemaVariableStatement.call(this), ...NamedObjectType_sparqlConstructQueryFunctionDeclaration.bind({
112
110
  features: this.features,
113
111
  filterType: this.filterType,
114
112
  name: this.name,
@@ -139,6 +137,7 @@ ${joinCode(staticModuleDeclarations, { on: "\n\n" })}
139
137
  }
140
138
  get discriminantProperty() {
141
139
  return Maybe.of({
140
+ jsonName: this._discriminantProperty.jsonName,
142
141
  name: this._discriminantProperty.name,
143
142
  ownValues: this._discriminantProperty.type.ownValues,
144
143
  descendantValues: this._discriminantProperty.type.descendantValues,
@@ -189,9 +189,9 @@ ${{
189
189
  return {};
190
190
  }
191
191
  return singleEntryRecord(`toRdfResource`, code `\
192
- export const toRdfResource: ${this.reusables.snippets.ToRdfResourceFunction}<${this.name}> = (value, options) => {
192
+ export const toRdfResource: ${this.reusables.snippets.ToRdfResourceFunction}<${this.name}> = (object, options) => {
193
193
  ${joinCode(this.members
194
- .map((member) => code `if (${member.type.name}.is${member.type.name}(value)) { return ${member.type.name}.toRdfResource(value, options); }`)
194
+ .map((member) => code `if (${member.type.name}.is${member.type.name}(object)) { return ${member.type.name}.toRdfResource(object, options); }`)
195
195
  .concat(code `throw new Error("unrecognized type");`))}
196
196
  };`);
197
197
  }
@@ -38,7 +38,6 @@ export declare class Snippets {
38
38
  get MaybeSchema(): Snippet;
39
39
  get NumericFilter(): Snippet;
40
40
  get NumericSchema(): Snippet;
41
- get PropertiesFromRdfResourceFunction(): Snippet;
42
41
  get PropertyPath(): Snippet;
43
42
  get RdfVocabularies(): Snippet;
44
43
  get ShaclPropertySchema(): Snippet;
@@ -54,6 +53,8 @@ export declare class Snippets {
54
53
  get UnwrapR(): Snippet;
55
54
  get ValueSparqlConstructTriplesFunction(): Snippet;
56
55
  get ValueSparqlWherePatternsFunction(): Snippet;
56
+ get _FromRdfResourceFunction(): Snippet;
57
+ get _ToRdfResourceFunction(): Snippet;
57
58
  get arrayEquals(): Snippet;
58
59
  get arrayIntersection(): Snippet;
59
60
  get bigDecimalLiteral(): Snippet;
@@ -103,6 +104,7 @@ export declare class Snippets {
103
104
  get parseBlankNode(): Snippet;
104
105
  get parseIdentifier(): Snippet;
105
106
  get parseIri(): Snippet;
107
+ get sequenceRecord(): Snippet;
106
108
  get setSparqlConstructTriples(): Snippet;
107
109
  get setSparqlWherePatterns(): Snippet;
108
110
  get shaclPropertyFromRdf(): Snippet;
@@ -117,6 +119,8 @@ export declare class Snippets {
117
119
  get termFilterSparqlPatterns(): Snippet;
118
120
  get termSchemaSparqlPatterns(): Snippet;
119
121
  get termSparqlWherePatterns(): Snippet;
122
+ get wrap_FromRdfResourceFunction(): Snippet;
123
+ get wrap_ToRdfResourceFunction(): Snippet;
120
124
  protected get snippets(): Snippets;
121
125
  private snippet;
122
126
  }
@@ -6,6 +6,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  };
7
7
  import { invariant } from "ts-invariant";
8
8
  import { Memoize } from "typescript-memoize";
9
+ import { snippets__FromRdfResourceFunction } from "./_snippets/snippets__FromRdfResourceFunction.js";
10
+ import { snippets__ToRdfResourceFunction } from "./_snippets/snippets__ToRdfResourceFunction.js";
9
11
  import { snippets_arrayEquals } from "./_snippets/snippets_arrayEquals.js";
10
12
  import { snippets_arrayIntersection } from "./_snippets/snippets_arrayIntersection.js";
11
13
  import { snippets_BlankNodeFilter } from "./_snippets/snippets_BlankNodeFilter.js";
@@ -80,7 +82,6 @@ import { snippets_NumericFilter } from "./_snippets/snippets_NumericFilter.js";
80
82
  import { snippets_NumericSchema } from "./_snippets/snippets_NumericSchema.js";
81
83
  import { snippets_normalizeSparqlWherePatterns } from "./_snippets/snippets_normalizeSparqlWherePatterns.js";
82
84
  import { snippets_numericSparqlWherePatterns } from "./_snippets/snippets_numericSparqlWherePatterns.js";
83
- import { snippets_PropertiesFromRdfResourceFunction } from "./_snippets/snippets_PropertiesFromRdfResourceFunction.js";
84
85
  import { snippets_PropertyPath } from "./_snippets/snippets_PropertyPath.js";
85
86
  import { snippets_parseBlankNode } from "./_snippets/snippets_parseBlankNode.js";
86
87
  import { snippets_parseIdentifier } from "./_snippets/snippets_parseIdentifier.js";
@@ -92,6 +93,7 @@ import { snippets_SparqlPattern } from "./_snippets/snippets_SparqlPattern.js";
92
93
  import { snippets_SparqlPattern_isSolutionGenerating } from "./_snippets/snippets_SparqlPattern_isSolutionGenerating.js";
93
94
  import { snippets_StringFilter } from "./_snippets/snippets_StringFilter.js";
94
95
  import { snippets_StringSchema } from "./_snippets/snippets_StringSchema.js";
96
+ import { snippets_sequenceRecord } from "./_snippets/snippets_sequenceRecord.js";
95
97
  import { snippets_setSparqlConstructTriples } from "./_snippets/snippets_setSparqlConstructTriples.js";
96
98
  import { snippets_setSparqlWherePatterns } from "./_snippets/snippets_setSparqlWherePatterns.js";
97
99
  import { snippets_shaclPropertyFromRdf } from "./_snippets/snippets_shaclPropertyFromRdf.js";
@@ -113,6 +115,8 @@ import { snippets_termSparqlWherePatterns } from "./_snippets/snippets_termSparq
113
115
  import { snippets_UnwrapR } from "./_snippets/snippets_UnwrapR.js";
114
116
  import { snippets_ValueSparqlConstructTriplesFunction } from "./_snippets/snippets_ValueSparqlConstructTriplesFunction.js";
115
117
  import { snippets_ValueSparqlWherePatternsFunction } from "./_snippets/snippets_ValueSparqlWherePatternsFunction.js";
118
+ import { snippets_wrap_FromRdfResourceFunction } from "./_snippets/snippets_wrap_FromRdfResourceFunction.js";
119
+ import { snippets_wrap_ToRdfResourceFunction } from "./_snippets/snippets_wrap_ToRdfResourceFunction.js";
116
120
  import { rdfjsTermExpression } from "./rdfjsTermExpression.js";
117
121
  import { syntheticNamePrefix } from "./syntheticNamePrefix.js";
118
122
  import { code } from "./ts-poet-wrapper.js";
@@ -210,9 +214,6 @@ export class Snippets {
210
214
  get NumericSchema() {
211
215
  return this.snippet(snippets_NumericSchema);
212
216
  }
213
- get PropertiesFromRdfResourceFunction() {
214
- return this.snippet(snippets_PropertiesFromRdfResourceFunction);
215
- }
216
217
  get PropertyPath() {
217
218
  return this.snippet(snippets_PropertyPath);
218
219
  }
@@ -258,6 +259,12 @@ export class Snippets {
258
259
  get ValueSparqlWherePatternsFunction() {
259
260
  return this.snippet(snippets_ValueSparqlWherePatternsFunction);
260
261
  }
262
+ get _FromRdfResourceFunction() {
263
+ return this.snippet(snippets__FromRdfResourceFunction);
264
+ }
265
+ get _ToRdfResourceFunction() {
266
+ return this.snippet(snippets__ToRdfResourceFunction);
267
+ }
261
268
  get arrayEquals() {
262
269
  return this.snippet(snippets_arrayEquals);
263
270
  }
@@ -421,6 +428,9 @@ export class Snippets {
421
428
  get parseIri() {
422
429
  return this.snippet(snippets_parseIri);
423
430
  }
431
+ get sequenceRecord() {
432
+ return this.snippet(snippets_sequenceRecord);
433
+ }
424
434
  get setSparqlConstructTriples() {
425
435
  return this.snippet(snippets_setSparqlConstructTriples);
426
436
  }
@@ -463,6 +473,12 @@ export class Snippets {
463
473
  get termSparqlWherePatterns() {
464
474
  return this.snippet(snippets_termSparqlWherePatterns);
465
475
  }
476
+ get wrap_FromRdfResourceFunction() {
477
+ return this.snippet(snippets_wrap_FromRdfResourceFunction);
478
+ }
479
+ get wrap_ToRdfResourceFunction() {
480
+ return this.snippet(snippets_wrap_ToRdfResourceFunction);
481
+ }
466
482
  get snippets() {
467
483
  return this;
468
484
  }
@@ -567,9 +583,6 @@ __decorate([
567
583
  __decorate([
568
584
  Memoize()
569
585
  ], Snippets.prototype, "NumericSchema", null);
570
- __decorate([
571
- Memoize()
572
- ], Snippets.prototype, "PropertiesFromRdfResourceFunction", null);
573
586
  __decorate([
574
587
  Memoize()
575
588
  ], Snippets.prototype, "PropertyPath", null);
@@ -615,6 +628,12 @@ __decorate([
615
628
  __decorate([
616
629
  Memoize()
617
630
  ], Snippets.prototype, "ValueSparqlWherePatternsFunction", null);
631
+ __decorate([
632
+ Memoize()
633
+ ], Snippets.prototype, "_FromRdfResourceFunction", null);
634
+ __decorate([
635
+ Memoize()
636
+ ], Snippets.prototype, "_ToRdfResourceFunction", null);
618
637
  __decorate([
619
638
  Memoize()
620
639
  ], Snippets.prototype, "arrayEquals", null);
@@ -759,6 +778,9 @@ __decorate([
759
778
  __decorate([
760
779
  Memoize()
761
780
  ], Snippets.prototype, "parseIri", null);
781
+ __decorate([
782
+ Memoize()
783
+ ], Snippets.prototype, "sequenceRecord", null);
762
784
  __decorate([
763
785
  Memoize()
764
786
  ], Snippets.prototype, "setSparqlConstructTriples", null);
@@ -801,4 +823,10 @@ __decorate([
801
823
  __decorate([
802
824
  Memoize()
803
825
  ], Snippets.prototype, "termSparqlWherePatterns", null);
826
+ __decorate([
827
+ Memoize()
828
+ ], Snippets.prototype, "wrap_FromRdfResourceFunction", null);
829
+ __decorate([
830
+ Memoize()
831
+ ], Snippets.prototype, "wrap_ToRdfResourceFunction", null);
804
832
  //# sourceMappingURL=Snippets.js.map
@@ -101,13 +101,13 @@ export declare abstract class AbstractProperty<TypeT extends Pick<Type, "filterF
101
101
  };
102
102
  }): readonly Code[];
103
103
  /**
104
- * Statements to deserialize JSON for this property (as described by toJsonObjectMember) to a typed value of the property.
104
+ * Expression to deserialize JSON for this property (as described by toJsonObjectMember) to a typed value of the property.
105
105
  */
106
- abstract fromJsonStatements(parameters: {
106
+ abstract fromJsonExpression(parameters: {
107
107
  variables: {
108
108
  jsonObject: Code;
109
109
  };
110
- }): readonly Code[];
110
+ }): Maybe<Code>;
111
111
  /**
112
112
  * Expression to deserialize this property on the given rdfjsResource.Resource to a Either<Error, this property type>.
113
113
  */
@@ -12,12 +12,12 @@ export declare class DiscriminantProperty extends AbstractProperty<DiscriminantP
12
12
  type: DiscriminantProperty.Type;
13
13
  } & ConstructorParameters<typeof AbstractProperty>[0]);
14
14
  get declaration(): Code;
15
+ get jsonName(): string;
15
16
  get jsonSchema(): AbstractProperty<DiscriminantProperty.Type>["jsonSchema"];
16
17
  get jsonSignature(): Maybe<Code>;
17
18
  private get initializer();
18
- private get override();
19
19
  constructorStatements(): readonly Code[];
20
- fromJsonStatements(): readonly Code[];
20
+ fromJsonExpression(): Maybe<Code>;
21
21
  fromRdfResourceValuesExpression(): Maybe<Code>;
22
22
  hashStatements({ variables, }: Parameters<AbstractProperty<DiscriminantProperty.Type>["hashStatements"]>[0]): readonly Code[];
23
23
  jsonUiSchemaElement({ variables, }: Parameters<AbstractProperty<DiscriminantProperty.Type>["jsonUiSchemaElement"]>[0]): Maybe<Code>;
@@ -22,49 +22,43 @@ export class DiscriminantProperty extends AbstractProperty {
22
22
  get declaration() {
23
23
  return code `readonly ${this.name}: ${this.type.name};`;
24
24
  }
25
+ get jsonName() {
26
+ return "@type";
27
+ }
25
28
  get jsonSchema() {
26
- if (this.override) {
27
- return Maybe.empty();
28
- }
29
29
  return Maybe.of({
30
- key: this.name,
30
+ key: this.jsonName,
31
31
  schema: this.type.values.length > 1
32
32
  ? code `${this.reusables.imports.z}.enum(${arrayOf(...this.type.values)})`
33
33
  : code `${this.reusables.imports.z}.literal(${literalOf(this.type.values[0])})`,
34
34
  });
35
35
  }
36
36
  get jsonSignature() {
37
- if (this.override) {
38
- return Maybe.empty();
39
- }
40
- return Maybe.of(code `readonly ${this.name}: ${this.type.name}`);
37
+ return Maybe.of(code `readonly "${this.jsonName}": ${this.type.name}`);
41
38
  }
42
39
  get initializer() {
43
40
  return code `${literalOf(this.namedObjectType.discriminantValue)} as const`;
44
41
  }
45
- get override() {
46
- return this.namedObjectType.parentObjectTypes.length > 0;
47
- }
48
42
  constructorStatements() {
49
43
  return [code `const ${this.name} = ${this.initializer};`];
50
44
  }
51
- fromJsonStatements() {
52
- return [code `const ${this.name} = ${this.initializer};`];
45
+ fromJsonExpression() {
46
+ return Maybe.empty();
53
47
  }
54
48
  fromRdfResourceValuesExpression() {
55
- return Maybe.of(code `${this.reusables.imports.Right}<${literalOf(this.namedObjectType.discriminantValue)}>(${this.initializer})`);
49
+ return Maybe.empty();
56
50
  }
57
51
  hashStatements({ variables, }) {
58
- if (this.override) {
52
+ if (this.namedObjectType.parentObjectTypes.length > 0) {
59
53
  return [];
60
54
  }
61
55
  return [code `${variables.hasher}.update(${variables.value});`];
62
56
  }
63
57
  jsonUiSchemaElement({ variables, }) {
64
- if (this.override) {
58
+ if (this.namedObjectType.parentObjectTypes.length > 0) {
65
59
  return Maybe.empty();
66
60
  }
67
- const scope = code `\`\${${variables.scopePrefix}}/properties/${this.name}\``;
61
+ const scope = code `\`\${${variables.scopePrefix}}/properties/${this.jsonName}\``;
68
62
  return Maybe.of(code `{ rule: { condition: { schema: { const: ${this.initializer} }, scope: ${scope} }, effect: "HIDE" }, scope: ${scope}, type: "Control" }`);
69
63
  }
70
64
  sparqlConstructTriplesExpression() {
@@ -74,10 +68,7 @@ export class DiscriminantProperty extends AbstractProperty {
74
68
  return Maybe.empty();
75
69
  }
76
70
  toJsonObjectMemberExpression({ variables, }) {
77
- if (this.override) {
78
- return Maybe.empty();
79
- }
80
- return Maybe.of(code `${this.name}: ${variables.value}`);
71
+ return Maybe.of(code `"${this.jsonName}": ${variables.value}`);
81
72
  }
82
73
  toRdfRdfResourceValuesStatements() {
83
74
  return [];
@@ -86,6 +77,9 @@ export class DiscriminantProperty extends AbstractProperty {
86
77
  return Maybe.empty();
87
78
  }
88
79
  }
80
+ __decorate([
81
+ Memoize()
82
+ ], DiscriminantProperty.prototype, "jsonName", null);
89
83
  __decorate([
90
84
  Memoize()
91
85
  ], DiscriminantProperty.prototype, "jsonSchema", null);
@@ -24,7 +24,7 @@ export declare class IdentifierProperty extends AbstractProperty<BlankNodeType |
24
24
  get jsonSignature(): Maybe<Code>;
25
25
  accessExpression({ variables, }: Parameters<AbstractProperty<BlankNodeType | IdentifierType | IriType>["accessExpression"]>[0]): Code;
26
26
  constructorStatements({ variables, }: Parameters<AbstractProperty<IdentifierType>["constructorStatements"]>[0]): readonly Code[];
27
- fromJsonStatements({ variables, }: Parameters<AbstractProperty<IdentifierType>["fromJsonStatements"]>[0]): readonly Code[];
27
+ fromJsonExpression({ variables, }: Parameters<AbstractProperty<IdentifierType>["fromJsonExpression"]>[0]): Maybe<Code>;
28
28
  fromRdfResourceValuesExpression({ variables, }: Parameters<AbstractProperty<IdentifierType>["fromRdfResourceValuesExpression"]>[0]): Maybe<Code>;
29
29
  hashStatements({ variables, }: Parameters<AbstractProperty<IdentifierType>["hashStatements"]>[0]): readonly Code[];
30
30
  jsonUiSchemaElement({ variables, }: Parameters<AbstractProperty<IdentifierType>["jsonUiSchemaElement"]>[0]): Maybe<Code>;
@@ -100,10 +100,10 @@ export class IdentifierProperty extends AbstractProperty {
100
100
  statements.push(joinCode(conversionBranches, { on: " else " }));
101
101
  return statements;
102
102
  }
103
- fromJsonStatements({ variables, }) {
104
- return [
105
- code `const ${this.name} = ${this.type.fromJsonExpression({ variables: { value: variables.jsonObject } })};`,
106
- ];
103
+ fromJsonExpression({ variables, }) {
104
+ return Maybe.of(this.type.fromJsonExpression({
105
+ variables: { value: variables.jsonObject },
106
+ }));
107
107
  }
108
108
  fromRdfResourceValuesExpression({ variables, }) {
109
109
  return Maybe.of(code `${this.type.fromRdfResourceValuesExpression({
@@ -1,12 +1,31 @@
1
1
  import { Maybe } from "purify-ts";
2
- import { code } from "../ts-poet-wrapper.js";
2
+ import { syntheticNamePrefix } from "../syntheticNamePrefix.js";
3
+ import { code, joinCode } from "../ts-poet-wrapper.js";
4
+ const variables = {
5
+ jsonObject: code `${syntheticNamePrefix}json`,
6
+ };
3
7
  export function NamedObjectType_fromJsonFunctionDeclaration() {
4
8
  if (!this.features.has("json")) {
5
9
  return Maybe.empty();
6
10
  }
11
+ const initializers = [];
12
+ const statements = [];
13
+ this.parentObjectTypes.forEach((parentObjectType) => {
14
+ initializers.push(code `...${parentObjectType.name}.fromJson(${variables.jsonObject})`);
15
+ });
16
+ for (const property of this.properties) {
17
+ property
18
+ .fromJsonExpression({
19
+ variables,
20
+ })
21
+ .ifJust((propertyFromJsonExpression) => {
22
+ initializers.push(code `${property.name}: ${propertyFromJsonExpression}`);
23
+ });
24
+ }
25
+ statements.push(code `return create({ ${joinCode(initializers, { on: ", " })} });`);
7
26
  return Maybe.of(code `\
8
- export function fromJson(json: ${this.jsonType().name}): ${this.name} {
9
- return create(propertiesFromJson(json));
27
+ export function fromJson(${variables.jsonObject}: ${this.jsonType().name}): ${this.name} {
28
+ ${joinCode(statements, { on: "\n" })}
10
29
  }`);
11
30
  }
12
31
  //# sourceMappingURL=NamedObjectType_fromJsonFunctionDeclaration.js.map
@@ -1,17 +1,110 @@
1
+ import { rdf } from "@tpluscode/rdf-ns-builders";
1
2
  import { Maybe } from "purify-ts";
2
3
  import { syntheticNamePrefix } from "../syntheticNamePrefix.js";
3
4
  import { code, joinCode } from "../ts-poet-wrapper.js";
5
+ const optionsVariable = `_${syntheticNamePrefix}options`;
6
+ const variables = {
7
+ context: code `${optionsVariable}.context`,
8
+ graph: code `${optionsVariable}.graph`,
9
+ ignoreRdfType: code `${optionsVariable}.ignoreRdfType`,
10
+ objectSet: code `${optionsVariable}.objectSet`,
11
+ preferredLanguages: code `${optionsVariable}.preferredLanguages`,
12
+ resource: code `${syntheticNamePrefix}resource`,
13
+ };
14
+ const propertyFromRdfResourceValuesExpressionVariable = {
15
+ context: variables.context,
16
+ graph: variables.graph,
17
+ objectSet: variables.objectSet,
18
+ preferredLanguages: variables.preferredLanguages,
19
+ resource: variables.resource,
20
+ };
4
21
  export function NamedObjectType_fromRdfResourceFunctionDeclaration() {
5
22
  if (!this.features.has("rdf")) {
6
23
  return Maybe.empty();
7
24
  }
25
+ const chains = [];
26
+ const partials = [];
27
+ this.parentObjectTypes.forEach((parentObjectType, parentObjectTypeI) => {
28
+ chains.push({
29
+ expression: code `${parentObjectType.name}._fromRdfResource(${variables.resource}, { ...${optionsVariable}, ignoreRdfType: true })`,
30
+ variable: `super${parentObjectTypeI}`,
31
+ });
32
+ partials.push(`super${parentObjectTypeI}`);
33
+ });
34
+ this.fromRdfType.ifJust((fromRdfType) => {
35
+ const fromRdfTypeVariable = this.fromRdfTypeVariable.unsafeCoerce();
36
+ const predicate = this.rdfjsTermExpression(rdf.type);
37
+ // Check the expected type and its known subtypes
38
+ const cases = new Set();
39
+ cases.add(fromRdfType.value);
40
+ for (const descendantFromRdfType of this.descendantFromRdfTypes) {
41
+ cases.add(descendantFromRdfType.value);
42
+ }
43
+ chains.push({
44
+ expression: code `!${variables.ignoreRdfType} ? ${variables.resource}.value(${predicate}, ${{ graph: variables.graph }})
45
+ .chain(actualRdfType => actualRdfType.toIri())
46
+ .chain((actualRdfType) => {
47
+ // Check the expected type and its known subtypes
48
+ switch (actualRdfType.value) {
49
+ ${[...cases].map((fromRdfType) => `case "${fromRdfType}":`).join("\n")}
50
+ return ${this.reusables.imports.Right}(true as const);
51
+ }
52
+
53
+ // Check arbitrary rdfs:subClassOf's of the expected type
54
+ if (${variables.resource}.isInstanceOf(${fromRdfTypeVariable}, ${{ graph: variables.graph }})) {
55
+ return ${this.reusables.imports.Right}(true as const);
56
+ }
57
+
58
+ return ${this.reusables.imports.Left}(new Error(\`\${${variables.resource}.identifier} has unexpected RDF type (actual: \${actualRdfType.value}, expected: ${fromRdfType.value})\`));
59
+ }) : ${this.reusables.imports.Right}(true as const)`,
60
+ variable: `_rdfTypeCheck`,
61
+ });
62
+ });
63
+ const propertyFromRdfResourceValuesExpressions = {};
64
+ for (const property of this.properties) {
65
+ property
66
+ .fromRdfResourceValuesExpression({
67
+ variables: propertyFromRdfResourceValuesExpressionVariable,
68
+ })
69
+ .ifJust((propertyFromRdfResourceValuesExpression) => {
70
+ propertyFromRdfResourceValuesExpressions[property.name] =
71
+ propertyFromRdfResourceValuesExpression;
72
+ });
73
+ }
74
+ if (Object.keys(propertyFromRdfResourceValuesExpressions).length > 0) {
75
+ chains.push({
76
+ expression: code `${this.reusables.snippets.sequenceRecord}(${propertyFromRdfResourceValuesExpressions})`,
77
+ variable: "properties",
78
+ });
79
+ partials.push("properties");
80
+ }
81
+ let partialsJoined;
82
+ switch (partials.length) {
83
+ case 0:
84
+ partialsJoined = code `{}`;
85
+ break;
86
+ case 1:
87
+ partialsJoined = code `${partials[0]}`;
88
+ break;
89
+ default:
90
+ partialsJoined = code `{ ${partials.map((partial) => `...${partial}`).join(", ")} }`;
91
+ break;
92
+ }
93
+ const statements = [];
94
+ const resultExpression = code `create(${partialsJoined})`;
95
+ if (chains.length === 0) {
96
+ statements.push(code `return ${this.reusables.imports.Right}(${resultExpression});`);
97
+ }
98
+ else {
99
+ statements.push(code `return ${chains
100
+ .reverse()
101
+ .reduce((acc, { expression, variable }, chainI) => code `(${expression}).${chainI === 0 ? "map" : "chain"}(${variable} => ${acc})`, code `(${resultExpression})`)}`);
102
+ }
8
103
  return Maybe.of(code `\
9
- export const fromRdfResource: ${this.reusables.snippets.FromRdfResourceFunction}<${this.name}> = (resource, options) => {
10
- ${joinCode([
11
- code `let { context, graph, ignoreRdfType = false, objectSet, preferredLanguages } = (options ?? {});`,
12
- code `if (!objectSet) { objectSet = new ${syntheticNamePrefix}RdfjsDatasetObjectSet(resource.dataset); }`,
13
- code `return ${code `${this.name}.propertiesFromRdfResource(resource, { context, graph, ignoreRdfType, objectSet, preferredLanguages }).map(create)`};`,
14
- ])}
15
- };`);
104
+ export const _fromRdfResource: ${this.reusables.snippets._FromRdfResourceFunction}<${this.name}> = (${variables.resource}, ${optionsVariable}) => {
105
+ ${joinCode(statements)}
106
+ }
107
+
108
+ export const fromRdfResource = ${this.reusables.snippets.wrap_FromRdfResourceFunction}(_fromRdfResource);`);
16
109
  }
17
110
  //# sourceMappingURL=NamedObjectType_fromRdfResourceFunctionDeclaration.js.map