From 3cb91ad478b3a5a64835bc4517c7ef5685d00aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Thu, 31 Mar 2016 10:12:41 +0200 Subject: [PATCH] Add assert decorator functions. --- config-serializer/config-items/KeyAction.ts | 1 + .../config-items/SwitchKeymapAction.ts | 49 +++++++++++++++++-- config-serializer/serializeConfig.ts | 5 ++ config-serializer/tsconfig.json | 3 +- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/config-serializer/config-items/KeyAction.ts b/config-serializer/config-items/KeyAction.ts index df937ebf..01b7429d 100644 --- a/config-serializer/config-items/KeyAction.ts +++ b/config-serializer/config-items/KeyAction.ts @@ -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}`; } } diff --git a/config-serializer/config-items/SwitchKeymapAction.ts b/config-serializer/config-items/SwitchKeymapAction.ts index e1e3b1b2..66f2e632 100644 --- a/config-serializer/config-items/SwitchKeymapAction.ts +++ b/config-serializer/config-items/SwitchKeymapAction.ts @@ -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(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