Skip to content

Commit

Permalink
Add compiler support for device attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
gcampax committed Nov 1, 2019
1 parent 341a7aa commit 7759ad3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/compiler/ops-to-jsir.js
Expand Up @@ -48,10 +48,17 @@ module.exports = class OpCompiler {
ast.__effectiveSelector = ast.selector;
}

// TODO more attributes, and dynamic attributes (param-passing)
const attributes = {};
if (ast.__effectiveSelector.id)
attributes.id = ast.__effectiveSelector.id;
// NOTE: "all" has no effect on the compiler, it only affects the dialog agent
// whether it should slot-fill id or not
// (in the future, this should probably be represented as id=$? like everywhere else...)

for (let attr of ast.__effectiveSelector.attributes) {
// attr.value cannot be a parameter passing in a program, so it's safe to call toJS here
attributes[attr.name] = attr.value.toJS();
}
return [ast.__effectiveSelector.kind, attributes, ast.channel];
}

Expand Down
28 changes: 28 additions & 0 deletions test/test_compiler.js
Expand Up @@ -6032,6 +6032,34 @@ const TEST_CASES = [
} catch(_exc_) {
__env.reportError("Failed to invoke action", _exc_);
}`]],

//78 Test device selector
[`now => @light-bulb(name="bedroom").set_power(power=enum(on));`,
[`"use strict";
let _t_0;
let _t_1;
try {
_t_0 = {};
_t_1 = "on";
_t_0.power = _t_1;
await __env.invokeAction("light-bulb", { name: "bedroom", }, "set_power", _t_0);
} catch(_exc_) {
__env.reportError("Failed to invoke action", _exc_);
}`]],

//78 Test device selector (with explicit all)
[`now => @light-bulb(name="bedroom", all=true).set_power(power=enum(on));`,
[`"use strict";
let _t_0;
let _t_1;
try {
_t_0 = {};
_t_1 = "on";
_t_0.power = _t_1;
await __env.invokeAction("light-bulb", { name: "bedroom", }, "set_power", _t_0);
} catch(_exc_) {
__env.reportError("Failed to invoke action", _exc_);
}`]],
];

// eslint-disable-next-line prefer-arrow-callback
Expand Down

0 comments on commit 7759ad3

Please sign in to comment.