Add assertion decorations for various numeric types.

This commit is contained in:
László Monda
2016-04-08 22:26:00 +02:00
parent 49d0ad270b
commit f6eb819cfe
9 changed files with 48 additions and 87 deletions

View File

@@ -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<E>(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;
},

View File

@@ -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');

View File

@@ -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');

View File

@@ -1,5 +1,6 @@
class KeystrokeModifiersAction extends KeyAction {
@assertUInt8
modifierMask: number;
_fromJsObject(jsObject: any): KeystrokeModifiersAction {

View File

@@ -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(

View File

@@ -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[<string>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;
}

View File

@@ -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');

View File

@@ -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');

View File

@@ -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;
}