From 7517b85c5d67a529cbdcf4d0b43d73a1c6f4177e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20=C3=98in=C3=A6s=20Myrseth?= Date: Sat, 18 Jan 2020 23:57:33 +0100 Subject: [PATCH] schema: Add status arrays Not exactly sure how fetching arrays without indices work. In TSH not giving an item index means to fetch all items in the given array: xstatus Audio Input Connectors HDMI Mute vs: xstatus Audio Input Connectors HDMI[1] Mute --- src/schema/index.ts | 7 ++++++- src/schema/nodes.ts | 6 ++++++ test/schema/index.spec.ts | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/schema/index.ts b/src/schema/index.ts index ed26773..3a99e6d 100644 --- a/src/schema/index.ts +++ b/src/schema/index.ts @@ -10,6 +10,7 @@ import { List, Type, Generic, + ArrayTree, } from './nodes'; export interface GenerateOpts { @@ -172,7 +173,11 @@ function parseStatusTree(root: Root, schema: any, tree: Node, path: string[]) { const vs = parseValueSpace(value.ValueSpace, fullPath); tree.addChild(new Member(key, vs, { docstring: value.description })); } else if (Array.isArray(value)) { - console.error(`warn: ${fullPath.join('/')} arrays not yet supported`); + if (value.length !== 1) { + throw new Error(`error: ${fullPath.join('/')} contains multiple entries`); + } + const subTree = tree.addChild(new ArrayTree(key)); + parseConfigTree(root, value[0], subTree, path.concat([key])); } else { const subTree = tree.addChild(new Tree(key)); parseStatusTree(root, value, subTree, path.concat(key)); diff --git a/src/schema/nodes.ts b/src/schema/nodes.ts index 0a713b1..8cae1a8 100644 --- a/src/schema/nodes.ts +++ b/src/schema/nodes.ts @@ -268,6 +268,12 @@ export class Tree extends Node { } } +export class ArrayTree extends Tree { + serialize(): string { + return `${super.serialize()}[]`; + } +} + export class Command extends Node { private params?: Type; private retval?: Type; diff --git a/test/schema/index.spec.ts b/test/schema/index.spec.ts index 354f4f0..0635132 100644 --- a/test/schema/index.spec.ts +++ b/test/schema/index.spec.ts @@ -10,6 +10,7 @@ import { Literal, List, Generic, + ArrayTree, } from '../../src/schema/nodes'; describe('schemas', () => { @@ -318,6 +319,43 @@ describe('schemas', () => { children: expect.arrayContaining([main, statusTree]), }); }); + + it('can fetch arrays', () => { + const schema = { + StatusSchema: { + Audio: { + Input: { + Connectors: { + HDMI: [{ + Mute: { + access: 'public-api', + read: 'Admin;User', + ValueSpace: { + type: 'Literal', + Value: [ + 'On', + 'Off' + ], + }, + }, + }], + }, + }, + }, + }, + }; + + statusTree + .addChild(new Tree('Audio')) + .addChild(new Tree('Input')) + .addChild(new Tree('Connectors')) + .addChild(new ArrayTree('HDMI')) + .addChild(new Member('Mute', new Literal('On', 'Off'))); + + expect(parse(schema)).toMatchObject({ + children: expect.arrayContaining([main, statusTree]), + }); + }); }); }); });