* Turns each field-less map record (a CDDL `{* text => V}` group the projector lifts out * as a synthetic type) into an alias of `{ map: V }`. Emitted as a record, it would have no * declared keys, so every received entry would be warned about and dropped as undeclared; * as a map, each key is de
(schema)
| 316 | * decisions 4, 7, 9 and 10). An alias keeps the type's exported name. |
| 317 | */ |
| 318 | function aliasMapRecords(schema) { |
| 319 | const sections = [schema, ...Object.values(schema.vendor ?? {})] |
| 320 | const maps = new Set() |
| 321 | for (const section of sections) { |
| 322 | for (const [name, node] of Object.entries(section.types ?? {})) { |
| 323 | if (node.kind === 'record' && node.map !== undefined && node.fields.length === 0) { |
| 324 | section.types[name] = { kind: 'alias', type: { map: node.map } } |
| 325 | maps.add(name) |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | // A union resolves its variants as records or unions, so it cannot select a map. |
| 330 | for (const section of sections) { |
| 331 | for (const [name, node] of Object.entries(section.types ?? {})) { |
| 332 | const variant = node.kind === 'union' ? node.variants.find((v) => maps.has(v)) : undefined |
| 333 | if (variant) throw new Error(`${name}: union variant ${variant} is a map, which a union cannot select`) |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // ============================================================ |
| 339 | // Schema-derived domain grouping |