From f6eb819cfebb1168c94e873834201aca487a95ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 8 Apr 2016 22:26:00 +0200 Subject: [PATCH] Add assertion decorations for various numeric types. --- config-serializer/assert.ts | 35 +++++++++++++++++-- .../config-items/DualRoleKeystrokeAction.ts | 21 +++-------- .../config-items/KeystrokeAction.ts | 11 ++---- .../config-items/KeystrokeModifiersAction.ts | 1 + .../KeystrokeWithModifiersAction.ts | 12 ++----- config-serializer/config-items/MouseAction.ts | 22 ++---------- .../config-items/PlayMacroAction.ts | 11 ++---- .../config-items/SwitchKeymapAction.ts | 11 ++---- .../config-items/SwitchLayerAction.ts | 11 ------ 9 files changed, 48 insertions(+), 87 deletions(-) diff --git a/config-serializer/assert.ts b/config-serializer/assert.ts index 7015ba2d..0c813989 100644 --- a/config-serializer/assert.ts +++ b/config-serializer/assert.ts @@ -1,4 +1,32 @@ function assertUInt8(target: any, key: string) { + return assertInteger(target, key, 0, 255); +} + +function assertInt8(target: any, key: string) { + return assertInteger(target, key, -128, 127); +} + +function assertUInt16(target: any, key: string) { + return assertInteger(target, key, 0, 65535); +} + +function assertInt16(target: any, key: string) { + return assertInteger(target, key, -32768, 32767); +} + +function assertUInt32(target: any, key: string) { + return assertInteger(target, key, 0, 4294967295); +} + +function assertInt32(target: any, key: string) { + return assertInteger(target, key, -2147483648, 2147483647); +} + +function assertCompactLength(target: any, key: string) { + return assertUInt16(target, key) +} + +function assertInteger(target: any, key: string, min: number, max: number) { let val = this[key]; if (delete this[key]) { Object.defineProperty(target, key, { @@ -6,8 +34,9 @@ function assertUInt8(target: any, key: string) { return val; }, set: function (newVal) { - if (newVal < 0 || newVal > 255) { - throw `Invalid ${target.constructor.name}.${key}: ${newVal} is not uint8`; + if (newVal < min || newVal > max) { + throw `${target.constructor.name}.${key}: ` + + `Integer ${newVal} is outside the valid [${min}, ${max}] interval`; } val = newVal; }, @@ -27,7 +56,7 @@ function assertEnum(enumerated: E) { }, set: function (newVal) { if (enumerated[newVal] === undefined) { - throw `Invalid ${target.constructor.name}.${key}: ${newVal} is not enum`; + throw `${target.constructor.name}.${key}: ${newVal} is not enum`; } val = newVal; }, diff --git a/config-serializer/config-items/DualRoleKeystrokeAction.ts b/config-serializer/config-items/DualRoleKeystrokeAction.ts index b05e8d27..f0c24052 100644 --- a/config-serializer/config-items/DualRoleKeystrokeAction.ts +++ b/config-serializer/config-items/DualRoleKeystrokeAction.ts @@ -14,24 +14,11 @@ enum LongPressAction { class DualRoleKeystrokeAction extends KeyAction { - public scancode; + @assertUInt8 + scancode: number; - private _longPressAction: LongPressAction; - - get longPressAction(): number { - return this._longPressAction; - } - - set longPressAction(value) { - if (!this.isDualRoleKeystrokeActionValid(value)) { - throw `Invalid DualRoleKeystrokeAction.longPressAction: ${value}`; - } - this._longPressAction = value; - } - - isDualRoleKeystrokeActionValid(keyActionIdParam): boolean { - return LongPressAction[keyActionIdParam] !== undefined; - } + @assertEnum(LongPressAction) + private longPressAction: LongPressAction; _fromJsObject(jsObject: any): DualRoleKeystrokeAction { this.assertKeyActionType(jsObject, KeyActionType.DualRoleKeystrokeAction, 'DualRoleKeystrokeAction'); diff --git a/config-serializer/config-items/KeystrokeAction.ts b/config-serializer/config-items/KeystrokeAction.ts index 07f7e3d2..0afaac1f 100644 --- a/config-serializer/config-items/KeystrokeAction.ts +++ b/config-serializer/config-items/KeystrokeAction.ts @@ -1,14 +1,7 @@ class KeystrokeAction extends KeyAction { - private _scancode: number; - - get scancode(): number { - return this._scancode; - } - - set scancode(value) { - this._scancode = value; - } + @assertUInt8 + scancode: number; _fromJsObject(jsObject: any): KeystrokeAction { this.assertKeyActionType(jsObject, KeyActionType.KeystrokeAction, 'KeystrokeAction'); diff --git a/config-serializer/config-items/KeystrokeModifiersAction.ts b/config-serializer/config-items/KeystrokeModifiersAction.ts index 971cf5c6..0a580dc0 100644 --- a/config-serializer/config-items/KeystrokeModifiersAction.ts +++ b/config-serializer/config-items/KeystrokeModifiersAction.ts @@ -1,5 +1,6 @@ class KeystrokeModifiersAction extends KeyAction { + @assertUInt8 modifierMask: number; _fromJsObject(jsObject: any): KeystrokeModifiersAction { diff --git a/config-serializer/config-items/KeystrokeWithModifiersAction.ts b/config-serializer/config-items/KeystrokeWithModifiersAction.ts index 5156e773..0eb25a3c 100644 --- a/config-serializer/config-items/KeystrokeWithModifiersAction.ts +++ b/config-serializer/config-items/KeystrokeWithModifiersAction.ts @@ -1,16 +1,10 @@ class KeystrokeWithModifiersAction extends KeyAction { + @assertUInt8 modifierMask: number; - private _scancode: number; - - get scancode(): number { - return this._scancode; - } - - set scancode(value) { - this._scancode = value; - } + @assertUInt8 + scancode: number; _fromJsObject(jsObject: any): KeystrokeWithModifiersAction { this.assertKeyActionType( diff --git a/config-serializer/config-items/MouseAction.ts b/config-serializer/config-items/MouseAction.ts index b1400f4d..eedbe325 100644 --- a/config-serializer/config-items/MouseAction.ts +++ b/config-serializer/config-items/MouseAction.ts @@ -15,22 +15,9 @@ enum MouseActionParam { } class MouseAction extends KeyAction { - private _mouseAction: MouseActionParam; - get mouseAction(): MouseActionParam { - return this._mouseAction; - } - - set mouseAction(mouseAction) { -// if (!this.isMouseActionValid(mouseAction)) { -// throw `Invalid MouseAction.mouseAction: ${mouseAction}`; -// } - this._mouseAction = mouseAction; - } - -// isMouseActionValid(keyActionParam): boolean { -// return MouseActionParam[keyActionParam] !== undefined; -// } + @assertUInt8 + mouseAction: MouseActionParam; _fromJsObject(jsObject: any): MouseAction { this.assertKeyActionType(jsObject, KeyActionType.MouseAction, 'MouseAction'); @@ -40,12 +27,7 @@ class MouseAction extends KeyAction { _fromBinary(buffer: UhkBuffer): MouseAction { this.readAndAssertKeyActionId(buffer, KeyActionId.MouseAction, 'MouseAction'); - this.mouseAction = buffer.readUInt8(); - // if (!this.isMouseActionValid(this.mouseAction)) { - // throw `Invalid MouseAction.param: ${this.mouseAction}`; - // } - return this; } diff --git a/config-serializer/config-items/PlayMacroAction.ts b/config-serializer/config-items/PlayMacroAction.ts index 2670aa79..852dcadb 100644 --- a/config-serializer/config-items/PlayMacroAction.ts +++ b/config-serializer/config-items/PlayMacroAction.ts @@ -1,14 +1,7 @@ class PlayMacroAction extends KeyAction { - private _macroId: number; - - get macroId(): number { - return this._macroId; - } - - set macroId(value) { - this._macroId = value; - } + @assertUInt8 + macroId: number; _fromJsObject(jsObject: any): PlayMacroAction { this.assertKeyActionType(jsObject, KeyActionType.PlayMacroAction, 'PlayMacroAction'); diff --git a/config-serializer/config-items/SwitchKeymapAction.ts b/config-serializer/config-items/SwitchKeymapAction.ts index 4411aa37..f377caac 100644 --- a/config-serializer/config-items/SwitchKeymapAction.ts +++ b/config-serializer/config-items/SwitchKeymapAction.ts @@ -1,14 +1,7 @@ class SwitchKeymapAction extends KeyAction { - private _keymapId: number; - - get keymapId(): number { - return this._keymapId; - } - - set keymapId(value) { - this._keymapId = value; - } + @assertUInt8 + keymapId: number; _fromJsObject(jsObject: any): SwitchKeymapAction { this.assertKeyActionType(jsObject, KeyActionType.SwitchKeymapAction, 'SwitchKeymapAction'); diff --git a/config-serializer/config-items/SwitchLayerAction.ts b/config-serializer/config-items/SwitchLayerAction.ts index 89b92399..13e7ecac 100644 --- a/config-serializer/config-items/SwitchLayerAction.ts +++ b/config-serializer/config-items/SwitchLayerAction.ts @@ -12,18 +12,7 @@ class SwitchLayerAction extends KeyAction { @assertEnum(Layer) private layer: Layer; -/* - get layer(): number { - return this._layer; - } - set layer(value) { - if (!TypeChecker.isUInt8Valid(value)) { - throw 'Invalid SwitchLayerAction.layerId: ${value}'; - } - this._layer = value; - } -*/ getToggleFlag() { return this.isLayerToggleable ? SwitchLayerAction.toggleFlag : 0; }