Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of export to JSON function #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 18 additions & 51 deletions src/v1/ExportToJson.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,26 @@
import { workerMethods } from "./app.worker";
import {workerMethods} from "./app.worker";
import {mapToGenericObject} from "./utils/ExportedValueMappers";

export function exportToJson(useHex: boolean = false) {
var indentLen = 2;
var result = "";

function expToNative(value: IExportedValue, padLvl: number = 0) {
var pad = " ".repeat((padLvl + 0) * indentLen);
var childPad = " ".repeat((padLvl + 1) * indentLen);

var isArray = value.type === ObjectType.Array;
const overrideDefaultNumbersWithHex = (key: string, value: any) => {
const isPropertyNumber = typeof value === 'number'
return isPropertyNumber
? '0x' + value.toString(16)
: value;
}

if (value.type === ObjectType.Object || isArray) {
result += isArray ? "[" : "{";
const getResultOrThrowError = (response: IWorkerResponse) => {
if (response.error) throw response.error;
return response.result;
}

var keys: any[] = isArray ? value.arrayItems : Object.keys(value.object.fields);
if (keys.length > 0) {
result += `\n${childPad}`;
let nextItemPrefix = "";
keys.forEach((arrItem, i) => {
const propValue: IExportedValue = isArray ? arrItem : value.object.fields[arrItem];
if (propValue.type === ObjectType.Undefined) return true;
result += nextItemPrefix + (isArray ? "" : `"${arrItem}": `);
expToNative(propValue, padLvl + 1);
var lineCont = isArray && arrItem.type === ObjectType.Primitive && typeof arrItem.primitiveValue !== "string" && i % 16 !== 15;
var last = i === keys.length - 1;
nextItemPrefix = "," + (lineCont ? " " : `\n${childPad}`);
});
result += `\n${pad}`;
}
result += isArray ? "]" : "}";
} else if (value.type === ObjectType.TypedArray) {
if (value.bytes.length <= 64)
result += "[" + Array.from(value.bytes).join(", ") + "]";
else
result += `{ "$start": ${value.ioOffset + value.start}, "$end": ${value.ioOffset + value.end - 1} }`;
} else if (value.type === ObjectType.Primitive) {
if (value.enumStringValue)
result += `{ "name": ${JSON.stringify(value.enumStringValue)}, "value": ${value.primitiveValue} }`;
else if (typeof value.primitiveValue === "number")
result += useHex ? `0x${value.primitiveValue.toString(16)}` : `${value.primitiveValue}`;
else
result += `${JSON.stringify(value.primitiveValue)}`;
}
const mapResultToJson = (objectToExport: IExportedValue) => {
const genericObject = mapToGenericObject(objectToExport);
const hexReplacer = useHex ? overrideDefaultNumbersWithHex : null;
return JSON.stringify(genericObject, hexReplacer, 2);
}

return workerMethods.reparse(true)
.then(response => {
if (response.error) {
throw response.error;
}
return response.result;
})
.then(exportedRoot => {
console.log("exported", exportedRoot);
expToNative(exportedRoot);
return result;
});
.then(getResultOrThrowError)
.then(mapResultToJson);
}
5 changes: 5 additions & 0 deletions src/v1/utils/ExportedValueMappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {ExportedValueToGenericObjectMapper} from "./ExportedValueMappers/ExportedValueToGenericObjectMapper";

export const mapToGenericObject = (objectToMap: IExportedValue): any => {
return new ExportedValueToGenericObjectMapper().map(objectToMap);
}
46 changes: 46 additions & 0 deletions src/v1/utils/ExportedValueMappers/AbstractExportedValueMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {IExportedValueMapper} from "./IExportedValueMapper";

export class AbstractExportedValueMapper<T> implements IExportedValueMapper<T> {
public map(value: IExportedValue): T {
switch (value.type) {
case ObjectType.Primitive:
const isEnumValue = !!value.enumStringValue;
return isEnumValue
? this.visitEnum(value)
: this.visitPrimitive(value)
case ObjectType.Array:
return this.visitArray(value);
case ObjectType.TypedArray:
return this.visitTypedArray(value);
case ObjectType.Object:
return this.visitObject(value);
case ObjectType.Undefined:
return this.visitUndefined(value);
}
}

protected visitPrimitive(value: IExportedValue): T {
return null;
}

protected visitArray(value: IExportedValue): T {
return null;
}

protected visitTypedArray(value: IExportedValue): T {
return null;
}

protected visitObject(value: IExportedValue): T {
return null;
}

protected visitEnum(value: IExportedValue): T {
return null;
}

protected visitUndefined(value: IExportedValue): T {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {AbstractExportedValueMapper} from "./AbstractExportedValueMapper";

export class ExportedValueToGenericObjectMapper extends AbstractExportedValueMapper<any> {

protected visitPrimitive(value: IExportedValue) {
super.visitPrimitive(value);
return value.primitiveValue;
}

protected visitArray(value: IExportedValue) {
super.visitArray(value);
return ([...value.arrayItems] || []).map((item) => this.map(item));
}

protected visitTypedArray(value: IExportedValue) {
super.visitTypedArray(value);
return [...value.bytes] || [];
}

protected visitObject(value: IExportedValue) {
super.visitObject(value);
const newObject = {};
Object.keys(value.object.fields || {}).forEach(key => {
newObject[key] = this.map(value.object.fields[key])
})
return newObject;
}

protected visitEnum(value: IExportedValue) {
super.visitEnum(value);
return {
name: value.enumStringValue,
value: value.primitiveValue
}
}

protected visitUndefined(value: IExportedValue): any {
return undefined;
}

}
3 changes: 3 additions & 0 deletions src/v1/utils/ExportedValueMappers/IExportedValueMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IExportedValueMapper<T> {
map(value: IExportedValue): T;
}