@distrohelena/canton-typescript-sdk 0.1.28 → 0.1.29

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 (31) hide show
  1. package/dist/daml-interface/analysis/analyzed-daml-type-definition.d.ts +6 -1
  2. package/dist/daml-interface/analysis/analyzed-daml-type.d.ts +7 -1
  3. package/dist/daml-interface/analysis/daml-interface-analyzer.d.ts +6 -0
  4. package/dist/daml-interface/analysis/daml-interface-analyzer.js +92 -25
  5. package/dist/daml-interface/emission/named-type-emitter.d.ts +7 -1
  6. package/dist/daml-interface/emission/named-type-emitter.js +86 -21
  7. package/dist/daml-interface/emission/project-emitter.js +1 -1
  8. package/dist/daml-interface/emission/registry-emitter.js +3 -3
  9. package/dist/daml-interface/emission/support-file-emitter.d.ts +2 -0
  10. package/dist/daml-interface/emission/support-file-emitter.js +45 -20
  11. package/dist/daml-interface/emission/template-binding-emitter.js +58 -16
  12. package/dist/daml-interface/emission/type-script-name-resolver.d.ts +8 -2
  13. package/dist/daml-interface/emission/type-script-name-resolver.js +41 -6
  14. package/dist/daml-interface/index.d.ts +3 -3
  15. package/dist/daml-interface/index.js +3 -3
  16. package/dist/daml-interface/runtime/daml-event-source-normalizer.d.ts +8 -11
  17. package/dist/daml-interface/runtime/daml-event-source-normalizer.js +14 -11
  18. package/dist/daml-interface/runtime/daml-type-descriptor.d.ts +2 -1
  19. package/dist/daml-interface/runtime/daml-value-converter.d.ts +5 -2
  20. package/dist/daml-interface/runtime/daml-value-converter.js +11 -5
  21. package/dist/daml-interface/runtime/daml-value-materializer.d.ts +4 -1
  22. package/dist/daml-interface/runtime/daml-value-materializer.js +7 -1
  23. package/dist/daml-lf/daml-lf-compilation.d.ts +3 -0
  24. package/dist/daml-lf/daml-lf-compilation.js +4 -0
  25. package/dist/daml-lf/model/daml-lf-data-type.d.ts +19 -0
  26. package/dist/daml-lf/model/daml-lf-data-type.js +15 -0
  27. package/dist/daml-lf/model/daml-lf-type.d.ts +13 -0
  28. package/dist/daml-lf/model/daml-lf-type.js +24 -0
  29. package/dist/daml-lf/model/lf-2-model-mapper.d.ts +4 -0
  30. package/dist/daml-lf/model/lf-2-model-mapper.js +70 -2
  31. package/package.json +1 -1
@@ -95,10 +95,10 @@ export class TemplateBindingEmitter {
95
95
  namedImports.set(reference.path, imported);
96
96
  }
97
97
  return [
98
- 'import { DamlMaterializationError, DamlTemplate, decodeDamlValue, materializeDamlValue, normalizeDamlCreatedEventSource, normalizeDamlExercisedEventSource } from "@distrohelena/canton-typescript-sdk/daml-interface";',
98
+ 'import { DamlEventSourceNormalizer, DamlMaterializationError, DamlTemplate, DamlValueConverter, DamlValueMaterializer } from "@distrohelena/canton-typescript-sdk/daml-interface";',
99
99
  'import type { DamlCreatedEventSource, DamlDate, DamlExercisedEventMetadata, DamlExercisedEventSource, DamlNormalizedExercisedEvent, DamlNumeric, DamlParty, DamlTimestamp, DamlTypeDescriptor, DamlUnit } from "@distrohelena/canton-typescript-sdk/daml-interface";',
100
- `import { generatedDamlTypeDescriptorRegistry } from ${JSON.stringify(this.relativeFilePath(binding.path, "generated/support/descriptors.ts"))};`,
101
- ...[...namedImports.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([path, imported]) => `import type { ${[...imported.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([name, alias]) => `${name} as ${alias}`).join(", ")} } from ${JSON.stringify(path)};`),
100
+ `import { GeneratedDamlTypeDescriptorRegistry } from ${JSON.stringify(this.relativeFilePath(binding.path, "generated/support/descriptors.ts"))};`,
101
+ ...[...namedImports.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([path, imported]) => `import type { ${[...imported.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([name, alias]) => name === alias ? name : `${name} as ${alias}`).join(", ")} } from ${JSON.stringify(path)};`),
102
102
  ];
103
103
  }
