* 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)
| 598 | * `params`/`result` are null when there are no params / no return value. |
| 599 | */ |
| 600 | function buildModel(ast) { |
| 601 | const model = {} |
| 602 | const resultTypes = buildResultTypeNames(ast) |
| 603 | const ensure = (domain) => (model[domain] ??= { commands: [], events: [] }) |
| 604 | |
| 605 | // A vendor entry carries its namespace so the schema projector can file it under |
| 606 | // `vendor.<namespace>`; spec entries carry none, so a vendor-free AST yields the same model. |
| 607 | const provenance = (entry) => (entry.vendor ? { vendor: entry.vendor } : {}) |
| 608 | |
| 609 | for (const c of extractCommands(ast)) { |
| 610 | const result = c.cddlName + 'Result' |
| 611 | ensure(c.domain).commands.push({ |
| 612 | method: c.methodStr, |
| 613 | name: c.methodName, |
| 614 | params: c.hasParams ? c.paramsCddl : null, |
| 615 | result: resultTypes.has(result) ? result : null, |
| 616 | ...provenance(c), |
| 617 | }) |
| 618 | } |
| 619 | |
| 620 | for (const e of extractEvents(ast)) { |
| 621 | ensure(e.domain).events.push({ |
| 622 | method: e.methodStr, |
| 623 | name: e.eventName, |
| 624 | params: e.paramsCddl || null, |
| 625 | ...provenance(e), |
| 626 | }) |
| 627 | } |
| 628 | |
| 629 | return model |
| 630 | } |
| 631 | |
| 632 | /** Result type names the spec defines with a value; an absent or `EmptyResult`-aliased result is void. */ |
| 633 | function buildResultTypeNames(ast) { |
no test coverage detected