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

View File

@@ -45,6 +45,7 @@ class KeyAction {
assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) {
if (jsObject.keyActionType !== keyActionTypeString) {
console.log(arguments.callee.prototype.name);
throw `Invalid ${classname}.keyActionType: ${jsObject.keyActionType}`;
}
}

View File

@@ -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 {
mod,
fn,
@@ -11,8 +51,9 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
isLayerToggleable: boolean;
private _layer: Layer;
@assertEnum(Layer)
private layer: Layer;
/*
get layer(): number {
return this._layer;
}
@@ -23,14 +64,14 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
}
this._layer = value;
}
*/
getToggleFlag() {
return this.isLayerToggleable ? SwitchLayerAction.toggleFlag : 0;
}
fromJsObject(jsObject: any): SwitchLayerAction {
this.assertKeyActionType(jsObject, SwitchLayerAction.keyActionTypeString, 'SwitchLayerAction');
this.layer = jsObject.keymapId;
this.layer = jsObject.layerId;
this.isLayerToggleable = jsObject.toggle;
return this;
}

View File

@@ -9,4 +9,9 @@ let writer = new UhkBuffer();
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
new SwitchLayerAction().fromJsObject({
keyActionType: 'switchLayer',
layerId: 10022
});
fs.writeFileSync('uhk-config.bin', writer.buffer);

View File

@@ -1,7 +1,8 @@
{
"compilerOptions": {
"out": "serializeConfig.js",
"target": "es5"
"target": "es5",
"experimentalDecorators": true
},
"files": [
"typings/main.d.ts",