* Build the binding-neutral model from the AST. Type refs are CDDL names. * Shape per domain key: * { commands: [{ method, name, params, result }], * events: [{ method, name, params }] } * `params`/`result` are null when there are no params / no return value.
(ast)
| 575 | * `params`/`result` are null when there are no params / no return value. |
| 576 | */ |
| 577 | function buildModel(ast) { |
| 578 | const model = {} |
| 579 | const resultTypes = buildResultTypeNames(ast) |
| 580 | const ensure = (domain) => (model[domain] ??= { commands: [], events: [] }) |
| 581 | |
| 582 | // A vendor entry carries its namespace so the schema projector can file it under |
| 583 | // `vendor.<namespace>`; spec entries carry none, so a vendor-free AST yields the same model. |
| 584 | const provenance = (entry) => (entry.vendor ? { vendor: entry.vendor } : {}) |
| 585 | |
| 586 | for (const c of extractCommands(ast)) { |
| 587 | const result = c.cddlName + 'Result' |
| 588 | ensure(c.domain).commands.push({ |
| 589 | method: c.methodStr, |
| 590 | name: c.methodName, |
| 591 | params: c.hasParams ? c.paramsCddl : null, |
| 592 | result: resultTypes.has(result) ? result : null, |
| 593 | ...provenance(c), |
| 594 | }) |
| 595 | } |
| 596 | |
| 597 | for (const e of extractEvents(ast)) { |
| 598 | ensure(e.domain).events.push({ |
| 599 | method: e.methodStr, |
| 600 | name: e.eventName, |
| 601 | params: e.paramsCddl || null, |
| 602 | ...provenance(e), |
| 603 | }) |
| 604 | } |
| 605 | |
| 606 | return model |
| 607 | } |
| 608 | |
| 609 | /** Result type names the spec defines with a value; an absent or `EmptyResult`-aliased result is void. */ |
| 610 | function buildResultTypeNames(ast) { |
no test coverage detected