Skip to content

Commit 7517b85

Browse files
author
Martin Øinæs Myrseth
committed
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
1 parent af4642d commit 7517b85

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/schema/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
List,
1111
Type,
1212
Generic,
13+
ArrayTree,
1314
} from './nodes';
1415

1516
export interface GenerateOpts {
@@ -172,7 +173,11 @@ function parseStatusTree(root: Root, schema: any, tree: Node, path: string[]) {
172173
const vs = parseValueSpace(value.ValueSpace, fullPath);
173174
tree.addChild(new Member(key, vs, { docstring: value.description }));
174175
} else if (Array.isArray(value)) {
175-
console.error(`warn: ${fullPath.join('/')} arrays not yet supported`);
176+
if (value.length !== 1) {
177+
throw new Error(`error: ${fullPath.join('/')} contains multiple entries`);
178+
}
179+
const subTree = tree.addChild(new ArrayTree(key));
180+
parseConfigTree(root, value[0], subTree, path.concat([key]));
176181
} else {
177182
const subTree = tree.addChild(new Tree(key));
178183
parseStatusTree(root, value, subTree, path.concat(key));

src/schema/nodes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ export class Tree extends Node {
268268
}
269269
}
270270

271+
export class ArrayTree extends Tree {
272+
serialize(): string {
273+
return `${super.serialize()}[]`;
274+
}
275+
}
276+
271277
export class Command extends Node {
272278
private params?: Type;
273279
private retval?: Type;

test/schema/index.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Literal,
1111
List,
1212
Generic,
13+
ArrayTree,
1314
} from '../../src/schema/nodes';
1415

1516
describe('schemas', () => {
@@ -318,6 +319,43 @@ describe('schemas', () => {
318319
children: expect.arrayContaining([main, statusTree]),
319320
});
320321
});
322+
323+
it('can fetch arrays', () => {
324+
const schema = {
325+
StatusSchema: {
326+
Audio: {
327+
Input: {
328+
Connectors: {
329+
HDMI: [{
330+
Mute: {
331+
access: 'public-api',
332+
read: 'Admin;User',
333+
ValueSpace: {
334+
type: 'Literal',
335+
Value: [
336+
'On',
337+
'Off'
338+
],
339+
},
340+
},
341+
}],
342+
},
343+
},
344+
},
345+
},
346+
};
347+
348+
statusTree
349+
.addChild(new Tree('Audio'))
350+
.addChild(new Tree('Input'))
351+
.addChild(new Tree('Connectors'))
352+
.addChild(new ArrayTree('HDMI'))
353+
.addChild(new Member('Mute', new Literal('On', 'Off')));
354+
355+
expect(parse(schema)).toMatchObject({
356+
children: expect.arrayContaining([main, statusTree]),
357+
});
358+
});
321359
});
322360
});
323361
});

0 commit comments

Comments
 (0)