From 92828705d39b4f099b625feafb7ff260439990f2 Mon Sep 17 00:00:00 2001 From: barsdeveloper Date: Mon, 25 Oct 2021 15:49:09 +0200 Subject: [PATCH] Entities cleanup, Primitive concept introduced --- dist/ueblueprint.js | 230 +++++++++++---------- js/Utility.js | 18 +- js/entity/Entity.js | 17 +- js/entity/FunctionReferenceEntity.js | 4 +- js/entity/GuidEntity.js | 37 ---- js/entity/Integer.js | 24 --- js/entity/LocalizedTextEntity.js | 15 -- js/entity/ObjectEntity.js | 19 +- js/entity/ObjectReferenceEntity.js | 21 -- js/entity/Path.js | 3 - js/entity/PinEntity.js | 22 +- js/entity/PinReferenceEntity.js | 4 +- js/entity/TypeInitialization.js | 12 +- js/entity/VariableReferenceEntity.js | 6 +- js/entity/primitive/Guid.js | 32 +++ js/entity/primitive/Integer.js | 25 +++ js/entity/primitive/LocalizedTextEntity.js | 22 ++ js/entity/primitive/ObjectReference.js | 23 +++ js/entity/primitive/Primitive.js | 5 + js/serialization/Grammar.js | 29 +-- js/serialization/Serializer.js | 8 +- 21 files changed, 292 insertions(+), 284 deletions(-) delete mode 100644 js/entity/GuidEntity.js delete mode 100644 js/entity/Integer.js delete mode 100644 js/entity/LocalizedTextEntity.js delete mode 100644 js/entity/ObjectReferenceEntity.js delete mode 100644 js/entity/Path.js create mode 100644 js/entity/primitive/Guid.js create mode 100644 js/entity/primitive/Integer.js create mode 100644 js/entity/primitive/LocalizedTextEntity.js create mode 100644 js/entity/primitive/ObjectReference.js create mode 100644 js/entity/primitive/Primitive.js diff --git a/dist/ueblueprint.js b/dist/ueblueprint.js index f765f63..2c68436 100644 --- a/dist/ueblueprint.js +++ b/dist/ueblueprint.js @@ -1,3 +1,33 @@ +class Primitive { + toString() { + return "Unimplemented for " + this.constructor.name + } +} + +class Integer extends Primitive { + + constructor(value) { + super(); + // Using constructor equality and not instanceof in order to consider both primitives and objects + if (value?.constructor === String) { + value = Number(value); + } + if (value?.constructor === Number) { + value = Math.round(value); + } + /** @type {number} */ + this.value = value; + } + + valueOf() { + this.value; + } + + toString() { + return this.value.toString() + } +} + class Utility { static clamp(val, min, max) { return Math.min(Math.max(val, min), max) @@ -55,18 +85,15 @@ class Utility { static sanitize(value) { if (!(value instanceof Object)) { - return value + return value // Is already primitive } - switch (value?.constructor) { - case Boolean: - return value.valueOf() - case Number: - return value.valueOf() - case String: - return value.toString() - default: - return value + if (value instanceof Boolean || value instanceof Integer || value instanceof Number) { + return value.valueOf() } + if (value instanceof String) { + return value.toString() + } + return value } static equals(a, b) { @@ -97,7 +124,17 @@ class Utility { } class TypeInitialization { - constructor(value, showDefault = true, type = Utility.getType(value)) { + + /** + * + * @param {typeof Object} type + * @param {boolean} showDefault + * @param {*} value + */ + constructor(type, showDefault = true, value = undefined) { + if (value === undefined) { + value = Utility.sanitize(new type()); + } this.value = value; this.showDefault = showDefault; this.type = type; @@ -112,14 +149,14 @@ class Entity { * @param {Object} target * @param {Object} properties */ - const defineAllAttributes = (prefix, target, properties, propertySetter = (t, p, v) => t[p] = v) => { + const defineAllAttributes = (prefix, target, properties) => { let fullKey = prefix.concat(""); const last = fullKey.length - 1; for (let property in properties) { fullKey[last] = property; - // Not instanceof because all objects are instenceof Object + // Not instanceof because all objects are instenceof Object, exact match needed if (properties[property]?.constructor === Object) { - propertySetter(target, property, {}); + target[property] = {}; defineAllAttributes(fullKey, target[property], properties[property]); continue } @@ -132,7 +169,7 @@ class Entity { */ const value = Utility.objectGet(options, fullKey); if (value !== null) { - propertySetter(target, property, value); + target[property] = value; continue } let defaultValue = properties[property]; @@ -143,29 +180,20 @@ class Entity { defaultValue = defaultValue.value; } if (defaultValue instanceof Array) { - propertySetter(target, property, []); - defineAllAttributes( - fullKey, - target[property], - defaultValue, - (t, _, v) => t.push(v)); + target[property] = []; continue } if (defaultValue instanceof Function) { defaultValue = Utility.sanitize(new defaultValue()); } - propertySetter(target, property, defaultValue); + target[property] = defaultValue; } }; defineAllAttributes([], this, this.getAttributes()); } } -class GuidEntity extends Entity { - - static attributes = { - value: String - } +class Guid extends Primitive { static generateGuid(random) { let values = new Uint32Array(4); @@ -180,46 +208,54 @@ class GuidEntity extends Entity { } constructor(guid) { - if (guid?.constructor === String) { - guid = { - value: guid - }; - } else if (guid?.constructor === Boolean) { - guid = { - value: GuidEntity.generateGuid(guid == true) - }; + super(); + // Using constructor equality and not instanceof in order to consider both primitives and objects + if (guid?.constructor === Boolean) { + guid = Guid.generateGuid(guid == true); } - super(guid); + if (guid instanceof Guid) { + guid = guid.value; + } + this.value = guid; } - getAttributes() { - return GuidEntity.attributes + toString() { + return this.value.toString() } } -class LocalizedTextEntity extends Entity { +class LocalizedTextEntity extends Primitive { - static attributes = { - namespace: "", - key: "", - value: "" + /** + * + * @param {String} namespace + * @param {String} key + * @param {String} value + */ + constructor(namespace, key, value) { + super(); + this.namespace = namespace; + this.key = key; + this.value = value; } - getAttributes() { - return LocalizedTextEntity.attributes + toString() { + "NSLOCTEXT(" + `"${this.namespace}"` + ", " + `"${this.key}"` + ", " + `"${this.value}"` + ")"; } } -class ObjectReferenceEntity extends Entity { +class ObjectReference extends Primitive { - static attributes = { - type: "", - path: "" - } - - getAttributes() { - return ObjectReferenceEntity.attributes + /** + * + * @param {String} type + * @param {String} path + */ + constructor(type, path) { + super(); + this.type = type; + this.path = path; } toString() { @@ -235,7 +271,7 @@ class PinReferenceEntity extends Entity { static attributes = { objectName: String, - pinGuid: GuidEntity + pinGuid: Guid } getAttributes() { @@ -246,27 +282,27 @@ class PinReferenceEntity extends Entity { class PinEntity$1 extends Entity { static attributes = { - PinId: GuidEntity, + PinId: Guid, PinName: "", - PinFriendlyName: new TypeInitialization(new LocalizedTextEntity(), false), + PinFriendlyName: new TypeInitialization(LocalizedTextEntity, false, null), PinToolTip: "", - Direction: new TypeInitialization("", false), + Direction: new TypeInitialization(String, false, ""), PinType: { PinCategory: "", PinSubCategory: "", - PinSubCategoryObject: ObjectReferenceEntity, + PinSubCategoryObject: ObjectReference, PinSubCategoryMemberReference: null, PinValueType: null, - ContainerType: ObjectReferenceEntity, + ContainerType: ObjectReference, bIsReference: false, bIsConst: false, bIsWeakPointer: false, bIsUObjectWrapper: false }, - LinkedTo: [new TypeInitialization(null, false, PinReferenceEntity)], + LinkedTo: [new TypeInitialization(PinReferenceEntity, false, null)], DefaultValue: "", AutogeneratedDefaultValue: "", - PersistentGuid: GuidEntity, + PersistentGuid: Guid, bHidden: false, bNotConnectable: false, bDefaultValueIsReadOnly: false, @@ -288,7 +324,7 @@ class PinEntity$1 extends Entity { class FunctionReferenceEntity extends Entity { static attributes = { - MemberParent: ObjectReferenceEntity, + MemberParent: ObjectReference, MemberName: "" } @@ -297,29 +333,6 @@ class FunctionReferenceEntity extends Entity { } } -class Integer extends Entity { - - static attributes = { - value: 0 - } - - constructor(value) { - if (value?.constructor === String) { - value = Number(value); - } - if (value?.constructor === Number) { - value = { - value: Math.round(value.valueOf()) - }; - } - super(value); - } - - getAttributes() { - return Integer.attributes - } -} - var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function getDefaultExportFromCjs (x) { @@ -338,8 +351,8 @@ class VariableReferenceEntity extends Entity { static attributes = { MemberName: String, - MemberGuid: GuidEntity, - bSelfContext: true + MemberGuid: Guid, + bSelfContext: false } getAttributes() { @@ -350,16 +363,16 @@ class VariableReferenceEntity extends Entity { class ObjectEntity extends Entity { static attributes = { - Class: ObjectReferenceEntity, + Class: ObjectReference, Name: "", - bIsPureFunc: new TypeInitialization(false, false), - VariableReference: new TypeInitialization(null, false, VariableReferenceEntity), - FunctionReference: new TypeInitialization(null, false, FunctionReferenceEntity), - TargetType: new TypeInitialization(null, false, ObjectReferenceEntity), - NodePosX: Integer, - NodePosY: Integer, - NodeGuid: GuidEntity, - CustomProperties: [new TypeInitialization(null, false, PinEntity$1)] + bIsPureFunc: new TypeInitialization(Boolean, false, false), + VariableReference: new TypeInitialization(VariableReferenceEntity, false, null), + FunctionReference: new TypeInitialization(FunctionReferenceEntity, false, null,), + TargetType: new TypeInitialization(ObjectReference, false, null), + NodePosX: 0, + NodePosY: 0, + NodeGuid: Guid, + CustomProperties: [PinEntity$1] } getAttributes() { @@ -375,13 +388,13 @@ class Grammar { InlineOptWhitespace = _ => P.regex(/[^\S\n]*/).desc("inline optional whitespace") WhitespaceNewline = _ => P.regex(/[^\S\n]*\n\s*/).desc("whitespace with at least a newline") Null = r => P.seq(P.string("("), r.InlineOptWhitespace, P.string(")")).map(_ => null).desc("null: ()") - None = _ => P.string("None").map(_ => new ObjectReferenceEntity({ type: "None" })).desc("none") + None = _ => P.string("None").map(_ => new ObjectReference({ type: "None" })).desc("none") Boolean = _ => P.alt(P.string("True"), P.string("False")).map(v => v === "True" ? true : false).desc("either True or False") Number = _ => P.regex(/[0-9]+(?:\.[0-9]+)?/).map(Number).desc("a number") Integer = _ => P.regex(/[0-9]+/).map(v => new Integer(v)).desc("an integer") String = _ => P.regex(/(?:[^"\\]|\\")*/).wrap(P.string('"'), P.string('"')).desc('string (with possibility to escape the quote using \")') Word = _ => P.regex(/[a-zA-Z]+/).desc("a word") - Guid = _ => P.regex(/[0-9a-zA-Z]{32}/).map(v => new GuidEntity({ value: v })).desc("32 digit hexadecimal (accepts all the letters for safety) value") + Guid = _ => P.regex(/[0-9a-zA-Z]{32}/).map(v => new Guid(v)).desc("32 digit hexadecimal (accepts all the letters for safety) value") PathSymbol = _ => P.regex(/[0-9a-zA-Z_]+/) ReferencePath = r => P.seq(P.string("/"), r.PathSymbol.sepBy1(P.string(".")).tieWith(".")) .tie() @@ -390,7 +403,7 @@ class Grammar { .desc('a path (words with possibly underscore, separated by ".", separated by "/")') Reference = r => P.alt( r.None, - r.ReferencePath.map(path => new ObjectReferenceEntity({ path: path })), + r.ReferencePath.map(path => new ObjectReference("", path)), P.seqMap( r.Word, P.optWhitespace, @@ -399,10 +412,7 @@ class Grammar { P.string(result.split("").reverse().join("")) ) ), - (referenceType, _, referencePath) => new ObjectReferenceEntity({ - type: referenceType, - path: referencePath - }) + (referenceType, _, referencePath) => new ObjectReference(referenceType, referencePath) ) ) AttributeName = r => r.Word.sepBy1(P.string(".")).tieWith(".").desc('words separated by ""') @@ -415,11 +425,7 @@ class Grammar { P.string(","), r.String.trim(P.optWhitespace), // value P.string(")"), - (_, namespace, __, key, ___, value, ____) => new LocalizedTextEntity({ - namespace: namespace, - key: key, - value: value - }) + (_, namespace, __, key, ___, value, ____) => new LocalizedTextEntity(namespace, key, value) ) PinReference = r => P.seqMap( r.PathSymbol, @@ -440,9 +446,9 @@ class Grammar { return r.Integer case String: return r.String - case GuidEntity: + case Guid: return r.Guid - case ObjectReferenceEntity: + case ObjectReference: return r.Reference case LocalizedTextEntity: return r.LocalizedText @@ -554,8 +560,8 @@ class Serializer { return this.writeValue(value()) case Boolean: return Utility.FirstCapital(value.toString()) - case ObjectReferenceEntity: - case GuidEntity: + case ObjectReference: + case Guid: return value.toString() case String: return `"${value}"` diff --git a/js/Utility.js b/js/Utility.js index f4b7003..938b4cc 100644 --- a/js/Utility.js +++ b/js/Utility.js @@ -1,3 +1,4 @@ +import Integer from "./entity/primitive/Integer" import TypeInitialization from "./entity/TypeInitialization" export default class Utility { @@ -57,18 +58,15 @@ export default class Utility { static sanitize(value) { if (!(value instanceof Object)) { - return value + return value // Is already primitive } - switch (value?.constructor) { - case Boolean: - return value.valueOf() - case Number: - return value.valueOf() - case String: - return value.toString() - default: - return value + if (value instanceof Boolean || value instanceof Integer || value instanceof Number) { + return value.valueOf() } + if (value instanceof String) { + return value.toString() + } + return value } static equals(a, b) { diff --git a/js/entity/Entity.js b/js/entity/Entity.js index 346c8d4..98aa909 100644 --- a/js/entity/Entity.js +++ b/js/entity/Entity.js @@ -9,14 +9,14 @@ export default class Entity { * @param {Object} target * @param {Object} properties */ - const defineAllAttributes = (prefix, target, properties, propertySetter = (t, p, v) => t[p] = v) => { + const defineAllAttributes = (prefix, target, properties) => { let fullKey = prefix.concat("") const last = fullKey.length - 1 for (let property in properties) { fullKey[last] = property - // Not instanceof because all objects are instenceof Object + // Not instanceof because all objects are instenceof Object, exact match needed if (properties[property]?.constructor === Object) { - propertySetter(target, property, {}) + target[property] = {} defineAllAttributes(fullKey, target[property], properties[property]) continue } @@ -29,7 +29,7 @@ export default class Entity { */ const value = Utility.objectGet(options, fullKey) if (value !== null) { - propertySetter(target, property, value) + target[property] = value continue } let defaultValue = properties[property] @@ -40,18 +40,13 @@ export default class Entity { defaultValue = defaultValue.value } if (defaultValue instanceof Array) { - propertySetter(target, property, []) - defineAllAttributes( - fullKey, - target[property], - defaultValue, - (t, _, v) => t.push(v)) + target[property] = [] continue } if (defaultValue instanceof Function) { defaultValue = Utility.sanitize(new defaultValue()) } - propertySetter(target, property, defaultValue) + target[property] = defaultValue } } defineAllAttributes([], this, this.getAttributes()) diff --git a/js/entity/FunctionReferenceEntity.js b/js/entity/FunctionReferenceEntity.js index 630a85f..441c6ac 100644 --- a/js/entity/FunctionReferenceEntity.js +++ b/js/entity/FunctionReferenceEntity.js @@ -1,9 +1,9 @@ import Entity from "./Entity" -import ObjectReferenceEntity from "./ObjectReferenceEntity" +import ObjectReference from "./primitive/ObjectReference" export default class FunctionReferenceEntity extends Entity { static attributes = { - MemberParent: ObjectReferenceEntity, + MemberParent: ObjectReference, MemberName: "" } diff --git a/js/entity/GuidEntity.js b/js/entity/GuidEntity.js deleted file mode 100644 index cb80a5d..0000000 --- a/js/entity/GuidEntity.js +++ /dev/null @@ -1,37 +0,0 @@ -import Entity from "./Entity"; - -export default class GuidEntity extends Entity { - - static attributes = { - value: String - } - - static generateGuid(random) { - let values = new Uint32Array(4); - if (random === true) { - crypto.getRandomValues(values) - } - let result = "" - values.forEach(n => { - result += ('00000000' + n.toString(16).toUpperCase()).slice(-8) - }) - return result - } - - constructor(guid) { - if (guid?.constructor === String) { - guid = { - value: guid - } - } else if (guid?.constructor === Boolean) { - guid = { - value: GuidEntity.generateGuid(guid == true) - } - } - super(guid) - } - - getAttributes() { - return GuidEntity.attributes - } -} \ No newline at end of file diff --git a/js/entity/Integer.js b/js/entity/Integer.js deleted file mode 100644 index 34305fc..0000000 --- a/js/entity/Integer.js +++ /dev/null @@ -1,24 +0,0 @@ -import Entity from "./Entity" - -export default class Integer extends Entity { - - static attributes = { - value: 0 - } - - constructor(value) { - if (value?.constructor === String) { - value = Number(value) - } - if (value?.constructor === Number) { - value = { - value: Math.round(value.valueOf()) - } - } - super(value) - } - - getAttributes() { - return Integer.attributes - } -} \ No newline at end of file diff --git a/js/entity/LocalizedTextEntity.js b/js/entity/LocalizedTextEntity.js deleted file mode 100644 index cddc9b7..0000000 --- a/js/entity/LocalizedTextEntity.js +++ /dev/null @@ -1,15 +0,0 @@ -import Entity from "./Entity" - -export default class LocalizedTextEntity extends Entity { - - static attributes = { - namespace: "", - key: "", - value: "" - } - - getAttributes() { - return LocalizedTextEntity.attributes - } - -} \ No newline at end of file diff --git a/js/entity/ObjectEntity.js b/js/entity/ObjectEntity.js index 9778bdf..77ea5a2 100644 --- a/js/entity/ObjectEntity.js +++ b/js/entity/ObjectEntity.js @@ -1,8 +1,7 @@ import Entity from "./Entity" import FunctionReferenceEntity from "./FunctionReferenceEntity" -import GuidEntity from "./GuidEntity" -import Integer from "./Integer" -import ObjectReferenceEntity from "./ObjectReferenceEntity" +import Guid from "./primitive/Guid" +import ObjectReference from "./primitive/ObjectReference" import PinEntity from "./PinEntity" import TypeInitialization from "./TypeInitialization" import VariableReferenceEntity from "./VariableReferenceEntity" @@ -10,16 +9,16 @@ import VariableReferenceEntity from "./VariableReferenceEntity" export default class ObjectEntity extends Entity { static attributes = { - Class: ObjectReferenceEntity, + Class: ObjectReference, Name: "", - bIsPureFunc: new TypeInitialization(false, false), - VariableReference: new TypeInitialization(null, false, VariableReferenceEntity), - FunctionReference: new TypeInitialization(null, false, FunctionReferenceEntity), - TargetType: new TypeInitialization(null, false, ObjectReferenceEntity), + bIsPureFunc: new TypeInitialization(Boolean, false, false), + VariableReference: new TypeInitialization(VariableReferenceEntity, false, null), + FunctionReference: new TypeInitialization(FunctionReferenceEntity, false, null,), + TargetType: new TypeInitialization(ObjectReference, false, null), NodePosX: 0, NodePosY: 0, - NodeGuid: GuidEntity, - CustomProperties: [new TypeInitialization(null, false, PinEntity)] + NodeGuid: Guid, + CustomProperties: [PinEntity] } getAttributes() { diff --git a/js/entity/ObjectReferenceEntity.js b/js/entity/ObjectReferenceEntity.js deleted file mode 100644 index 0e7fb96..0000000 --- a/js/entity/ObjectReferenceEntity.js +++ /dev/null @@ -1,21 +0,0 @@ -import Entity from "./Entity" - -export default class ObjectReferenceEntity extends Entity { - - static attributes = { - type: "", - path: "" - } - - getAttributes() { - return ObjectReferenceEntity.attributes - } - - toString() { - return (this.type ?? "") + ( - this.path - ? this.type ? `'"${this.path}"'` : this.path - : "" - ) - } -} \ No newline at end of file diff --git a/js/entity/Path.js b/js/entity/Path.js deleted file mode 100644 index bfb7c0b..0000000 --- a/js/entity/Path.js +++ /dev/null @@ -1,3 +0,0 @@ -export default class Path { - -} \ No newline at end of file diff --git a/js/entity/PinEntity.js b/js/entity/PinEntity.js index a786334..156b251 100644 --- a/js/entity/PinEntity.js +++ b/js/entity/PinEntity.js @@ -1,34 +1,34 @@ import Entity from "./Entity" -import GuidEntity from "./GuidEntity" -import LocalizedTextEntity from "./LocalizedTextEntity" -import ObjectReferenceEntity from "./ObjectReferenceEntity" -import TypeInitialization from "./TypeInitialization" +import Guid from "./primitive/Guid" +import LocalizedTextEntity from "./primitive/LocalizedTextEntity" +import ObjectReference from "./primitive/ObjectReference" import PinReferenceEntity from "./PinReferenceEntity" +import TypeInitialization from "./TypeInitialization" export default class PinEntity extends Entity { static attributes = { - PinId: GuidEntity, + PinId: Guid, PinName: "", - PinFriendlyName: new TypeInitialization(new LocalizedTextEntity(), false), + PinFriendlyName: new TypeInitialization(LocalizedTextEntity, false, null), PinToolTip: "", - Direction: new TypeInitialization("", false), + Direction: new TypeInitialization(String, false, ""), PinType: { PinCategory: "", PinSubCategory: "", - PinSubCategoryObject: ObjectReferenceEntity, + PinSubCategoryObject: ObjectReference, PinSubCategoryMemberReference: null, PinValueType: null, - ContainerType: ObjectReferenceEntity, + ContainerType: ObjectReference, bIsReference: false, bIsConst: false, bIsWeakPointer: false, bIsUObjectWrapper: false }, - LinkedTo: [new TypeInitialization(null, false, PinReferenceEntity)], + LinkedTo: [new TypeInitialization(PinReferenceEntity, false, null)], DefaultValue: "", AutogeneratedDefaultValue: "", - PersistentGuid: GuidEntity, + PersistentGuid: Guid, bHidden: false, bNotConnectable: false, bDefaultValueIsReadOnly: false, diff --git a/js/entity/PinReferenceEntity.js b/js/entity/PinReferenceEntity.js index bb19642..ca3598c 100644 --- a/js/entity/PinReferenceEntity.js +++ b/js/entity/PinReferenceEntity.js @@ -1,11 +1,11 @@ import Entity from "./Entity" -import GuidEntity from "./GuidEntity" +import Guid from "./primitive/Guid" export default class PinReferenceEntity extends Entity { static attributes = { objectName: String, - pinGuid: GuidEntity + pinGuid: Guid } getAttributes() { diff --git a/js/entity/TypeInitialization.js b/js/entity/TypeInitialization.js index 2e539ab..9b7ba97 100644 --- a/js/entity/TypeInitialization.js +++ b/js/entity/TypeInitialization.js @@ -1,7 +1,17 @@ import Utility from "../Utility" export default class TypeInitialization { - constructor(value, showDefault = true, type = Utility.getType(value)) { + + /** + * + * @param {typeof Object} type + * @param {boolean} showDefault + * @param {*} value + */ + constructor(type, showDefault = true, value = undefined) { + if (value === undefined) { + value = Utility.sanitize(new type()) + } this.value = value this.showDefault = showDefault this.type = type diff --git a/js/entity/VariableReferenceEntity.js b/js/entity/VariableReferenceEntity.js index da15cbf..37f526d 100644 --- a/js/entity/VariableReferenceEntity.js +++ b/js/entity/VariableReferenceEntity.js @@ -1,12 +1,12 @@ -import GuidEntity from "./GuidEntity" import Entity from "./Entity" +import Guid from "./primitive/Guid" export default class VariableReferenceEntity extends Entity { static attributes = { MemberName: String, - MemberGuid: GuidEntity, - bSelfContext: true + MemberGuid: Guid, + bSelfContext: false } getAttributes() { diff --git a/js/entity/primitive/Guid.js b/js/entity/primitive/Guid.js new file mode 100644 index 0000000..8377a4d --- /dev/null +++ b/js/entity/primitive/Guid.js @@ -0,0 +1,32 @@ +import Primitive from "./Primitive"; + +export default class Guid extends Primitive { + + static generateGuid(random) { + let values = new Uint32Array(4); + if (random === true) { + crypto.getRandomValues(values) + } + let result = "" + values.forEach(n => { + result += ('00000000' + n.toString(16).toUpperCase()).slice(-8) + }) + return result + } + + constructor(guid) { + super() + // Using constructor equality and not instanceof in order to consider both primitives and objects + if (guid?.constructor === Boolean) { + guid = Guid.generateGuid(guid == true) + } + if (guid instanceof Guid) { + guid = guid.value + } + this.value = guid + } + + toString() { + return this.value.toString() + } +} \ No newline at end of file diff --git a/js/entity/primitive/Integer.js b/js/entity/primitive/Integer.js new file mode 100644 index 0000000..e691b99 --- /dev/null +++ b/js/entity/primitive/Integer.js @@ -0,0 +1,25 @@ +import Primitive from "./Primitive" + +export default class Integer extends Primitive { + + constructor(value) { + super() + // Using constructor equality and not instanceof in order to consider both primitives and objects + if (value?.constructor === String) { + value = Number(value) + } + if (value?.constructor === Number) { + value = Math.round(value) + } + /** @type {number} */ + this.value = value + } + + valueOf() { + this.value + } + + toString() { + return this.value.toString() + } +} \ No newline at end of file diff --git a/js/entity/primitive/LocalizedTextEntity.js b/js/entity/primitive/LocalizedTextEntity.js new file mode 100644 index 0000000..9304fad --- /dev/null +++ b/js/entity/primitive/LocalizedTextEntity.js @@ -0,0 +1,22 @@ +import Primitive from "./Primitive" + +export default class LocalizedTextEntity extends Primitive { + + /** + * + * @param {String} namespace + * @param {String} key + * @param {String} value + */ + constructor(namespace, key, value) { + super() + this.namespace = namespace + this.key = key + this.value = value + } + + toString() { + "NSLOCTEXT(" + `"${this.namespace}"` + ", " + `"${this.key}"` + ", " + `"${this.value}"` + ")" + } + +} \ No newline at end of file diff --git a/js/entity/primitive/ObjectReference.js b/js/entity/primitive/ObjectReference.js new file mode 100644 index 0000000..bc9f0e0 --- /dev/null +++ b/js/entity/primitive/ObjectReference.js @@ -0,0 +1,23 @@ +import Primitive from "./Primitive" + +export default class ObjectReference extends Primitive { + + /** + * + * @param {String} type + * @param {String} path + */ + constructor(type, path) { + super() + this.type = type + this.path = path + } + + toString() { + return (this.type ?? "") + ( + this.path + ? this.type ? `'"${this.path}"'` : this.path + : "" + ) + } +} \ No newline at end of file diff --git a/js/entity/primitive/Primitive.js b/js/entity/primitive/Primitive.js new file mode 100644 index 0000000..fb850da --- /dev/null +++ b/js/entity/primitive/Primitive.js @@ -0,0 +1,5 @@ +export default class Primitive { + toString() { + return "Unimplemented for " + this.constructor.name + } +} \ No newline at end of file diff --git a/js/serialization/Grammar.js b/js/serialization/Grammar.js index 9d840c6..37f39e0 100644 --- a/js/serialization/Grammar.js +++ b/js/serialization/Grammar.js @@ -1,12 +1,12 @@ import FunctionReferenceEntity from "../entity/FunctionReferenceEntity" -import GuidEntity from "../entity/GuidEntity" -import Integer from "../entity/Integer" -import ObjectReferenceEntity from "../entity/ObjectReferenceEntity" +import Guid from "../entity/primitive/Guid" +import Integer from "../entity/primitive/Integer" +import ObjectReference from "../entity/primitive/ObjectReference" import Parsimmon from "parsimmon" import PinEntity from "../entity/PinEntity" import Utility from "../Utility" import ObjectEntity from "../entity/ObjectEntity" -import LocalizedTextEntity from "../entity/LocalizedTextEntity" +import LocalizedTextEntity from "../entity/primitive/LocalizedTextEntity" import PinReferenceEntity from "../entity/PinReferenceEntity" let P = Parsimmon @@ -17,13 +17,13 @@ export default class Grammar { InlineOptWhitespace = _ => P.regex(/[^\S\n]*/).desc("inline optional whitespace") WhitespaceNewline = _ => P.regex(/[^\S\n]*\n\s*/).desc("whitespace with at least a newline") Null = r => P.seq(P.string("("), r.InlineOptWhitespace, P.string(")")).map(_ => null).desc("null: ()") - None = _ => P.string("None").map(_ => new ObjectReferenceEntity({ type: "None" })).desc("none") + None = _ => P.string("None").map(_ => new ObjectReference({ type: "None" })).desc("none") Boolean = _ => P.alt(P.string("True"), P.string("False")).map(v => v === "True" ? true : false).desc("either True or False") Number = _ => P.regex(/[0-9]+(?:\.[0-9]+)?/).map(Number).desc("a number") Integer = _ => P.regex(/[0-9]+/).map(v => new Integer(v)).desc("an integer") String = _ => P.regex(/(?:[^"\\]|\\")*/).wrap(P.string('"'), P.string('"')).desc('string (with possibility to escape the quote using \")') Word = _ => P.regex(/[a-zA-Z]+/).desc("a word") - Guid = _ => P.regex(/[0-9a-zA-Z]{32}/).map(v => new GuidEntity({ value: v })).desc("32 digit hexadecimal (accepts all the letters for safety) value") + Guid = _ => P.regex(/[0-9a-zA-Z]{32}/).map(v => new Guid(v)).desc("32 digit hexadecimal (accepts all the letters for safety) value") PathSymbol = _ => P.regex(/[0-9a-zA-Z_]+/) ReferencePath = r => P.seq(P.string("/"), r.PathSymbol.sepBy1(P.string(".")).tieWith(".")) .tie() @@ -32,7 +32,7 @@ export default class Grammar { .desc('a path (words with possibly underscore, separated by ".", separated by "/")') Reference = r => P.alt( r.None, - r.ReferencePath.map(path => new ObjectReferenceEntity({ path: path })), + r.ReferencePath.map(path => new ObjectReference("", path)), P.seqMap( r.Word, P.optWhitespace, @@ -41,10 +41,7 @@ export default class Grammar { P.string(result.split("").reverse().join("")) ) ), - (referenceType, _, referencePath) => new ObjectReferenceEntity({ - type: referenceType, - path: referencePath - }) + (referenceType, _, referencePath) => new ObjectReference(referenceType, referencePath) ) ) AttributeName = r => r.Word.sepBy1(P.string(".")).tieWith(".").desc('words separated by ""') @@ -57,11 +54,7 @@ export default class Grammar { P.string(","), r.String.trim(P.optWhitespace), // value P.string(")"), - (_, namespace, __, key, ___, value, ____) => new LocalizedTextEntity({ - namespace: namespace, - key: key, - value: value - }) + (_, namespace, __, key, ___, value, ____) => new LocalizedTextEntity(namespace, key, value) ) PinReference = r => P.seqMap( r.PathSymbol, @@ -82,9 +75,9 @@ export default class Grammar { return r.Integer case String: return r.String - case GuidEntity: + case Guid: return r.Guid - case ObjectReferenceEntity: + case ObjectReference: return r.Reference case LocalizedTextEntity: return r.LocalizedText diff --git a/js/serialization/Serializer.js b/js/serialization/Serializer.js index e59b088..26404be 100644 --- a/js/serialization/Serializer.js +++ b/js/serialization/Serializer.js @@ -1,6 +1,6 @@ import Grammar from "./Grammar" -import GuidEntity from "../entity/GuidEntity" -import ObjectReferenceEntity from "../entity/ObjectReferenceEntity" +import Guid from "../entity/primitive/Guid" +import ObjectReference from "../entity/primitive/ObjectReference" import Parsimmon from "parsimmon" import TypeInitialization from "../entity/TypeInitialization" import Utility from "../Utility" @@ -19,8 +19,8 @@ export default class Serializer { return this.writeValue(value()) case Boolean: return Utility.FirstCapital(value.toString()) - case ObjectReferenceEntity: - case GuidEntity: + case ObjectReference: + case Guid: return value.toString() case String: return `"${value}"`