104
104
  emitTemplateClass(binding, constructorParameters, constructorAssignments, orderedArguments, exercisedReturnType) {
@@ -115,9 +115,9 @@ export class TemplateBindingEmitter {
115
115
  " }",
116
116
  "",
117
117
  ` public static fromCreatedEvent(event: DamlCreatedEventSource): ${binding.className} {`,
118
- " const normalized = normalizeDamlCreatedEventSource(event);",
118
+ " const normalized = DamlEventSourceNormalizer.normalizeCreated(event);",
119
119
  ` ${binding.className}.assertTemplateIdentity(normalized.metadata.templateId);`,
120
- ` const fields = materializeDamlValue<${binding.createFieldsTypeName}>(decodeDamlValue(normalized.payload, ${binding.className}.descriptor, generatedDamlTypeDescriptorRegistry, "create arguments"));`,
120
+ ` const fields = DamlValueMaterializer.materialize<${binding.createFieldsTypeName}>(DamlValueConverter.decode(normalized.payload, ${binding.className}.descriptor, GeneratedDamlTypeDescriptorRegistry, "create arguments"));`,
121
121
  ` return new ${binding.className}(`,
122
122
  " normalized.contractId,",
123
123
  ...orderedArguments,
@@ -125,7 +125,7 @@ export class TemplateBindingEmitter {
125
125
  " }",
126
126
  "",
127
127
  ` public static fromExercisedEvent(event: DamlExercisedEventSource): ${exercisedReturnType} {`,
128
- " const normalized = normalizeDamlExercisedEventSource(event);",
128
+ " const normalized = DamlEventSourceNormalizer.normalizeExercised(event);",
129
129
  ` ${binding.className}.assertTemplateIdentity(normalized.metadata.templateId);`,
130
130
  " switch (normalized.choice) {",
131
131
  ...binding.choices.map((choice) => ` case ${JSON.stringify(choice.name)}:\n return ${choice.exercisedEventTypeName}.fromNormalizedEvent(normalized);`),
@@ -164,7 +164,7 @@ export class TemplateBindingEmitter {
164
164
  " }",
165
165
  "",
166
166
  ` public static fromExercisedEvent(event: DamlExercisedEventSource): ${choice.exercisedEventTypeName} {`,
167
- " const normalized = normalizeDamlExercisedEventSource(event);",
167
+ " const normalized = DamlEventSourceNormalizer.normalizeExercised(event);",
168
168
  ` ${choice.exercisedEventTypeName}.assertTemplateIdentity(normalized.metadata.templateId);`,
169
169
  ` return ${choice.exercisedEventTypeName}.fromNormalizedEvent(normalized);`,
170
170
  " }",
@@ -174,8 +174,8 @@ export class TemplateBindingEmitter {
174
174
  ` if (event.choice !== ${JSON.stringify(choice.name)}) {`,
175
175
  ` throw new DamlMaterializationError("choice", \`Expected choice '${choice.name}' but received '\${event.choice}'\`);`,
176
176
  " }",
177
- ` const argument = materializeDamlValue<${choice.parameterTypeName}>(decodeDamlValue(event.argument, ${choice.exercisedEventTypeName}.argumentDescriptor, generatedDamlTypeDescriptorRegistry, "choice argument"));`,
178
- ` const result = materializeDamlValue<${choice.returnTypeName}>(decodeDamlValue(event.result, ${choice.exercisedEventTypeName}.resultDescriptor, generatedDamlTypeDescriptorRegistry, "exercise result"));`,
177
+ ` const argument = DamlValueMaterializer.materialize<${choice.parameterTypeName}>(DamlValueConverter.decode(event.argument, ${choice.exercisedEventTypeName}.argumentDescriptor, GeneratedDamlTypeDescriptorRegistry, "choice argument"));`,
178
+ ` const result = DamlValueMaterializer.materialize<${choice.returnTypeName}>(DamlValueConverter.decode(event.result, ${choice.exercisedEventTypeName}.resultDescriptor, GeneratedDamlTypeDescriptorRegistry, "exercise result"));`,
179
179
  ` return new ${choice.exercisedEventTypeName}(event.contractId, argument, result, event.consuming, event.metadata);`,
180
180
  " }",
181
181
  "",
@@ -210,8 +210,10 @@ export class TemplateBindingEmitter {
210
210
  return `{ kind: "variant", constructors: [${type.constructors.map((constructor) => `{ constructor: ${JSON.stringify(constructor.constructor)}, payload: ${this.emitDescriptor(constructor.payload)} }`).join(", ")}] }`;
211
211
  case "enum":
212
212
  return `{ kind: "enum", constructors: [${type.constructors.map((constructor) => JSON.stringify(constructor)).join(", ")}] }`;
213
+ case "typeVariable":
214
+ throw new Error("Cannot emit generic DAML type variables");
213
215
  case "namedReference":
214
- return `{ kind: "namedReference", identity: { packageId: ${JSON.stringify(type.identity.packageId)}, moduleName: ${JSON.stringify(type.identity.moduleName)}, entityName: ${JSON.stringify(type.identity.name)} } }`;
216
+ return `{ kind: "namedReference", identity: { packageId: ${JSON.stringify(type.identity.packageId)}, moduleName: ${JSON.stringify(type.identity.moduleName)}, entityName: ${JSON.stringify(type.identity.name)} }, typeArguments: [${(type.typeArguments ?? []).map((argument) => this.emitDescriptor(argument)).join(", ")}] }`;
215
217
  }
216
218
  }
217
219
  getTypeName(type, namedReferences) {
@@ -234,8 +236,14 @@ export class TemplateBindingEmitter {
234
236
  return type.constructors.map((constructor) => `{ readonly tag: ${JSON.stringify(constructor.constructor)}; readonly value: ${this.getTypeName(constructor.payload, namedReferences)}; }`).join(" | ");
235
237
  case "enum":
236
238
  return type.constructors.map((constructor) => JSON.stringify(constructor)).join(" | ");
237
- case "namedReference":
238
- return this.getNamedReference(type.identity, namedReferences).alias;
239
+ case "typeVariable":
240
+ throw new Error("Cannot emit generic DAML type variables");
241
+ case "namedReference": {
242
+ const name = this.getNamedReference(type.identity, namedReferences).alias;
243
+ return (type.typeArguments?.length ?? 0) === 0
244
+ ? name
245
+ : `${name}<${(type.typeArguments ?? []).map((argument) => this.getTypeName(argument, namedReferences)).join(", ")}>`;
246
+ }
239
247
  }
240
248
  }
241
249
  getPrimitiveTypeName(type) {
@@ -262,7 +270,14 @@ export class TemplateBindingEmitter {
262
270
  return { kind: "contractId" };
263
271
  }
264
272
  else if (type.typeConReference !== undefined) {
265
- return { kind: "namedReference", identity: type.typeConReference };
273
+ if (type.typeArguments.length !== 0) {
274
+ throw new Error("Cannot emit generic DAML named type applications");
275
+ }
276
+ return {
277
+ kind: "namedReference",
278
+ identity: type.typeConReference,
279
+ typeArguments: [],
280
+ };
266
281
  }
267
282
  const arguments_ = type.typeArguments.map((argument) => this.normalizeType(argument));
268
283
  switch (type.builtinType) {
@@ -304,7 +319,26 @@ export class TemplateBindingEmitter {
304
319
  }
305
320
  const aliases = new Map();
306
321
  const usedAliases = new Set();
307
- for (const [key, reference] of [...identities.entries()].sort(([left], [right]) => left.localeCompare(right))) {
322
+ const exportedNameCounts = new Map();
323
+ for (const [key, reference] of identities) {
324
+ const file = namedTypeFiles.find((candidate) => candidate.packageId === reference.identity.packageId
325
+ && candidate.moduleName === reference.identity.moduleName);
326
+ const exportedName = file?.exportedTypeNamesByIdentity.get(key);
327
+ if (exportedName !== undefined) {
328
+ exportedNameCounts.set(exportedName, (exportedNameCounts.get(exportedName) ?? 0) + 1);
329
+ }
330
+ }
331
+ for (const [key, reference] of [...identities.entries()].sort(([, left], [, right]) => {
332
+ const leftIsLocal = left.identity.packageId === template.templateId.packageId
333
+ && left.identity.moduleName === template.templateId.moduleName;
334
+ const rightIsLocal = right.identity.packageId === template.templateId.packageId
335
+ && right.identity.moduleName === template.templateId.moduleName;
336
+ if (leftIsLocal !== rightIsLocal) {
337
+ return leftIsLocal ? -1 : 1;
338
+ }
339
+ return this.getNamedReferenceKey(left.identity)
340
+ .localeCompare(this.getNamedReferenceKey(right.identity));
341
+ })) {
308
342
  const file = namedTypeFiles.find((candidate) => candidate.packageId === reference.identity.packageId
309
343
  && candidate.moduleName === reference.identity.moduleName);
310
344
  const exportedName = file?.exportedTypeNamesByIdentity.get(key);
@@ -315,7 +349,11 @@ export class TemplateBindingEmitter {
315
349
  aliases.set(key, reference.identity.name);
316
350
  continue;
317
351
  }
318
- const baseAlias = this.toTypeName(`${reference.identity.packageId} ${reference.identity.moduleName} ${exportedName}`);
352
+ const isLocal = reference.identity.packageId === template.templateId.packageId
353
+ && reference.identity.moduleName === template.templateId.moduleName;
354
+ const baseAlias = exportedNameCounts.get(exportedName) === 1 || isLocal
355
+ ? exportedName
356
+ : this.toTypeName(`${file.namespaceAlias} ${exportedName}`);
319
357
  let alias = baseAlias;
320
358
  let escalation = 2;
321
359
  while (usedAliases.has(alias)) {
@@ -348,6 +386,9 @@ export class TemplateBindingEmitter {
348
386
  switch (type.kind) {
349
387
  case "namedReference":
350
388
  yield type;
389
+ for (const typeArgument of type.typeArguments ?? []) {
390
+ yield* this.walkNamedReferences(typeArgument);
391
+ }
351
392
  return;
352
393
  case "contractId": return;
353
394
  case "optional":
@@ -372,7 +413,8 @@ export class TemplateBindingEmitter {
372
413
  }
373
414
  return;
374
415
  case "primitive":
375
- case "enum": return;
416
+ case "enum":
417
+ case "typeVariable": return;
376
418
  }
377
419
  }
378
420
  getNamedReference(identity, namedReferences) {
@@ -2,15 +2,17 @@ import { AnalyzedChoice } from "../analysis/analyzed-choice.js";
2
2
  import { AnalyzedDamlType } from "../analysis/analyzed-daml-type.js";
3
3
  import { AnalyzedDamlTypeDefinition } from "../analysis/analyzed-daml-type-definition.js";
4
4
  import { AnalyzedTemplate, AnalyzedTemplateField } from "../analysis/analyzed-template.js";
5
+ import { DamlInterfacePackageMetadata } from "../analysis/daml-interface-analyzer.js";
5
6
  export declare class TypeScriptNameResolver {
6
7
  private readonly templatesByIdentity;
7
8
  private resolvedTemplates;
8
9
  private resolvedModules;
9
- constructor(templates?: readonly AnalyzedTemplate[]);
10
+ private packageMetadata;
11
+ constructor(templates?: readonly AnalyzedTemplate[], packageMetadata?: ReadonlyMap<string, DamlInterfacePackageMetadata>);
10
12
  /** Prepares a project-wide name table and rejects irreconcilable output collisions. */
11
13
  prepareTemplatesOrThrow(templates: readonly AnalyzedTemplate[]): void;
12
14
  /** Prepares shared package/module paths for template and named DAML type output. */
13
- prepareProjectOrThrow(templates: readonly AnalyzedTemplate[], typeDefinitions: readonly AnalyzedDamlTypeDefinition[]): void;
15
+ prepareProjectOrThrow(templates: readonly AnalyzedTemplate[], typeDefinitions: readonly AnalyzedDamlTypeDefinition[], packageMetadata?: ReadonlyMap<string, DamlInterfacePackageMetadata>): void;
14
16
  /** Resolves the full package/module/entity identity key for a template. */
15
17
  getTemplateIdentityKey(template: AnalyzedTemplate): string;
16
18
  /** Resolves the generated namespace alias for a template package/module. */
@@ -55,6 +57,10 @@ export declare class TypeScriptNameResolver {
55
57
  private assertDistinctTemplateOutput;
56
58
  private assertDistinctNamespaceAliases;
57
59
  private getModuleDirectory;
60
+ private getPackageDirectoryBaseName;
61
+ private getPackageNamespaceLabel;
62
+ private toPackageNameSegment;
63
+ private toPackageVersionSegment;
58
64
  private getTemplateFileBaseName;
59
65
  private getPackageModuleIdentityKey;
60
66
  private getPackageModuleIdentityKeyFromParts;
@@ -17,18 +17,20 @@ export class TypeScriptNameResolver {
17
17
  templatesByIdentity = new Map();
18
18
  resolvedTemplates = new Map();
19
19
  resolvedModules = new Map();
20
- constructor(templates = []) {
21
- this.prepareTemplatesOrThrow(templates);
20
+ packageMetadata = new Map();
21
+ constructor(templates = [], packageMetadata = new Map()) {
22
+ this.prepareProjectOrThrow(templates, [], packageMetadata);
22
23
  }
23
24
  /** Prepares a project-wide name table and rejects irreconcilable output collisions. */
24
25
  prepareTemplatesOrThrow(templates) {
25
- this.prepareProjectOrThrow(templates, []);
26
+ this.prepareProjectOrThrow(templates, [], this.packageMetadata);
26
27
  }
27
28
  /** Prepares shared package/module paths for template and named DAML type output. */
28
- prepareProjectOrThrow(templates, typeDefinitions) {
29
+ prepareProjectOrThrow(templates, typeDefinitions, packageMetadata = new Map()) {
29
30
  this.templatesByIdentity.clear();
30
31
  this.resolvedTemplates.clear();
31
32
  this.resolvedModules.clear();
33
+ this.packageMetadata = new Map(packageMetadata);
32
34
  for (const template of templates) {
33
35
  const identityKey = this.getTemplateIdentityKey(template);
34
36
  const existing = this.templatesByIdentity.get(identityKey);
@@ -56,7 +58,7 @@ export class TypeScriptNameResolver {
56
58
  const packageDirectories = this.resolveNames(packageIds.map((packageId) => ({
57
59
  value: packageId,
58
60
  key: `package\u0000${packageId}`,
59
- baseName: this.toKebabCase(packageId),
61
+ baseName: this.getPackageDirectoryBaseName(packageId),
60
62
  scope: "packages",
61
63
  })));
62
64
  const moduleDirectories = this.resolveNames([...modulesByIdentity.entries()].map(([identityKey, module]) => ({
@@ -74,7 +76,7 @@ export class TypeScriptNameResolver {
74
76
  const namespaceAliases = this.resolveNames([...modulesByIdentity.entries()].map(([identityKey, module]) => ({
75
77
  value: identityKey,
76
78
  key: `namespace\u0000${identityKey}`,
77
- baseName: this.safeNamespaceAlias(`${module.packageId} ${module.moduleName}`),
79
+ baseName: this.safeNamespaceAlias(`${this.getPackageNamespaceLabel(module.packageId)} ${module.moduleName}`),
78
80
  collisionSeparator: "_",
79
81
  })));
80
82
  const classNames = this.resolveNames(allTemplates.map((template) => ({
@@ -336,6 +338,39 @@ export class TypeScriptNameResolver {
336
338
  .map((segment) => this.toKebabCase(segment))
337
339
  .join("/");
338
340
  }
341
+ getPackageDirectoryBaseName(packageId) {
342
+ const metadata = this.packageMetadata.get(packageId);
343
+ if (metadata === undefined
344
+ || metadata.packageName.trim().length === 0
345
+ || metadata.packageVersion.trim().length === 0) {
346
+ return this.toKebabCase(packageId);
347
+ }
348
+ return `${this.toPackageNameSegment(metadata.packageName)}_${this.toPackageVersionSegment(metadata.packageVersion)}`;
349
+ }
350
+ getPackageNamespaceLabel(packageId) {
351
+ const packageName = this.packageMetadata.get(packageId)?.packageName.trim();
352
+ return packageName === undefined || packageName.length === 0
353
+ ? packageId
354
+ : packageName;
355
+ }
356
+ toPackageNameSegment(value) {
357
+ const segment = value
358
+ .trim()
359
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
360
+ .replace(/[^A-Za-z0-9]+/g, "-")
361
+ .replace(/-+/g, "-")
362
+ .replace(/^-|-$/g, "")
363
+ .toLowerCase();
364
+ return segment.length === 0 ? "package" : segment;
365
+ }
366
+ toPackageVersionSegment(value) {
367
+ const segment = value
368
+ .trim()
369
+ .replace(/[^A-Za-z0-9._-]+/g, "-")
370
+ .replace(/-+/g, "-")
371
+ .replace(/^-|-$/g, "");
372
+ return segment.length === 0 ? "unknown" : segment;
373
+ }
339
374
  getTemplateFileBaseName(template) {
340
375
  const fileName = this.toKebabCase(template.templateId.templateName);
341
376
  return fileName === "index" || fileName === "types"
@@ -14,9 +14,9 @@ export { DamlInterfaceWriteException } from "./errors/daml-interface-write.excep
14
14
  export { DamlInterfaceWriter } from "./writing/daml-interface-writer.js";
15
15
  export { DamlTemplate } from "./runtime/daml-template.js";
16
16
  export { DamlMaterializationError } from "./runtime/daml-materialization-error.js";
17
- export { decodeDamlValue } from "./runtime/daml-value-converter.js";
18
- export { materializeDamlValue } from "./runtime/daml-value-materializer.js";
19
- export { normalizeDamlCreatedEventSource, normalizeDamlExercisedEventSource, } from "./runtime/daml-event-source-normalizer.js";
17
+ export { DamlValueConverter } from "./runtime/daml-value-converter.js";
18
+ export { DamlValueMaterializer } from "./runtime/daml-value-materializer.js";
19
+ export { DamlEventSourceNormalizer } from "./runtime/daml-event-source-normalizer.js";
20
20
  export { DamlNumeric } from "../core/types/daml-numeric.js";
21
21
  export { DamlParty } from "../core/types/daml-party.js";
22
22
  export { DamlDate, DamlEnum, DamlGenMap, DamlRecord, DamlTextMap, DamlTimestamp, DamlUnit, DamlVariant, } from "../core/types/daml-values.js";
@@ -14,9 +14,9 @@ export { DamlInterfaceWriteException } from "./errors/daml-interface-write.excep
14
14
  export { DamlInterfaceWriter } from "./writing/daml-interface-writer.js";
15
15
  export { DamlTemplate } from "./runtime/daml-template.js";
16
16
  export { DamlMaterializationError } from "./runtime/daml-materialization-error.js";
17
- export { decodeDamlValue } from "./runtime/daml-value-converter.js";
18
- export { materializeDamlValue } from "./runtime/daml-value-materializer.js";
19
- export { normalizeDamlCreatedEventSource, normalizeDamlExercisedEventSource, } from "./runtime/daml-event-source-normalizer.js";
17
+ export { DamlValueConverter } from "./runtime/daml-value-converter.js";
18
+ export { DamlValueMaterializer } from "./runtime/daml-value-materializer.js";
19
+ export { DamlEventSourceNormalizer } from "./runtime/daml-event-source-normalizer.js";
20
20
  export { DamlNumeric } from "../core/types/daml-numeric.js";
21
21
  export { DamlParty } from "../core/types/daml-party.js";
22
22
  export { DamlDate, DamlEnum, DamlGenMap, DamlRecord, DamlTextMap, DamlTimestamp, DamlUnit, DamlVariant, } from "../core/types/daml-values.js";
@@ -39,14 +39,11 @@ export type DamlNormalizedExercisedEvent = {
39
39
  readonly consuming: boolean;
40
40
  readonly metadata: DamlExercisedEventMetadata;
41
41
  };
42
- /**
43
- * Recognizes a Ledger API create event, a contract response/wrapper, or a
44
- * PQS/JSON contract record and produces one immutable runtime shape.
45
- */
46
- export declare function normalizeDamlCreatedEventSource(source: DamlCreatedEventSource): DamlNormalizedCreatedEvent;
47
- /**
48
- * Recognizes a Ledger API exercise event or a PQS/JSON exercise record and
49
- * produces one immutable runtime shape. Choice information may be supplied by
50
- * PQS's exercise-type relation and identity by its contract relation.
51
- */
52
- export declare function normalizeDamlExercisedEventSource(source: DamlExercisedEventSource): DamlNormalizedExercisedEvent;
42
+ /** Recognizes Ledger API and PQS event sources as immutable DAML event shapes. */
43
+ export declare class DamlEventSourceNormalizer {
44
+ private constructor();
45
+ /** Recognizes a Ledger API create event, contract response/wrapper, or PQS/JSON contract record. */
46
+ static normalizeCreated(source: DamlCreatedEventSource): DamlNormalizedCreatedEvent;
47
+ /** Recognizes a Ledger API exercise event or a PQS/JSON exercise record. */
48
+ static normalizeExercised(source: DamlExercisedEventSource): DamlNormalizedExercisedEvent;
49
+ }
@@ -5,11 +5,19 @@ import { ActiveContract } from "../../transports/grpc/generated/canton/com/daml/
5
5
  import { DamlMaterializationError } from "./daml-materialization-error.js";
6
6
  const DAML_MIN_TIMESTAMP_SECONDS = -62135596800n;
7
7
  const DAML_MAX_TIMESTAMP_SECONDS = 253402300799n;
8
- /**
9
- * Recognizes a Ledger API create event, a contract response/wrapper, or a
10
- * PQS/JSON contract record and produces one immutable runtime shape.
11
- */
12
- export function normalizeDamlCreatedEventSource(source) {
8
+ /** Recognizes Ledger API and PQS event sources as immutable DAML event shapes. */
9
+ export class DamlEventSourceNormalizer {
10
+ constructor() { }
11
+ /** Recognizes a Ledger API create event, contract response/wrapper, or PQS/JSON contract record. */
12
+ static normalizeCreated(source) {
13
+ return normalizeDamlCreatedEventSource(source);
14
+ }
15
+ /** Recognizes a Ledger API exercise event or a PQS/JSON exercise record. */
16
+ static normalizeExercised(source) {
17
+ return normalizeDamlExercisedEventSource(source);
18
+ }
19
+ }
20
+ function normalizeDamlCreatedEventSource(source) {
13
21
  const recognized = findNestedEvent(source, "created");
14
22
  const { event } = recognized;
15
23
  const contractId = requiredString(event, ["contractId", "contract_id"], "contract ID", "created event source");
@@ -23,12 +31,7 @@ export function normalizeDamlCreatedEventSource(source) {
23
31
  metadata,
24
32
  });
25
33
  }
26
- /**
27
- * Recognizes a Ledger API exercise event or a PQS/JSON exercise record and
28
- * produces one immutable runtime shape. Choice information may be supplied by
29
- * PQS's exercise-type relation and identity by its contract relation.
30
- */
31
- export function normalizeDamlExercisedEventSource(source) {
34
+ function normalizeDamlExercisedEventSource(source) {
32
35
  const recognized = findNestedEvent(source, "exercised");
33
36
  const { event } = recognized;
34
37
  const contractId = requiredString(event, ["contractId", "contract_id"], "contract ID", "exercised event source");
@@ -55,12 +55,13 @@ export type DamlEnumDescriptor = {
55
55
  export type DamlNamedReferenceDescriptor = {
56
56
  readonly kind: "namedReference";
57
57
  readonly identity: DamlTypeIdentity;
58
+ readonly typeArguments: readonly DamlTypeDescriptor[];
58
59
  };
59
60
  /** A closed description of a serializable DAML value. */
60
61
  export type DamlTypeDescriptor = DamlPrimitiveDescriptor | DamlContractIdDescriptor | DamlOptionalDescriptor | DamlListDescriptor | DamlTextMapDescriptor | DamlGenMapDescriptor | DamlRecordDescriptor | DamlVariantDescriptor | DamlEnumDescriptor | DamlNamedReferenceDescriptor;
61
62
  /** Resolves named descriptors lazily so recursive data types remain finite. */
62
63
  export type DamlTypeDescriptorRegistry = {
63
- readonly resolve: (identity: DamlTypeIdentity) => (() => DamlTypeDescriptor) | undefined;
64
+ readonly resolve: (identity: DamlTypeIdentity, typeArguments: readonly DamlTypeDescriptor[]) => DamlTypeDescriptor | undefined;
64
65
  };
65
66
  export type DamlValueSource = {
66
67
  readonly kind: "protobuf";
@@ -3,5 +3,8 @@ import { DamlNumeric } from "../../core/types/daml-numeric.js";
3
3
  import { DamlParty } from "../../core/types/daml-party.js";
4
4
  import { DamlTypeDescriptor, DamlTypeDescriptorRegistry, DamlValueSource } from "./daml-type-descriptor.js";
5
5
  export type DamlDecodedValue = DamlUnit | boolean | bigint | DamlDate | DamlTimestamp | DamlNumeric | DamlParty | string | undefined | readonly DamlDecodedValue[] | DamlTextMap | DamlGenMap | DamlRecord | DamlVariant | DamlEnum;
6
- /** Decodes a protobuf or JSON/PQS DAML value according to its generated descriptor. */
7
- export declare function decodeDamlValue(source: DamlValueSource, descriptor: DamlTypeDescriptor, registry: DamlTypeDescriptorRegistry, path: string): DamlDecodedValue;
6
+ /** Decodes protobuf or JSON/PQS DAML values according to generated descriptors. */
7
+ export declare class DamlValueConverter {
8
+ private constructor();
9
+ static decode(source: DamlValueSource, descriptor: DamlTypeDescriptor, registry: DamlTypeDescriptorRegistry, path: string): DamlDecodedValue;
10
+ }
@@ -8,14 +8,20 @@ const DAML_MIN_TIMESTAMP_MICROSECONDS = -62135596800000000n;
8
8
  const DAML_MAX_TIMESTAMP_MICROSECONDS = 253402300799999999n;
9
9
  const DAML_MIN_INT64 = -9223372036854775808n;
10
10
  const DAML_MAX_INT64 = 9223372036854775807n;
11
- /** Decodes a protobuf or JSON/PQS DAML value according to its generated descriptor. */
12
- export function decodeDamlValue(source, descriptor, registry, path) {
11
+ /** Decodes protobuf or JSON/PQS DAML values according to generated descriptors. */
12
+ export class DamlValueConverter {
13
+ constructor() { }
14
+ static decode(source, descriptor, registry, path) {
15
+ return decodeDamlValue(source, descriptor, registry, path);
16
+ }
17
+ }
18
+ function decodeDamlValue(source, descriptor, registry, path) {
13
19
  if (descriptor.kind === "namedReference") {
14
- const factory = registry.resolve(descriptor.identity);
15
- if (factory === undefined) {
20
+ const resolved = registry.resolve(descriptor.identity, descriptor.typeArguments);
21
+ if (resolved === undefined) {
16
22
  throw materializationError(path, "named type descriptor is not registered");
17
23
  }
18
- return decodeDamlValue(source, factory(), registry, path);
24
+ return decodeDamlValue(source, resolved, registry, path);
19
25
  }
20
26
  else if (source.kind === "protobuf") {
21
27
  return decodeProtobufValue(source.value, descriptor, registry, path);
@@ -1,3 +1,6 @@
1
1
  import { DamlDecodedValue } from "./daml-value-converter.js";
2
2
  /** Converts decoded DAML containers into the shapes exposed by generated TypeScript declarations. */
3
- export declare function materializeDamlValue<T>(value: DamlDecodedValue): T;
3
+ export declare class DamlValueMaterializer {
4
+ private constructor();
5
+ static materialize<T>(value: DamlDecodedValue): T;
6
+ }
@@ -1,6 +1,12 @@
1
1
  import { DamlEnum, DamlGenMap, DamlRecord, DamlTextMap, DamlVariant, } from "../../core/types/daml-values.js";
2
2
  /** Converts decoded DAML containers into the shapes exposed by generated TypeScript declarations. */
3
- export function materializeDamlValue(value) {
3
+ export class DamlValueMaterializer {
4
+ constructor() { }
5
+ static materialize(value) {
6
+ return materializeDamlValue(value);
7
+ }
8
+ }
9
+ function materializeDamlValue(value) {
4
10
  return materialize(value);
5
11
  }
6
12
  function materialize(value) {
@@ -1,5 +1,6 @@
1
1
  import { DamlLfChoice } from "./model/daml-lf-choice.js";
2
2
  import { DamlLfModule } from "./model/daml-lf-module.js";
3
+ import { DamlLfPackage } from "./model/daml-lf-package.js";
3
4
  import { DamlLfTemplate } from "./model/daml-lf-template.js";
4
5
  import { DamlLfTemplateId } from "./model/daml-lf-template-id.js";
5
6
  import { DamlLfValueDefinition } from "./model/daml-lf-value-definition.js";
@@ -30,6 +31,8 @@ export declare class DamlLfCompilation {
30
31
  definitionName: string;
31
32
  };
32
33
  getTemplates(): readonly DamlLfTemplate[];
34
+ /** Returns the packages available to this compilation. */
35
+ getPackages(): readonly DamlLfPackage[];
33
36
  getTemplateChoicesOrThrow(templateId: DamlLfTemplateId): readonly DamlLfChoice[];
34
37
  private buildIndexes;
35
38
  private validateReferencesOrThrow;
@@ -70,6 +70,10 @@ export class DamlLfCompilation {
70
70
  getTemplates() {
71
71
  return [...this.templates.values()];
72
72
  }
73
+ /** Returns the packages available to this compilation. */
74
+ getPackages() {
75
+ return this.workspace.packages;
76
+ }
73
77
  getTemplateChoicesOrThrow(templateId) {
74
78
  return this.getTemplateOrThrow(templateId).choices;
75
79
  }
@@ -2,6 +2,23 @@ import { DamlLfDefinition } from "./daml-lf-definition.js";
2
2
  import { DamlLfField } from "./daml-lf-field.js";
3
3
  import { DamlLfNodeKind } from "./daml-lf-node-kind.js";
4
4
  import { DamlLfType } from "./daml-lf-type.js";
5
+ export type DamlLfTypeParameterKind = {
6
+ readonly kind: "star";
7
+ } | {
8
+ readonly kind: "nat";
9
+ } | {
10
+ readonly kind: "arrow";
11
+ readonly parameters: readonly DamlLfTypeParameterKind[];
12
+ readonly result: DamlLfTypeParameterKind;
13
+ } | {
14
+ readonly kind: "unknown";
15
+ readonly internedKindIndex?: number;
16
+ };
17
+ export interface DamlLfTypeParameter {
18
+ readonly name?: string;
19
+ readonly internedStringIndex: number;
20
+ readonly kind: DamlLfTypeParameterKind;
21
+ }
5
22
  export type DamlLfDataTypeDefinition = {
6
23
  readonly kind: "record";
7
24
  readonly fields: readonly DamlLfField[];
@@ -20,9 +37,11 @@ export declare class DamlLfDataType extends DamlLfDefinition {
20
37
  readonly nodeKind = DamlLfNodeKind.dataType;
21
38
  readonly definition: DamlLfDataTypeDefinition;
22
39
  readonly fields: readonly DamlLfField[];
40
+ readonly typeParameters: readonly DamlLfTypeParameter[];
23
41
  constructor(init: {
24
42
  name: string;
25
43
  definition?: DamlLfDataTypeDefinition;
26
44
  fields?: readonly DamlLfField[];
45
+ typeParameters?: readonly DamlLfTypeParameter[];
27
46
  });
28
47
  }
@@ -4,6 +4,7 @@ export class DamlLfDataType extends DamlLfDefinition {
4
4
  nodeKind = DamlLfNodeKind.dataType;
5
5
  definition;
6
6
  fields;
7
+ typeParameters;
7
8
  constructor(init) {
8
9
  super({
9
10
  name: init.name,
@@ -15,5 +16,19 @@ export class DamlLfDataType extends DamlLfDefinition {
15
16
  this.fields = this.definition.kind === "record"
16
17
  ? this.definition.fields
17
18
  : [];
19
+ this.typeParameters = Object.freeze((init.typeParameters ?? []).map((parameter) => Object.freeze({
20
+ ...parameter,
21
+ kind: freezeTypeParameterKind(parameter.kind),
22
+ })));
18
23
  }
19
24
  }
25
+ function freezeTypeParameterKind(kind) {
26
+ if (kind.kind !== "arrow") {
27
+ return Object.freeze({ ...kind });
28
+ }
29
+ return Object.freeze({
30
+ kind: "arrow",
31
+ parameters: Object.freeze(kind.parameters.map((parameter) => freezeTypeParameterKind(parameter))),
32
+ result: freezeTypeParameterKind(kind.result),
33
+ });
34
+ }
@@ -1,16 +1,29 @@
1
1
  import { DamlLfBuiltinType } from "./daml-lf-builtin-type.js";
2
+ import { DamlLfTypeParameter } from "./daml-lf-data-type.js";
2
3
  import { DamlLfNodeKind } from "./daml-lf-node-kind.js";
3
4
  import { TypeConReference } from "./type-con-reference.js";
5
+ export interface DamlLfTypeVariableReference {
6
+ readonly name?: string;
7
+ readonly internedStringIndex: number;
8
+ }
9
+ export interface DamlLfDiagnosticForall {
10
+ readonly typeParameters: readonly DamlLfTypeParameter[];
11
+ readonly body: DamlLfType;
12
+ }
4
13
  export declare class DamlLfType {
5
14
  readonly nodeKind = DamlLfNodeKind.type;
6
15
  readonly builtinType: DamlLfBuiltinType;
7
16
  readonly numericScale?: number;
8
17
  readonly typeConReference?: TypeConReference;
18
+ readonly typeVariable?: DamlLfTypeVariableReference;
19
+ readonly diagnosticForall?: DamlLfDiagnosticForall;
9
20
  readonly typeArguments: readonly DamlLfType[];
10
21
  constructor(init: {
11
22
  builtinType?: DamlLfBuiltinType;
12
23
  numericScale?: number;
13
24
  typeConReference?: TypeConReference;
25
+ typeVariable?: DamlLfTypeVariableReference;
26
+ diagnosticForall?: DamlLfDiagnosticForall;
14
27
  typeArguments?: readonly DamlLfType[];
15
28
  });
16
29
  }
@@ -5,6 +5,8 @@ export class DamlLfType {
5
5
  builtinType;
6
6
  numericScale;
7
7
  typeConReference;
8
+ typeVariable;
9
+ diagnosticForall;
8
10
  typeArguments;
9
11
  constructor(init) {
10
12
  this.builtinType = init.builtinType ?? DamlLfBuiltinType.unknown;
@@ -12,6 +14,28 @@ export class DamlLfType {
12
14
  this.numericScale = init.numericScale;
13
15
  }
14
16
  this.typeConReference = init.typeConReference;
17
+ this.typeVariable = init.typeVariable === undefined
18
+ ? undefined
19
+ : Object.freeze({ ...init.typeVariable });
20
+ this.diagnosticForall = init.diagnosticForall === undefined
21
+ ? undefined
22
+ : Object.freeze({
23
+ typeParameters: Object.freeze(init.diagnosticForall.typeParameters.map((parameter) => Object.freeze({
24
+ ...parameter,
25
+ kind: freezeTypeParameterKind(parameter.kind),
26
+ }))),
27
+ body: init.diagnosticForall.body,
28
+ });
15
29
  this.typeArguments = Object.freeze([...(init.typeArguments ?? [])]);
16
30
  }
17
31
  }
32
+ function freezeTypeParameterKind(kind) {
33
+ if (kind.kind !== "arrow") {
34
+ return Object.freeze({ ...kind });
35
+ }
36
+ return Object.freeze({
37
+ kind: "arrow",
38
+ parameters: Object.freeze(kind.parameters.map((parameter) => freezeTypeParameterKind(parameter))),
39
+ result: freezeTypeParameterKind(kind.result),
40
+ });
41
+ }
@@ -4,6 +4,9 @@ import { DamlLfPackage } from "./daml-lf-package.js";
4
4
  export declare class Lf2ModelMapper {
5
5
  static mapPackage(packageId: string, rawPackage: LfArchivePackage, languageVersion: DamlLfLanguageVersion): DamlLfPackage;
6
6
  private static mapType;
7
+ private static mapTypeParameter;
8
+ private static mapTypeVariableReference;
9
+ private static mapTypeParameterKind;
7
10
  private static mapBuiltinType;
8
11
  private static readNumericScale;
9
12
  private static mapModule;
@@ -21,5 +24,6 @@ export declare class Lf2ModelMapper {
21
24
  private static mapUpdateExpression;
22
25
  private static mapTemplateReferenceOrThrow;
23
26
  private static resolveInternedString;
27
+ private static resolveInternedStringOrUndefined;
24
28
  private static resolveInternedDottedName;
25
29
  }