(ast, model)
| 381 | * @returns {{schemaVersion: number, commands: object[], events: object[], types: object}} The schema. |
| 382 | */ |
| 383 | export function projectSchema(ast, model) { |
| 384 | const types = {} |
| 385 | for (const def of normalizeAst(ast)) { |
| 386 | if (!def?.Name) continue |
| 387 | const node = projectType(def) |
| 388 | // Types the normalizer minted for anonymous CDDL constructs (hoisted enums / |
| 389 | // inline records, union arms) carry their decomposition so a binding can name |
| 390 | // or nest them idiomatically without parsing the synthetic key. `owner` is the |
| 391 | // type the construct was lifted out of; `label` is the member name within it. |
| 392 | if (def['x-selenium-synthetic']) { |
| 393 | node.synthetic = true |
| 394 | node.owner = def['x-selenium-owner'] |
| 395 | node.label = def['x-selenium-label'] |
| 396 | } |
| 397 | types[def.Name] = node |
| 398 | } |
| 399 | for (const [name, node] of Object.entries(types)) |
| 400 | if (node.kind === 'union') node.selector = unionSelector(name, types) |
| 401 | // Override the result-grouping unions: they are dispatched by request id, so a |
| 402 | // payload selector for them is meaningless (and would be empty/ambiguous). |
| 403 | for (const name of correlatedUnions(types)) types[name].selector = { correlated: true } |
| 404 | |
| 405 | const commands = [] |
| 406 | const events = [] |
| 407 | const envelopeParams = commandEnvelopeParams(types) |
| 408 | for (const [domain, entry] of Object.entries(model)) { |
| 409 | for (const c of entry.commands ?? []) |
| 410 | commands.push({ |
| 411 | domain, |
| 412 | method: c.method, |
| 413 | name: c.name, |
| 414 | // Prefer the envelope's params (it captures inline params the model drops). |
| 415 | params: typeRef(envelopeParams.get(c.method) ?? c.params), |
| 416 | result: typeRef(c.result), |
| 417 | }) |
| 418 | for (const e of entry.events ?? []) |
| 419 | events.push({ domain, method: e.method, name: e.name, params: typeRef(envelopeParams.get(e.method) ?? e.params) }) |
| 420 | } |
| 421 | |
| 422 | return { schemaVersion: 1, commands, events, types } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Fail-closed validation: every type reference resolves, and no type projects to |
no test coverage detected