Add assert decorator functions.

This commit is contained in:
László Monda
2016-03-31 10:12:41 +02:00
parent d2f3ec62f0
commit 3cb91ad478
4 changed files with 53 additions and 5 deletions
@@ -45,6 +45,7 @@ class KeyAction {
assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) { assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) {
if (jsObject.keyActionType !== keyActionTypeString) { if (jsObject.keyActionType !== keyActionTypeString) {
console.log(arguments.callee.prototype.name);
throw `Invalid ${classname}.keyActionType: ${jsObject.keyActionType}`; throw `Invalid ${classname}.keyActionType: ${jsObject.keyActionType}`;
} }
} }
@@ -1,3 +1,43 @@
function assertUInt8(target: any, key: string) {
let val = this[key];
if (delete this[key]) {
Object.defineProperty(target, key, {
get: function () {
return val;
},
set: function (newVal) {
if (newVal < 0 || newVal > 255) {
throw `Invalid ${target.constructor.name}.${key}: ${newVal} is not uint8`;
}
val = newVal;
},
enumerable: true,
configurable: true
});
}
}
function assertEnum<E>(enumerated: E) {
return function(target: any, key: string) {
let val = this[key];
if (delete this[key]) {
Object.defineProperty(target, key, {
get: function () {
return val;
},
set: function (newVal) {
if (enumerated[newVal] === undefined) {
throw `Invalid ${target.constructor.name}.${key}: ${newVal} is not enum`;
}
val = newVal;
},
enumerable: true,
configurable: true
});
}
}
}
enum Layer { enum Layer {
mod, mod,
fn, fn,
@@ -11,8 +51,9 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
isLayerToggleable: boolean; isLayerToggleable: boolean;
private _layer: Layer; @assertEnum(Layer)
private layer: Layer;
/*
get layer(): number { get layer(): number {
return this._layer; return this._layer;
} }
@@ -23,14 +64,14 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
} }
this._layer = value; this._layer = value;
} }
*/
getToggleFlag() { getToggleFlag() {
return this.isLayerToggleable ? SwitchLayerAction.toggleFlag : 0; return this.isLayerToggleable ? SwitchLayerAction.toggleFlag : 0;
} }
fromJsObject(jsObject: any): SwitchLayerAction { fromJsObject(jsObject: any): SwitchLayerAction {
this.assertKeyActionType(jsObject, SwitchLayerAction.keyActionTypeString, 'SwitchLayerAction'); this.assertKeyActionType(jsObject, SwitchLayerAction.keyActionTypeString, 'SwitchLayerAction');
this.layer = jsObject.keymapId; this.layer = jsObject.layerId;
this.isLayerToggleable = jsObject.toggle; this.isLayerToggleable = jsObject.toggle;
return this; return this;
} }
+5
View File
@@ -9,4 +9,9 @@ let writer = new UhkBuffer();
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json')); let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions; let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
new SwitchLayerAction().fromJsObject({
keyActionType: 'switchLayer',
layerId: 10022
});
fs.writeFileSync('uhk-config.bin', writer.buffer); fs.writeFileSync('uhk-config.bin', writer.buffer);
+2 -1
View File
@@ -1,7 +1,8 @@
{ {
"compilerOptions": { "compilerOptions": {
"out": "serializeConfig.js", "out": "serializeConfig.js",
"target": "es5" "target": "es5",
"experimentalDecorators": true
}, },
"files": [ "files": [
"typings/main.d.ts", "typings/main.d.ts",