Ease debugging by making the dump much easier to read for deeper object hierarchies.

This commit is contained in:
László Monda
2016-04-15 01:49:01 +02:00
parent 96c56c8081
commit 97c025d19b
4 changed files with 30 additions and 40 deletions

View File

@@ -13,7 +13,6 @@ abstract class ClassArray<T> extends Serializable<T> {
let arrayLength = buffer.readCompactLength();
if (buffer.enableDump) {
process.stdout.write(']\n');
buffer.enableDump = false;
}
@@ -35,7 +34,6 @@ abstract class ClassArray<T> extends Serializable<T> {
buffer.writeCompactLength(this.elements.length);
if (buffer.enableDump) {
process.stdout.write(']\n');
buffer.enableDump = false;
}

View File

@@ -5,58 +5,43 @@ abstract class Serializable<T> {
private static enableDump = true;
fromJsObject(jsObject: any): Serializable<T> {
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<T> {
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<T> {
}
}
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 {

View File

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

View File

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