Skip to content

Commit

Permalink
schema: Add status arrays
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Martin Øinæs Myrseth committed Jan 18, 2020
1 parent af4642d commit 7517b85
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
List,
Type,
Generic,
ArrayTree,
} from './nodes';

export interface GenerateOpts {
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions src/schema/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions test/schema/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Literal,
List,
Generic,
ArrayTree,
} from '../../src/schema/nodes';

describe('schemas', () => {
Expand Down Expand Up @@ -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]),
});
});
});
});
});

0 comments on commit 7517b85

Please sign in to comment.