Entities cleanup, Primitive concept introduced

This commit is contained in:
barsdeveloper
2021-10-25 15:49:09 +02:00
parent bee57a1402
commit 92828705d3
21 changed files with 292 additions and 284 deletions

230
dist/ueblueprint.js vendored
View File

@@ -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}"`

View File

@@ -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) {

View File

@@ -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())

View File

@@ -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: ""
}

View File

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

View File

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

View File

@@ -1,15 +0,0 @@
import Entity from "./Entity"
export default class LocalizedTextEntity extends Entity {
static attributes = {
namespace: "",
key: "",
value: ""
}
getAttributes() {
return LocalizedTextEntity.attributes
}
}

View File

@@ -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() {

View File

@@ -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
: ""
)
}
}

View File

@@ -1,3 +0,0 @@
export default class Path {
}

View File

@@ -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,

View File

@@ -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() {

View File

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

View File

@@ -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() {

View File

@@ -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()
}
}

View File

@@ -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()
}
}

View File

@@ -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}"` + ")"
}
}

View File

@@ -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
: ""
)
}
}

View File

@@ -0,0 +1,5 @@
export default class Primitive {
toString() {
return "Unimplemented for " + this.constructor.name
}
}

View File

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

View File

@@ -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}"`