Refactor: remove Serializable

This commit is contained in:
Farkas József
2016-12-31 11:16:09 +01:00
committed by József Farkas
parent 13d91ad26b
commit e40dbb83b0
23 changed files with 52 additions and 118 deletions
-51
View File
@@ -1,51 +0,0 @@
/// <references path="Function.d.ts">
import { UhkBuffer } from './UhkBuffer';
export abstract class Serializable<T> {
private static depth = 0;
private static maxDisplayedJsonLength = 160;
private static enableDump = false;
toJsonObject(): any {
this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`);
Serializable.depth++;
let value = this._toJsonObject();
Serializable.depth--;
this.dump(`${this.getIndentation()}=> ${this.stringifyJsonObject(value)}\n`);
return value;
}
// TODO: remove parameter and return the buffer
toBinary(buffer: UhkBuffer): void {
this.dump(`\n${this.getIndentation()}${this.constructor.name}.toBinary: ${this} [`);
Serializable.depth++;
buffer.enableDump = Serializable.enableDump;
let value = this._toBinary(buffer);
buffer.enableDump = false;
Serializable.depth--;
this.dump(`]`);
return value;
}
abstract _toJsonObject(): any;
abstract _toBinary(buffer: UhkBuffer): void;
private dump(value: any) {
if (Serializable.enableDump) {
process.stdout.write(value);
}
}
private getIndentation() {
return new Array(Serializable.depth + 1).join(' ');
}
private stringifyJsonObject(jsonObject: any): string {
let json = JSON.stringify(jsonObject);
return json.length > Serializable.maxDisplayedJsonLength
? json.substr(0, Serializable.maxDisplayedJsonLength) + '...'
: json;
}
}
+3 -5
View File
@@ -1,5 +1,3 @@
import { Serializable } from './Serializable';
export class UhkBuffer { export class UhkBuffer {
private static eepromSize = 32 * 1024; private static eepromSize = 32 * 1024;
@@ -153,7 +151,7 @@ export class UhkBuffer {
this.writeUInt8(bool ? 1 : 0); this.writeUInt8(bool ? 1 : 0);
} }
readArray<T extends Serializable<T>>(elementReader: (buffer: UhkBuffer) => T): T[] { readArray<T>(elementReader: (buffer: UhkBuffer) => T): T[] {
let array: T[] = []; let array: T[] = [];
let length = this.readCompactLength(); let length = this.readCompactLength();
for (let i = 0; i < length; ++i) { for (let i = 0; i < length; ++i) {
@@ -162,10 +160,10 @@ export class UhkBuffer {
return array; return array;
} }
writeArray<T extends Serializable<T>>(array: T[]): void { writeArray<T>(array: T[]): void {
this.writeCompactLength(array.length); this.writeCompactLength(array.length);
for (let element of array) { for (let element of array) {
element.toBinary(this); (<any>element).toBinary(this); // TODO: Remove any
} }
} }
+3 -5
View File
@@ -1,9 +1,8 @@
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Layer } from './Layer'; import { Layer } from './Layer';
import { Macro } from './Macro'; import { Macro } from './Macro';
export class Keymap extends Serializable<Keymap> { export class Keymap {
name: string; name: string;
@@ -16,7 +15,6 @@ export class Keymap extends Serializable<Keymap> {
layers: Layer[]; layers: Layer[];
constructor(keymap?: Keymap) { constructor(keymap?: Keymap) {
super();
if (!keymap) { if (!keymap) {
return; return;
} }
@@ -48,7 +46,7 @@ export class Keymap extends Serializable<Keymap> {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
isDefault: this.isDefault, isDefault: this.isDefault,
abbreviation: this.abbreviation, abbreviation: this.abbreviation,
@@ -58,7 +56,7 @@ export class Keymap extends Serializable<Keymap> {
}; };
} }
_toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
buffer.writeString(this.abbreviation); buffer.writeString(this.abbreviation);
buffer.writeBoolean(this.isDefault); buffer.writeBoolean(this.isDefault);
buffer.writeString(this.name); buffer.writeString(this.name);
+3 -5
View File
@@ -1,15 +1,13 @@
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Keymap } from './Keymap'; import { Keymap } from './Keymap';
import { Macro } from './Macro'; import { Macro } from './Macro';
import { Module } from './Module'; import { Module } from './Module';
export class Layer extends Serializable<Layer> { export class Layer {
modules: Module[]; modules: Module[];
constructor(layers?: Layer) { constructor(layers?: Layer) {
super();
if (!layers) { if (!layers) {
return; return;
} }
@@ -28,13 +26,13 @@ export class Layer extends Serializable<Layer> {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
modules: this.modules.map(module => module.toJsonObject()) modules: this.modules.map(module => module.toJsonObject())
}; };
} }
_toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
buffer.writeArray(this.modules); buffer.writeArray(this.modules);
} }
+3 -5
View File
@@ -1,9 +1,8 @@
import { assertUInt8 } from '../assert'; import { assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Helper as MacroActionHelper, MacroAction } from './macro-action'; import { Helper as MacroActionHelper, MacroAction } from './macro-action';
export class Macro extends Serializable<Macro> { export class Macro {
@assertUInt8 @assertUInt8
id: number; id: number;
@@ -17,7 +16,6 @@ export class Macro extends Serializable<Macro> {
macroActions: MacroAction[]; macroActions: MacroAction[];
constructor(other?: Macro) { constructor(other?: Macro) {
super();
if (!other) { if (!other) {
return; return;
} }
@@ -50,7 +48,7 @@ export class Macro extends Serializable<Macro> {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
id: this.id, id: this.id,
isLooped: this.isLooped, isLooped: this.isLooped,
@@ -60,7 +58,7 @@ export class Macro extends Serializable<Macro> {
}; };
} }
_toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id); buffer.writeUInt8(this.id);
buffer.writeBoolean(this.isLooped); buffer.writeBoolean(this.isLooped);
buffer.writeBoolean(this.isPrivate); buffer.writeBoolean(this.isPrivate);
+3 -5
View File
@@ -1,5 +1,4 @@
import { assertEnum, assertUInt8 } from '../assert'; import { assertEnum, assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Helper as KeyActionHelper, KeyAction, NoneAction } from './key-action'; import { Helper as KeyActionHelper, KeyAction, NoneAction } from './key-action';
import { Keymap } from './Keymap'; import { Keymap } from './Keymap';
@@ -11,7 +10,7 @@ enum PointerRole {
scroll scroll
} }
export class Module extends Serializable<Module> { export class Module {
@assertUInt8 @assertUInt8
id: number; id: number;
@@ -22,7 +21,6 @@ export class Module extends Serializable<Module> {
pointerRole: PointerRole; pointerRole: PointerRole;
constructor(other?: Module) { constructor(other?: Module) {
super();
if (!other) { if (!other) {
return; return;
} }
@@ -51,7 +49,7 @@ export class Module extends Serializable<Module> {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
id: this.id, id: this.id,
pointerRole: PointerRole[this.pointerRole], pointerRole: PointerRole[this.pointerRole],
@@ -63,7 +61,7 @@ export class Module extends Serializable<Module> {
}; };
} }
_toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id); buffer.writeUInt8(this.id);
buffer.writeUInt8(this.pointerRole); buffer.writeUInt8(this.pointerRole);
@@ -1,8 +1,7 @@
import { assertUInt8 } from '../assert'; import { assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
export class ModuleConfiguration extends Serializable<ModuleConfiguration> { export class ModuleConfiguration {
/* /*
* module id enumeration is a separate story * module id enumeration is a separate story
@@ -36,7 +35,7 @@ export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
id: this.id, id: this.id,
initialPointerSpeed: this.initialPointerSpeed, initialPointerSpeed: this.initialPointerSpeed,
@@ -45,7 +44,7 @@ export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
}; };
} }
_toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id); buffer.writeUInt8(this.id);
buffer.writeUInt8(this.initialPointerSpeed); buffer.writeUInt8(this.initialPointerSpeed);
buffer.writeUInt8(this.pointerAcceleration); buffer.writeUInt8(this.pointerAcceleration);
@@ -1,11 +1,10 @@
import { assertUInt16, assertUInt32, assertUInt8 } from '../assert'; import { assertUInt16, assertUInt32, assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Keymap } from './Keymap'; import { Keymap } from './Keymap';
import { Macro } from './Macro'; import { Macro } from './Macro';
import { ModuleConfiguration } from './ModuleConfiguration'; import { ModuleConfiguration } from './ModuleConfiguration';
export class UhkConfiguration extends Serializable<UhkConfiguration> { export class UhkConfiguration {
signature: string; signature: string;
@@ -60,7 +59,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
signature: this.signature, signature: this.signature,
dataModelVersion: this.dataModelVersion, dataModelVersion: this.dataModelVersion,
@@ -74,7 +73,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
}; };
} }
_toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
buffer.writeString(this.signature); buffer.writeString(this.signature);
buffer.writeUInt16(this.dataModelVersion); buffer.writeUInt16(this.dataModelVersion);
buffer.writeUInt32(this.prologue); buffer.writeUInt32(this.prologue);
@@ -1,6 +1,5 @@
/// <reference path="../../Function.d.ts" /> /// <reference path="../../Function.d.ts" />
import { Serializable } from '../../Serializable';
import { UhkBuffer } from '../../UhkBuffer'; import { UhkBuffer } from '../../UhkBuffer';
export enum KeyActionId { export enum KeyActionId {
@@ -29,7 +28,7 @@ export let keyActionType = {
PlayMacroAction : 'playMacro' PlayMacroAction : 'playMacro'
}; };
export abstract class KeyAction extends Serializable<KeyAction> { export abstract class KeyAction {
assertKeyActionType(jsObject: any): void { assertKeyActionType(jsObject: any): void {
let keyActionClassname: string = this.constructor.name; let keyActionClassname: string = this.constructor.name;
@@ -53,6 +52,6 @@ export abstract class KeyAction extends Serializable<KeyAction> {
return readKeyActionId; return readKeyActionId;
} }
abstract _toJsonObject(): any; abstract toJsonObject(): any;
abstract _toBinary(buffer: UhkBuffer): void; abstract toBinary(buffer: UhkBuffer): void;
} }
@@ -63,7 +63,7 @@ export class KeystrokeAction extends KeyAction {
return this; return this;
} }
_toJsonObject(): JsonObjectKeystrokeAction { toJsonObject(): JsonObjectKeystrokeAction {
let jsonObject: JsonObjectKeystrokeAction = { let jsonObject: JsonObjectKeystrokeAction = {
keyActionType: keyActionType.KeystrokeAction keyActionType: keyActionType.KeystrokeAction
}; };
@@ -83,7 +83,7 @@ export class KeystrokeAction extends KeyAction {
return jsonObject; return jsonObject;
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
let flags = 0; let flags = 0;
let bufferData: number[] = []; let bufferData: number[] = [];
@@ -43,14 +43,14 @@ export class MouseAction extends KeyAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
keyActionType: keyActionType.MouseAction, keyActionType: keyActionType.MouseAction,
mouseAction: MouseActionParam[this.mouseAction] mouseAction: MouseActionParam[this.mouseAction]
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.MouseAction); buffer.writeUInt8(KeyActionId.MouseAction);
buffer.writeUInt8(this.mouseAction); buffer.writeUInt8(this.mouseAction);
} }
@@ -19,13 +19,13 @@ export class NoneAction extends KeyAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
keyActionType: keyActionType.NoneAction keyActionType: keyActionType.NoneAction
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.NoneAction); buffer.writeUInt8(KeyActionId.NoneAction);
} }
@@ -32,14 +32,14 @@ export class PlayMacroAction extends KeyAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
keyActionType: keyActionType.PlayMacroAction, keyActionType: keyActionType.PlayMacroAction,
macroId: this.macroId macroId: this.macroId
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.PlayMacroAction); buffer.writeUInt8(KeyActionId.PlayMacroAction);
buffer.writeUInt8(this.macroId); buffer.writeUInt8(this.macroId);
} }
@@ -30,14 +30,14 @@ export class SwitchKeymapAction extends KeyAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
keyActionType: keyActionType.SwitchKeymapAction, keyActionType: keyActionType.SwitchKeymapAction,
keymapAbbreviation: this.keymapAbbreviation keymapAbbreviation: this.keymapAbbreviation
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.SwitchKeymapAction); buffer.writeUInt8(KeyActionId.SwitchKeymapAction);
buffer.writeString(this.keymapAbbreviation); buffer.writeString(this.keymapAbbreviation);
} }
@@ -38,7 +38,7 @@ export class SwitchLayerAction extends KeyAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
keyActionType: keyActionType.SwitchLayerAction, keyActionType: keyActionType.SwitchLayerAction,
layer: LayerName[this.layer], layer: LayerName[this.layer],
@@ -46,7 +46,7 @@ export class SwitchLayerAction extends KeyAction {
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.SwitchLayerAction); buffer.writeUInt8(KeyActionId.SwitchLayerAction);
buffer.writeUInt8(this.layer); buffer.writeUInt8(this.layer);
buffer.writeBoolean(this.isLayerToggleable); buffer.writeBoolean(this.isLayerToggleable);
@@ -27,14 +27,14 @@ export class DelayMacroAction extends MacroAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
macroActionType: macroActionType.DelayMacroAction, macroActionType: macroActionType.DelayMacroAction,
delay: this.delay delay: this.delay
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.DelayMacroAction); buffer.writeUInt8(MacroActionId.DelayMacroAction);
buffer.writeUInt16(this.delay); buffer.writeUInt16(this.delay);
} }
@@ -55,7 +55,7 @@ export class KeyMacroAction extends MacroAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
let jsObject: JsObjectKeyMacroAction = { let jsObject: JsObjectKeyMacroAction = {
macroActionType: macroActionType.KeyMacroAction, macroActionType: macroActionType.KeyMacroAction,
action: MacroSubAction[this.action] action: MacroSubAction[this.action]
@@ -72,7 +72,7 @@ export class KeyMacroAction extends MacroAction {
return jsObject; return jsObject;
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
let keyMacroType: number = MacroActionId.KeyMacroAction; let keyMacroType: number = MacroActionId.KeyMacroAction;
keyMacroType += NUM_OF_COMBINATIONS * this.action; keyMacroType += NUM_OF_COMBINATIONS * this.action;
@@ -1,4 +1,3 @@
import { Serializable } from '../../Serializable';
import { UhkBuffer } from '../../UhkBuffer'; import { UhkBuffer } from '../../UhkBuffer';
export enum MacroActionId { export enum MacroActionId {
@@ -45,7 +44,7 @@ export let macroActionType = {
TextMacroAction : 'text' TextMacroAction : 'text'
}; };
export abstract class MacroAction extends Serializable<MacroAction> { export abstract class MacroAction {
assertMacroActionType(jsObject: any) { assertMacroActionType(jsObject: any) {
let macroActionClassname = this.constructor.name; let macroActionClassname = this.constructor.name;
let macroActionTypeString = macroActionType[macroActionClassname]; let macroActionTypeString = macroActionType[macroActionClassname];
@@ -73,6 +72,6 @@ export abstract class MacroAction extends Serializable<MacroAction> {
return readMacroActionId; return readMacroActionId;
} }
abstract _toJsonObject(): any; abstract toJsonObject(): any;
abstract _toBinary(buffer: UhkBuffer): void; abstract toBinary(buffer: UhkBuffer): void;
} }
@@ -44,7 +44,7 @@ export class MouseButtonMacroAction extends MacroAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
macroActionType: macroActionType.MouseButtonMacroAction, macroActionType: macroActionType.MouseButtonMacroAction,
action: MacroSubAction[this.action], action: MacroSubAction[this.action],
@@ -52,7 +52,7 @@ export class MouseButtonMacroAction extends MacroAction {
}; };
} }
_toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(MacroActionId.MouseButtonMacroAction + this.action); buffer.writeUInt8(MacroActionId.MouseButtonMacroAction + this.action);
buffer.writeUInt8(this.mouseButtonsMask); buffer.writeUInt8(this.mouseButtonsMask);
} }
@@ -33,7 +33,7 @@ export class MoveMouseMacroAction extends MacroAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
macroActionType: macroActionType.MoveMouseMacroAction, macroActionType: macroActionType.MoveMouseMacroAction,
x: this.x, x: this.x,
@@ -41,7 +41,7 @@ export class MoveMouseMacroAction extends MacroAction {
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.MoveMouseMacroAction); buffer.writeUInt8(MacroActionId.MoveMouseMacroAction);
buffer.writeInt16(this.x); buffer.writeInt16(this.x);
buffer.writeInt16(this.y); buffer.writeInt16(this.y);
@@ -33,7 +33,7 @@ export class ScrollMouseMacroAction extends MacroAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
macroActionType: macroActionType.ScrollMouseMacroAction, macroActionType: macroActionType.ScrollMouseMacroAction,
x: this.x, x: this.x,
@@ -41,7 +41,7 @@ export class ScrollMouseMacroAction extends MacroAction {
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.ScrollMouseMacroAction); buffer.writeUInt8(MacroActionId.ScrollMouseMacroAction);
buffer.writeInt16(this.x); buffer.writeInt16(this.x);
buffer.writeInt16(this.y); buffer.writeInt16(this.y);
@@ -25,14 +25,14 @@ export class TextMacroAction extends MacroAction {
return this; return this;
} }
_toJsonObject(): any { toJsonObject(): any {
return { return {
macroActionType: macroActionType.TextMacroAction, macroActionType: macroActionType.TextMacroAction,
text: this.text text: this.text
}; };
} }
_toBinary(buffer: UhkBuffer) { toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.TextMacroAction); buffer.writeUInt8(MacroActionId.TextMacroAction);
buffer.writeString(this.text); buffer.writeString(this.text);
} }
+1 -2
View File
@@ -1,5 +1,4 @@
import { UhkConfiguration } from '../src/config-serializer/config-items/UhkConfiguration'; import { UhkConfiguration } from '../src/config-serializer/config-items/UhkConfiguration';
import { Serializable } from '../src/config-serializer/Serializable';
import { UhkBuffer } from '../src/config-serializer/UhkBuffer'; import { UhkBuffer } from '../src/config-serializer/UhkBuffer';
let assert = require('assert'); let assert = require('assert');
@@ -8,7 +7,7 @@ let fs = require('fs');
let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json')); let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json'));
let config1Js = uhkConfig; let config1Js = uhkConfig;
let config1Ts: Serializable<UhkConfiguration> = new UhkConfiguration().fromJsonObject(config1Js); let config1Ts: UhkConfiguration = new UhkConfiguration().fromJsonObject(config1Js);
let config1Buffer = new UhkBuffer(); let config1Buffer = new UhkBuffer();
config1Ts.toBinary(config1Buffer); config1Ts.toBinary(config1Buffer);
let config1BufferContent = config1Buffer.getBufferContent(); let config1BufferContent = config1Buffer.getBufferContent();