From 97c025d19bb5774217922fc8c976a7e2f8bb59bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 15 Apr 2016 01:49:01 +0200 Subject: [PATCH] Ease debugging by making the dump much easier to read for deeper object hierarchies. --- config-serializer/ClassArray.ts | 2 -- config-serializer/Serializable.ts | 38 ++++++++-------------------- config-serializer/UhkBuffer.ts | 27 ++++++++++++-------- config-serializer/test-serializer.ts | 3 +++ 4 files changed, 30 insertions(+), 40 deletions(-) diff --git a/config-serializer/ClassArray.ts b/config-serializer/ClassArray.ts index 73420141..af514ec8 100644 --- a/config-serializer/ClassArray.ts +++ b/config-serializer/ClassArray.ts @@ -13,7 +13,6 @@ abstract class ClassArray extends Serializable { let arrayLength = buffer.readCompactLength(); if (buffer.enableDump) { - process.stdout.write(']\n'); buffer.enableDump = false; } @@ -35,7 +34,6 @@ abstract class ClassArray extends Serializable { buffer.writeCompactLength(this.elements.length); if (buffer.enableDump) { - process.stdout.write(']\n'); buffer.enableDump = false; } diff --git a/config-serializer/Serializable.ts b/config-serializer/Serializable.ts index c7b0b50a..f108db94 100644 --- a/config-serializer/Serializable.ts +++ b/config-serializer/Serializable.ts @@ -5,58 +5,43 @@ abstract class Serializable { private static enableDump = true; fromJsObject(jsObject: any): Serializable { - let isArray = this instanceof ClassArray; - this.dumpMethodName('fromJsObject'); - this.dump(`${this.strintifyJsObject(jsObject)}` + (isArray ? '\n' : ` => `)); + this.dump(`${this.getIndentation()}${this.constructor.name}.fromJsObject: ` + + `${this.strintifyJsObject(jsObject)}\n`); Serializable.depth++; let value = this._fromJsObject(jsObject); Serializable.depth--; - if (!isArray) { - this.dump(`${value}\n`); - } + this.dump(`${this.getIndentation()}=> ${value}\n`); return value; } fromBinary(buffer: UhkBuffer): Serializable { - let isArray = this instanceof ClassArray; - this.dumpMethodName('fromBinary'); - this.dump('['); + this.dump(`\n${this.getIndentation()}${this.constructor.name}.fromBinary: [`); Serializable.depth++; buffer.enableDump = Serializable.enableDump; let value = this._fromBinary(buffer); buffer.enableDump = false; Serializable.depth--; - if (!isArray) { - this.dump(`] => ${value}\n`); - } + this.dump(`]\n${this.getIndentation()}=> ${value}`); return value; } toJsObject(): any { - let isArray = this instanceof ClassArray; - this.dumpMethodName('toJsObject'); - this.dump(`${this}` + (isArray ? '\n' : ` => `)); + this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`); Serializable.depth++; let value = this._toJsObject(); Serializable.depth--; - if (!isArray) { - this.dump(`${this.strintifyJsObject(value)}\n`); - } + this.dump(`${this.getIndentation()}=> ${this.strintifyJsObject(value)}\n`); return value; } toBinary(buffer: UhkBuffer): void { - let isArray = this instanceof ClassArray; - this.dumpMethodName('toBinary'); - this.dump(`${this} => ['`); + 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--; - if (!isArray) { - this.dump(`]\n`); - } + this.dump(`]`); return value; } @@ -71,9 +56,8 @@ abstract class Serializable { } } - private dumpMethodName(methodName: string) { - let indentation = new Array(Serializable.depth + 1).join(' '); - this.dump(`${indentation}${this.constructor.name}.${methodName}: `); + private getIndentation() { + return new Array(Serializable.depth + 1).join(' '); } private strintifyJsObject(jsObject: any): string { diff --git a/config-serializer/UhkBuffer.ts b/config-serializer/UhkBuffer.ts index d7e66db3..85f1204f 100644 --- a/config-serializer/UhkBuffer.ts +++ b/config-serializer/UhkBuffer.ts @@ -1,4 +1,5 @@ class UhkBuffer { + private static eepromSize = 32 * 1024; private static maxCompactLength = 0xFFFF; private static longCompactLengthPrefix = 0xFF; @@ -156,21 +157,25 @@ class UhkBuffer { } set enableDump(value) { - UhkBuffer.isFirstElementToDump = true; + if (value) { + UhkBuffer.isFirstElementToDump = true; + } this._enableDump = value; } dump(value) { - if (this.enableDump) { - if (!UhkBuffer.isFirstElementToDump) { - process.stdout.write(', '); - } - process.stdout.write(value); - if (UhkBuffer.isFirstElementToDump) { - UhkBuffer.isFirstElementToDump = false; - } - } else { - UhkBuffer.isFirstElementToDump = true; + if (!this.enableDump) { + return; + } + + if (!UhkBuffer.isFirstElementToDump) { + process.stdout.write(', '); + } + + process.stdout.write(value); + + if (UhkBuffer.isFirstElementToDump) { + UhkBuffer.isFirstElementToDump = false; } } } diff --git a/config-serializer/test-serializer.ts b/config-serializer/test-serializer.ts index f2cf8f69..1730d593 100644 --- a/config-serializer/test-serializer.ts +++ b/config-serializer/test-serializer.ts @@ -18,7 +18,9 @@ let keyActions1BufferContent = keyActions1Buffer.getBufferContent(); fs.writeFileSync('uhk-config.bin', keyActions1BufferContent); keyActions1Buffer.offset = 0; +console.log(); let keyActions2Ts = new KeyActions().fromBinary(keyActions1Buffer); +console.log('\n'); let keyActions2Js = keyActions2Ts.toJsObject(); let keyActions2Buffer = new UhkBuffer(); keyActions2Ts.toBinary(keyActions2Buffer); @@ -26,6 +28,7 @@ fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(keyActions2Js, und let keyActions2BufferContent = keyActions1Buffer.getBufferContent(); fs.writeFileSync('uhk-config-serialized.bin', keyActions2BufferContent); +console.log('\n'); try { assert.deepEqual(keyActions1Js, keyActions2Js); console.log('JSON configurations are identical.');