mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-16 19:04:40 +08:00
Refactoring entities (#23)
* Still WIP * WIP * ArrayEntity parsing fixed * Fix format text entity * Tests for various entity classes and update entity class implementations * More tests and fixed * More entities fixed * Simple entities serialization fixed * Entities tests fixed * Remove serialization bits * Fix Function reference * CustomProperties creating fixed * WIP * Better typing for grammars * Decoding code fixes * Fixing still * Several fixes * rename toString to serialize * Several fixes * More fixes * Moving more stuff out of Utility * Several fixes * Fixing Linear color entity print * Serialization fixes * Fix serialization * Method to compute grammar * Renaming fix * Fix array grammar and equality check * Fix inlined keys * Fix type * Several serialization fixes * Fix undefined dereference * Several fixes * More fixes and cleanup * Fix keys quoting mechanism * Fix natural number assignment * Fix Int64 toString() * Fix quoted keys for inlined arrays * Fix PG pins * Fix several test cases * Types fixes * New pin default value empty * Fix non existing DefaultValue for variadic nodes * Smaller fixes for crashes * Fix link color when attached to knot * Linking test and more reliability operations for adding pins * Improve issue 18 test * More tests and fixes * Fix enum pin entity * Remove failing test
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
import Serializer from "./Serializer.js"
|
||||
|
||||
/**
|
||||
* @template {AttributeConstructor<Attribute>} T
|
||||
* @extends {Serializer<T>}
|
||||
*/
|
||||
export default class CustomSerializer extends Serializer {
|
||||
|
||||
#objectWriter
|
||||
|
||||
/**
|
||||
* @param {(v: ConstructedType<T>, insideString: Boolean) => String} objectWriter
|
||||
* @param {T} entityType
|
||||
*/
|
||||
constructor(objectWriter, entityType) {
|
||||
super(entityType)
|
||||
this.#objectWriter = objectWriter
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ConstructedType<T>} entity
|
||||
* @param {Boolean} insideString
|
||||
* @returns {String}
|
||||
*/
|
||||
doWrite(entity, insideString, indentation = "") {
|
||||
let result = this.#objectWriter(entity, insideString)
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
import Parsernostrum from "parsernostrum"
|
||||
import Configuration from "../Configuration.js"
|
||||
import Utility from "../Utility.js"
|
||||
import AttributeInfo from "../entity/AttributeInfo.js"
|
||||
import AlternativesEntity from "../entity/AlternativesEntity.js"
|
||||
import IEntity from "../entity/IEntity.js"
|
||||
import MirroredEntity from "../entity/MirroredEntity.js"
|
||||
import Union from "../entity/Union.js"
|
||||
import Serializable from "./Serializable.js"
|
||||
|
||||
export default class Grammar {
|
||||
|
||||
/** @type {String} */
|
||||
// @ts-expect-error
|
||||
static numberRegexSource = Parsernostrum.number.getParser().parser.regexp.source
|
||||
|
||||
static separatedBy = (source, separator, min = 1) =>
|
||||
new RegExp(
|
||||
source + "(?:" + separator + source + ")"
|
||||
@@ -36,10 +37,11 @@ export default class Grammar {
|
||||
static null = Parsernostrum.reg(/\(\s*\)/).map(() => null)
|
||||
static true = Parsernostrum.reg(/true/i).map(() => true)
|
||||
static false = Parsernostrum.reg(/false/i).map(() => false)
|
||||
static boolean = Parsernostrum.regArray(/(true)|false/i).map(v => v[1] ? true : false)
|
||||
static number = Parsernostrum.regArray(
|
||||
// @ts-expect-error
|
||||
new RegExp(`(${Parsernostrum.number.getParser().parser.regexp.source})|(\\+?inf)|(-inf)`)
|
||||
).map(([_0, n, plusInf, minusInf]) => n ? Number(n) : plusInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY)
|
||||
// @ts-expect-error
|
||||
static bigInt = Parsernostrum.reg(new RegExp(Parsernostrum.number.getParser().parser.regexp.source)).map(BigInt)
|
||||
.map(result =>
|
||||
result[2] !== undefined
|
||||
@@ -68,173 +70,84 @@ export default class Grammar {
|
||||
/* --- Factory --- */
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {AttributeInfo<T>} attribute
|
||||
* @param {Parsernostrum<any>} defaultGrammar
|
||||
* @returns {Parsernostrum<T>}
|
||||
*/
|
||||
static grammarFor(attribute, type = attribute?.type, defaultGrammar = this.unknownValue) {
|
||||
let result = defaultGrammar
|
||||
if (type === Array || type instanceof Array) {
|
||||
if (attribute?.inlined) {
|
||||
return this.grammarFor(undefined, type[0])
|
||||
}
|
||||
result = Parsernostrum.seq(
|
||||
Parsernostrum.reg(/\(\s*/),
|
||||
this.grammarFor(undefined, type[0]).sepBy(this.commaSeparation).opt(),
|
||||
Parsernostrum.reg(/\s*(?:,\s*)?\)/),
|
||||
).map(([_0, values, _3]) => values instanceof Array ? values : [])
|
||||
} else if (type instanceof Union) {
|
||||
result = type.values
|
||||
.map(v => this.grammarFor(undefined, v))
|
||||
.reduce((acc, cur) => !cur || cur === this.unknownValue || acc === this.unknownValue
|
||||
? this.unknownValue
|
||||
: Parsernostrum.alt(acc, cur)
|
||||
)
|
||||
} else if (type instanceof MirroredEntity) {
|
||||
// @ts-expect-error
|
||||
return this.grammarFor(undefined, type.getTargetType())
|
||||
.map(v => new MirroredEntity(type.type, () => v))
|
||||
} else if (attribute?.constructor === Object) {
|
||||
result = this.grammarFor(undefined, type)
|
||||
} else {
|
||||
switch (type) {
|
||||
case Boolean:
|
||||
result = this.boolean
|
||||
break
|
||||
case null:
|
||||
result = this.null
|
||||
break
|
||||
case Number:
|
||||
result = this.number
|
||||
break
|
||||
case BigInt:
|
||||
result = this.bigInt
|
||||
break
|
||||
case String:
|
||||
result = this.string
|
||||
break
|
||||
default:
|
||||
if (/** @type {AttributeConstructor<any>} */(type)?.prototype instanceof Serializable) {
|
||||
result = /** @type {typeof Serializable} */(type).grammar
|
||||
}
|
||||
}
|
||||
}
|
||||
if (attribute) {
|
||||
if (attribute.serialized && type.constructor !== String) {
|
||||
if (result == this.unknownValue) {
|
||||
result = this.string
|
||||
} else {
|
||||
result = Parsernostrum.seq(Parsernostrum.str('"'), result, Parsernostrum.str('"')).map(([_0, value, _2]) => value)
|
||||
}
|
||||
}
|
||||
if (attribute.nullable) {
|
||||
result = Parsernostrum.alt(result, this.null)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {AttributeConstructor<Attribute>} T
|
||||
* @param {T} entityType
|
||||
* @param {typeof IEntity} entityType
|
||||
* @param {String[]} key
|
||||
* @returns {AttributeInfo}
|
||||
* @returns {typeof IEntity}
|
||||
*/
|
||||
static getAttribute(entityType, key) {
|
||||
let result
|
||||
let type
|
||||
if (entityType instanceof Union) {
|
||||
for (let t of entityType.values) {
|
||||
if (result = this.getAttribute(t, key)) {
|
||||
return result
|
||||
static getAttribute(entityType, [key, ...keys]) {
|
||||
const attribute = entityType?.attributes?.[key]
|
||||
if (!attribute) {
|
||||
return
|
||||
}
|
||||
if (attribute.prototype instanceof AlternativesEntity) {
|
||||
for (const alternative of /** @type {typeof AlternativesEntity} */(attribute).alternatives) {
|
||||
const candidate = this.getAttribute(alternative, keys)
|
||||
if (candidate) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
}
|
||||
if (entityType instanceof IEntity.constructor) {
|
||||
// @ts-expect-error
|
||||
result = entityType.attributes[key[0]]
|
||||
type = result?.type
|
||||
} else if (entityType instanceof Array) {
|
||||
result = entityType[key[0]]
|
||||
type = result
|
||||
if (keys.length > 0) {
|
||||
return this.getAttribute(attribute, keys)
|
||||
}
|
||||
if (key.length > 1) {
|
||||
return this.getAttribute(type, key.slice(1))
|
||||
}
|
||||
return result
|
||||
return attribute
|
||||
}
|
||||
|
||||
/** @param {typeof IEntity} entityType */
|
||||
static createAttributeGrammar(
|
||||
entityType,
|
||||
attributeName = this.attributeName,
|
||||
attributeNameGrammar = this.attributeName,
|
||||
valueSeparator = this.equalSeparation,
|
||||
handleObjectSet = (obj, k, v) => { }
|
||||
handleObjectSet = (values, attributeKey, attributeValue) => { },
|
||||
) {
|
||||
return Parsernostrum.seq(
|
||||
attributeName,
|
||||
attributeNameGrammar,
|
||||
valueSeparator,
|
||||
).chain(([attributeName, _1]) => {
|
||||
const attributeKey = attributeName.split(Configuration.keysSeparator)
|
||||
const attributeValue = this.getAttribute(entityType, attributeKey)
|
||||
return this
|
||||
.grammarFor(attributeValue)
|
||||
.map(attributeValue =>
|
||||
values => {
|
||||
handleObjectSet(values, attributeKey, attributeValue)
|
||||
Utility.objectSet(values, attributeKey, attributeValue)
|
||||
}
|
||||
)
|
||||
const grammar = attributeValue ? attributeValue.grammar : IEntity.unknownEntityGrammar
|
||||
return grammar.map(attributeValue =>
|
||||
values => {
|
||||
Utility.objectSet(values, attributeKey, attributeValue)
|
||||
handleObjectSet(values, attributeKey, attributeValue)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {IEntity} T
|
||||
* @param {(new (...args: any) => T) & EntityConstructor} entityType
|
||||
* @param {Boolean | Number} acceptUnknownKeys Number to specify the limit or true, to let it be a reasonable value
|
||||
* @template {typeof IEntity & (new (...values: any) => InstanceType<T>)} T
|
||||
* @param {T} entityType
|
||||
* @return {Parsernostrum<InstanceType<T>>}
|
||||
*/
|
||||
static createEntityGrammar(entityType, acceptUnknownKeys = true, entriesSeparator = this.commaSeparation) {
|
||||
const lookbehind = entityType.attributes.lookbehind.default
|
||||
static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation, complete = false, minKeys = 1) {
|
||||
const lookbehind = entityType.lookbehind instanceof Array ? entityType.lookbehind.join("|") : entityType.lookbehind
|
||||
return Parsernostrum.seq(
|
||||
Parsernostrum.reg(
|
||||
lookbehind instanceof Union
|
||||
? new RegExp(`(${lookbehind.values.reduce((acc, cur) => acc + "|" + cur)})\\s*\\(\\s*`)
|
||||
: lookbehind.constructor == String && lookbehind.length > 0
|
||||
? new RegExp(`(${lookbehind})\\s*\\(\\s*`)
|
||||
: /()\(\s*/,
|
||||
1
|
||||
),
|
||||
this.createAttributeGrammar(entityType).sepBy(entriesSeparator),
|
||||
Parsernostrum.reg(/\s*(?:,\s*)?\)/), // trailing comma
|
||||
Parsernostrum.reg(new RegExp(String.raw`(${lookbehind}\s*)\(\s*`), 1),
|
||||
this.createAttributeGrammar(entityType).sepBy(entriesSeparator, minKeys),
|
||||
Parsernostrum.reg(/\s*(,\s*)?\)/, 1), // optional trailing comma
|
||||
)
|
||||
.map(([lookbehind, attributes, _2]) => {
|
||||
.map(([lookbehind, attributes, trailing]) => {
|
||||
let values = {}
|
||||
attributes.forEach(attributeSetter => attributeSetter(values))
|
||||
if (lookbehind.length) {
|
||||
values.lookbehind = lookbehind
|
||||
values["lookbehind"] = lookbehind
|
||||
}
|
||||
attributes.forEach(attributeSetter => attributeSetter(values))
|
||||
values["trailing"] = trailing !== undefined
|
||||
return values
|
||||
})
|
||||
// Decide if we accept the entity or not. It is accepted if it doesn't have too many unexpected keys
|
||||
.chain(values => {
|
||||
let totalKeys = Object.keys(values)
|
||||
let missingKey
|
||||
// Check missing values
|
||||
if (
|
||||
Object.keys(/** @type {AttributeDeclarations} */(entityType.attributes))
|
||||
.filter(key => entityType.attributes[key].expected)
|
||||
.find(key => !totalKeys.includes(key) && (missingKey = key))
|
||||
) {
|
||||
return Parsernostrum.failure()
|
||||
if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) {
|
||||
entityType = entityType.withLookbehind(lookbehind)
|
||||
}
|
||||
const unknownKeys = Object.keys(values).filter(key => !(key in entityType.attributes)).length
|
||||
if (!acceptUnknownKeys && unknownKeys > 0) {
|
||||
return Parsernostrum.failure()
|
||||
}
|
||||
return Parsernostrum.success().map(() => new entityType(values))
|
||||
const keys = Object.keys(values)
|
||||
return complete
|
||||
? Parsernostrum.success()
|
||||
.assert(v => Object.keys(entityType.attributes).every(k => keys.includes(k)))
|
||||
.map(() => new entityType(values))
|
||||
: Parsernostrum.success().map(() => new entityType(values))
|
||||
})
|
||||
}
|
||||
|
||||
/** @type {Parsernostrum<any>} */
|
||||
static unknownValue // Defined in initializeSerializerFactor to avoid circular include
|
||||
}
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
import Configuration from "../Configuration.js"
|
||||
import AttributeInfo from "../entity/AttributeInfo.js"
|
||||
import ObjectEntity from "../entity/ObjectEntity.js"
|
||||
import PinEntity from "../entity/PinEntity.js"
|
||||
import Grammar from "./Grammar.js"
|
||||
import Serializer from "./Serializer.js"
|
||||
import SerializerFactory from "./SerializerFactory.js"
|
||||
|
||||
/** @extends Serializer<ObjectEntityConstructor> */
|
||||
export default class ObjectSerializer extends Serializer {
|
||||
|
||||
constructor(entityType = ObjectEntity) {
|
||||
super(entityType, undefined, "\n", true, undefined, Serializer.same)
|
||||
}
|
||||
|
||||
showProperty(entity, key) {
|
||||
switch (key) {
|
||||
case "Class":
|
||||
case "Name":
|
||||
case "Archetype":
|
||||
case "ExportPath":
|
||||
case "CustomProperties":
|
||||
// Serielized separately, check doWrite()
|
||||
return false
|
||||
}
|
||||
return super.showProperty(entity, key)
|
||||
}
|
||||
|
||||
/** @param {ObjectEntity} value */
|
||||
write(value, insideString = false) {
|
||||
return this.doWrite(value, insideString) + "\n"
|
||||
}
|
||||
|
||||
/** @param {String} value */
|
||||
doRead(value) {
|
||||
return Grammar.grammarFor(undefined, this.entityType).parse(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} value
|
||||
* @returns {ObjectEntity[]}
|
||||
*/
|
||||
readMultiple(value) {
|
||||
return ObjectEntity.getMultipleObjectsGrammar().parse(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ObjectEntity} entity
|
||||
* @param {Boolean} insideString
|
||||
* @returns {String}
|
||||
*/
|
||||
doWrite(
|
||||
entity,
|
||||
insideString,
|
||||
indentation = "",
|
||||
wrap = this.wrap,
|
||||
attributeSeparator = this.attributeSeparator,
|
||||
trailingSeparator = this.trailingSeparator,
|
||||
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
|
||||
attributeKeyPrinter = this.attributeKeyPrinter,
|
||||
) {
|
||||
const moreIndentation = indentation + Configuration.indentation
|
||||
if (!(entity instanceof ObjectEntity)) {
|
||||
return super.doWrite(
|
||||
entity,
|
||||
insideString,
|
||||
indentation,
|
||||
wrap,
|
||||
attributeSeparator,
|
||||
trailingSeparator,
|
||||
attributeValueConjunctionSign,
|
||||
// @ts-expect-error
|
||||
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
|
||||
)
|
||||
}
|
||||
let result = indentation + "Begin Object"
|
||||
+ (entity.Class?.type || entity.Class?.path ? ` Class=${this.doWriteValue(entity.Class, insideString)}` : "")
|
||||
+ (entity.Name ? ` Name=${this.doWriteValue(entity.Name, insideString)}` : "")
|
||||
+ (entity.Archetype ? ` Archetype=${this.doWriteValue(entity.Archetype, insideString)}` : "")
|
||||
+ (entity.ExportPath?.type || entity.ExportPath?.path ? ` ExportPath=${this.doWriteValue(entity.ExportPath, insideString)}` : "")
|
||||
+ "\n"
|
||||
+ super.doWrite(
|
||||
entity,
|
||||
insideString,
|
||||
moreIndentation,
|
||||
wrap,
|
||||
attributeSeparator,
|
||||
true,
|
||||
attributeValueConjunctionSign,
|
||||
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
|
||||
)
|
||||
+ (!AttributeInfo.getAttribute(entity, "CustomProperties", "ignored")
|
||||
? entity.getCustomproperties().map(pin =>
|
||||
moreIndentation
|
||||
+ attributeKeyPrinter("CustomProperties ")
|
||||
+ SerializerFactory.getSerializer(PinEntity).doWrite(pin, insideString)
|
||||
+ this.attributeSeparator
|
||||
).join("")
|
||||
: ""
|
||||
)
|
||||
+ indentation + "End Object"
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import Parsernostrum from "parsernostrum"
|
||||
|
||||
export default class Serializable {
|
||||
|
||||
static grammar = this.createGrammar()
|
||||
|
||||
/** @protected */
|
||||
static createGrammar() {
|
||||
return /** @type {Parsernostrum<any>} */(Parsernostrum.failure())
|
||||
}
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
import Utility from "../Utility.js"
|
||||
import AttributeInfo from "../entity/AttributeInfo.js"
|
||||
import IEntity from "../entity/IEntity.js"
|
||||
import Grammar from "./Grammar.js"
|
||||
import SerializerFactory from "./SerializerFactory.js"
|
||||
|
||||
/** @template {AttributeConstructor<Attribute>} T */
|
||||
export default class Serializer {
|
||||
|
||||
/** @type {(v: String) => String} */
|
||||
static same = v => v
|
||||
|
||||
/** @type {(entity: Attribute, serialized: String) => String} */
|
||||
static notWrapped = (entity, serialized) => serialized
|
||||
|
||||
/** @type {(entity: Attribute, serialized: String) => String} */
|
||||
static bracketsWrapped = (entity, serialized) => `(${serialized})`
|
||||
|
||||
/** @param {T} entityType */
|
||||
constructor(
|
||||
entityType,
|
||||
/** @type {(entity: ConstructedType<T>, serialized: String) => String} */
|
||||
wrap = (entity, serialized) => serialized,
|
||||
attributeSeparator = ",",
|
||||
trailingSeparator = false,
|
||||
attributeValueConjunctionSign = "=",
|
||||
attributeKeyPrinter = Serializer.same
|
||||
) {
|
||||
this.entityType = entityType
|
||||
this.wrap = wrap
|
||||
this.attributeSeparator = attributeSeparator
|
||||
this.trailingSeparator = trailingSeparator
|
||||
this.attributeValueConjunctionSign = attributeValueConjunctionSign
|
||||
this.attributeKeyPrinter = attributeKeyPrinter
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} value
|
||||
* @returns {ConstructedType<T>}
|
||||
*/
|
||||
read(value) {
|
||||
return this.doRead(value.trim())
|
||||
}
|
||||
|
||||
/** @param {ConstructedType<T>} value */
|
||||
write(value, insideString = false) {
|
||||
return this.doWrite(value, insideString)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} value
|
||||
* @returns {ConstructedType<T>}
|
||||
*/
|
||||
doRead(value) {
|
||||
let grammar = Grammar.grammarFor(undefined, this.entityType)
|
||||
const parseResult = grammar.run(value)
|
||||
if (!parseResult.status) {
|
||||
throw new Error(
|
||||
this.entityType
|
||||
? `Error when trying to parse the entity ${this.entityType.prototype.constructor.name}`
|
||||
: "Error when trying to parse null"
|
||||
)
|
||||
}
|
||||
return parseResult.value
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ConstructedType<T>} entity
|
||||
* @param {Boolean} insideString
|
||||
* @returns {String}
|
||||
*/
|
||||
doWrite(
|
||||
entity,
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
wrap = this.wrap,
|
||||
attributeSeparator = this.attributeSeparator,
|
||||
trailingSeparator = this.trailingSeparator,
|
||||
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
|
||||
attributeKeyPrinter = this.attributeKeyPrinter
|
||||
) {
|
||||
let result = ""
|
||||
const keys = entity._keys ?? Object.keys(entity)
|
||||
let first = true
|
||||
for (const key of keys) {
|
||||
const value = entity[key]
|
||||
if (value !== undefined && this.showProperty(entity, key)) {
|
||||
let keyValue = entity instanceof Array ? `(${key})` : key
|
||||
if (AttributeInfo.getAttribute(entity, key, "quoted")) {
|
||||
keyValue = `"${keyValue}"`
|
||||
}
|
||||
const isSerialized = AttributeInfo.getAttribute(entity, key, "serialized")
|
||||
if (first) {
|
||||
first = false
|
||||
} else {
|
||||
result += attributeSeparator
|
||||
}
|
||||
if (AttributeInfo.getAttribute(entity, key, "inlined")) {
|
||||
result += this.doWrite(
|
||||
value,
|
||||
insideString,
|
||||
indentation,
|
||||
Serializer.notWrapped,
|
||||
attributeSeparator,
|
||||
false,
|
||||
attributeValueConjunctionSign,
|
||||
AttributeInfo.getAttribute(entity, key, "type") instanceof Array
|
||||
? k => attributeKeyPrinter(`${keyValue}${k}`)
|
||||
: k => attributeKeyPrinter(`${keyValue}.${k}`)
|
||||
)
|
||||
continue
|
||||
}
|
||||
const keyPrinted = attributeKeyPrinter(keyValue)
|
||||
const indentationPrinted = attributeSeparator.includes("\n") ? indentation : ""
|
||||
result += (
|
||||
keyPrinted.length
|
||||
? (indentationPrinted + keyPrinted + this.attributeValueConjunctionSign)
|
||||
: ""
|
||||
)
|
||||
+ (
|
||||
isSerialized
|
||||
? `"${this.doWriteValue(value, true, indentation)}"`
|
||||
: this.doWriteValue(value, insideString, indentation)
|
||||
)
|
||||
}
|
||||
}
|
||||
if (trailingSeparator && result.length) {
|
||||
// append separator at the end if asked and there was printed content
|
||||
result += attributeSeparator
|
||||
}
|
||||
return wrap(entity, result)
|
||||
}
|
||||
|
||||
/** @param {Boolean} insideString */
|
||||
doWriteValue(value, insideString, indentation = "") {
|
||||
const type = Utility.getType(value)
|
||||
const serializer = SerializerFactory.getSerializer(type)
|
||||
if (!serializer) {
|
||||
throw new Error(
|
||||
`Unknown value type "${type.name}", a serializer must be registered in the SerializerFactory class, `
|
||||
+ "check initializeSerializerFactory.js"
|
||||
)
|
||||
}
|
||||
return serializer.doWrite(value, insideString, indentation)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {IEntity} entity
|
||||
* @param {String} key
|
||||
*/
|
||||
showProperty(entity, key) {
|
||||
if (entity instanceof IEntity) {
|
||||
if (AttributeInfo.getAttribute(entity, key, "ignored")) {
|
||||
return false
|
||||
}
|
||||
if (AttributeInfo.getAttribute(entity, key, "silent")) {
|
||||
let defaultValue = AttributeInfo.getAttribute(entity, key, "default")
|
||||
if (defaultValue instanceof Function) {
|
||||
defaultValue = defaultValue(entity)
|
||||
}
|
||||
if (Utility.equals(entity[key], defaultValue)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
export default class SerializerFactory {
|
||||
|
||||
static #serializers = new Map()
|
||||
|
||||
/**
|
||||
* @template {AttributeConstructor<Attribute>} T
|
||||
* @param {T} type
|
||||
* @param {Serializer<T>} object
|
||||
*/
|
||||
static registerSerializer(type, object) {
|
||||
SerializerFactory.#serializers.set(type, object)
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {AttributeConstructor<Attribute>} T
|
||||
* @param {T} type
|
||||
* @returns {Serializer<T>}
|
||||
*/
|
||||
static getSerializer(type) {
|
||||
return SerializerFactory.#serializers.get(type)
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import Utility from "../Utility.js"
|
||||
import Serializer from "./Serializer.js"
|
||||
|
||||
/**
|
||||
* @template {AttributeConstructor<Attribute>} T
|
||||
* @extends {Serializer<T>}
|
||||
*/
|
||||
export default class ToStringSerializer extends Serializer {
|
||||
|
||||
/** @param {T} entityType */
|
||||
constructor(entityType, escape = true) {
|
||||
super(entityType)
|
||||
if (escape) {
|
||||
this.wrap = (entity, serialized) => Utility.escapeString(serialized)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ConstructedType<T>} entity
|
||||
* @param {Boolean} insideString
|
||||
*/
|
||||
doWrite(entity, insideString, indentation = "") {
|
||||
|
||||
return !insideString && entity.constructor === String
|
||||
? `"${this.wrap(entity, entity.toString())}"` // String will have quotes if not inside a string already
|
||||
: this.wrap(entity, entity.toString())
|
||||
}
|
||||
}
|
||||
@@ -1,342 +1,49 @@
|
||||
import Parsernostrum from "parsernostrum"
|
||||
import Utility from "../Utility.js"
|
||||
import BlueprintEntity from "../entity/BlueprintEntity.js"
|
||||
import ByteEntity from "../entity/ByteEntity.js"
|
||||
import ColorChannelEntity from "../entity/ColorChannelEntity.js"
|
||||
import EnumDisplayValueEntity from "../entity/EnumDisplayValueEntity.js"
|
||||
import EnumEntity from "../entity/EnumEntity.js"
|
||||
import AlternativesEntity from "../entity/AlternativesEntity.js"
|
||||
import ArrayEntity from "../entity/ArrayEntity.js"
|
||||
import BooleanEntity from "../entity/BooleanEntity.js"
|
||||
import FormatTextEntity from "../entity/FormatTextEntity.js"
|
||||
import FunctionReferenceEntity from "../entity/FunctionReferenceEntity.js"
|
||||
import GuidEntity from "../entity/GuidEntity.js"
|
||||
import IdentifierEntity from "../entity/IdentifierEntity.js"
|
||||
import Integer64Entity from "../entity/Integer64Entity.js"
|
||||
import IntegerEntity from "../entity/IntegerEntity.js"
|
||||
import IEntity from "../entity/IEntity.js"
|
||||
import InvariantTextEntity from "../entity/InvariantTextEntity.js"
|
||||
import KeyBindingEntity from "../entity/KeyBindingEntity.js"
|
||||
import LinearColorEntity from "../entity/LinearColorEntity.js"
|
||||
import LocalizedTextEntity from "../entity/LocalizedTextEntity.js"
|
||||
import MacroGraphReferenceEntity from "../entity/MacroGraphReferenceEntity.js"
|
||||
import MirroredEntity from "../entity/MirroredEntity.js"
|
||||
import ObjectEntity from "../entity/ObjectEntity.js"
|
||||
import NullEntity from "../entity/NullEntity.js"
|
||||
import NumberEntity from "../entity/NumberEntity.js"
|
||||
import ObjectReferenceEntity from "../entity/ObjectReferenceEntity.js"
|
||||
import PathSymbolEntity from "../entity/PathSymbolEntity.js"
|
||||
import PinEntity from "../entity/PinEntity.js"
|
||||
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
|
||||
import PinTypeEntity from "../entity/PinTypeEntity.js"
|
||||
import RBSerializationVector2DEntity from "../entity/RBSerializationVector2DEntity.js"
|
||||
import RotatorEntity from "../entity/RotatorEntity.js"
|
||||
import ScriptVariableEntity from "../entity/ScriptVariableEntity.js"
|
||||
import SimpleSerializationRotatorEntity from "../entity/SimpleSerializationRotatorEntity.js"
|
||||
import SimpleSerializationVector2DEntity from "../entity/SimpleSerializationVector2DEntity.js"
|
||||
import SimpleSerializationVector4DEntity from "../entity/SimpleSerializationVector4DEntity.js"
|
||||
import SimpleSerializationVectorEntity from "../entity/SimpleSerializationVectorEntity.js"
|
||||
import StringEntity from "../entity/StringEntity.js"
|
||||
import SymbolEntity from "../entity/SymbolEntity.js"
|
||||
import TerminalTypeEntity from "../entity/TerminalTypeEntity.js"
|
||||
import Union from "../entity/Union.js"
|
||||
import UnknownKeysEntity from "../entity/UnknownKeysEntity.js"
|
||||
import VariableReferenceEntity from "../entity/VariableReferenceEntity.js"
|
||||
import Vector2DEntity from "../entity/Vector2DEntity.js"
|
||||
import Vector4DEntity from "../entity/Vector4DEntity.js"
|
||||
import VectorEntity from "../entity/VectorEntity.js"
|
||||
import CustomSerializer from "./CustomSerializer.js"
|
||||
import Grammar from "./Grammar.js"
|
||||
import ObjectSerializer from "./ObjectSerializer.js"
|
||||
import Serializer from "./Serializer.js"
|
||||
import SerializerFactory from "./SerializerFactory.js"
|
||||
import ToStringSerializer from "./ToStringSerializer.js"
|
||||
|
||||
Grammar.unknownValue =
|
||||
Parsernostrum.alt(
|
||||
// Remember to keep the order, otherwise parsing might fail
|
||||
Grammar.boolean,
|
||||
GuidEntity.grammar,
|
||||
Parsernostrum.str("None").map(() => new ObjectReferenceEntity({ type: "None" })),
|
||||
Grammar.null,
|
||||
Grammar.number,
|
||||
ObjectReferenceEntity.fullReferenceGrammar,
|
||||
Grammar.string,
|
||||
LocalizedTextEntity.grammar,
|
||||
InvariantTextEntity.grammar,
|
||||
FormatTextEntity.grammar,
|
||||
PinReferenceEntity.grammar,
|
||||
Vector4DEntity.grammar,
|
||||
VectorEntity.grammar,
|
||||
RotatorEntity.grammar,
|
||||
LinearColorEntity.grammar,
|
||||
Vector2DEntity.grammar,
|
||||
UnknownKeysEntity.grammar,
|
||||
SymbolEntity.grammar,
|
||||
Grammar.grammarFor(undefined, [PinReferenceEntity]),
|
||||
Grammar.grammarFor(undefined, [new Union(Number, String, SymbolEntity)]),
|
||||
Parsernostrum.lazy(() => Grammar.grammarFor(undefined, [undefined])),
|
||||
)
|
||||
|
||||
export default function initializeSerializerFactory() {
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
null,
|
||||
new CustomSerializer(
|
||||
(nullValue, insideString) => "()",
|
||||
null
|
||||
IEntity.unknownEntityGrammar =
|
||||
Parsernostrum.alt(
|
||||
// Remember to keep the order, otherwise parsing might fail
|
||||
BooleanEntity.grammar,
|
||||
GuidEntity.grammar,
|
||||
Parsernostrum.str("None").map(() => ObjectReferenceEntity.createNoneInstance()),
|
||||
NullEntity.grammar,
|
||||
NumberEntity.grammar,
|
||||
ObjectReferenceEntity.fullReferenceGrammar,
|
||||
StringEntity.grammar,
|
||||
LocalizedTextEntity.grammar,
|
||||
InvariantTextEntity.grammar,
|
||||
FormatTextEntity.grammar,
|
||||
PinReferenceEntity.grammar,
|
||||
Vector4DEntity.grammar,
|
||||
VectorEntity.grammar,
|
||||
Vector2DEntity.grammar,
|
||||
RotatorEntity.grammar,
|
||||
LinearColorEntity.grammar,
|
||||
UnknownKeysEntity.grammar,
|
||||
SymbolEntity.grammar,
|
||||
ArrayEntity.of(PinReferenceEntity).grammar,
|
||||
ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar,
|
||||
Parsernostrum.lazy(() => ArrayEntity.createGrammar(IEntity.unknownEntityGrammar)),
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
Array,
|
||||
new CustomSerializer(
|
||||
(array, insideString) =>
|
||||
`(${array
|
||||
.map(v => SerializerFactory.getSerializer(Utility.getType(v)).write(v, insideString))
|
||||
.join(",")
|
||||
})`,
|
||||
Array
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
BigInt,
|
||||
new ToStringSerializer(BigInt)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
BlueprintEntity,
|
||||
new ObjectSerializer(BlueprintEntity),
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
Boolean,
|
||||
new CustomSerializer(
|
||||
/** @param {Boolean} boolean */
|
||||
(boolean, insideString) => boolean
|
||||
? insideString
|
||||
? "true"
|
||||
: "True"
|
||||
: insideString
|
||||
? "false"
|
||||
: "False",
|
||||
Boolean
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
ByteEntity,
|
||||
new ToStringSerializer(ByteEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
ColorChannelEntity,
|
||||
new ToStringSerializer(ColorChannelEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
EnumDisplayValueEntity,
|
||||
new ToStringSerializer(EnumDisplayValueEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
EnumEntity,
|
||||
new ToStringSerializer(EnumEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
FormatTextEntity,
|
||||
new CustomSerializer(
|
||||
(v, insideString) => {
|
||||
let result = v.getLookbehind() + "("
|
||||
+ v.value.map(v =>
|
||||
SerializerFactory.getSerializer(Utility.getType(v)).write(v, insideString)
|
||||
).join(", ")
|
||||
+ ")"
|
||||
return result
|
||||
},
|
||||
FormatTextEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
FunctionReferenceEntity,
|
||||
new Serializer(FunctionReferenceEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
GuidEntity,
|
||||
new ToStringSerializer(GuidEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
IdentifierEntity,
|
||||
new ToStringSerializer(IdentifierEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
Integer64Entity,
|
||||
new ToStringSerializer(Integer64Entity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
IntegerEntity,
|
||||
new ToStringSerializer(IntegerEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
InvariantTextEntity,
|
||||
new Serializer(InvariantTextEntity, (entity, v) => `${entity.getLookbehind()}(${v})`, ", ", false, "", () => "")
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
KeyBindingEntity,
|
||||
new Serializer(KeyBindingEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
LinearColorEntity,
|
||||
new Serializer(LinearColorEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
LocalizedTextEntity,
|
||||
new Serializer(LocalizedTextEntity, (entity, v) => `${entity.getLookbehind()}(${v})`, ", ", false, "", () => "")
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
MacroGraphReferenceEntity,
|
||||
new Serializer(MacroGraphReferenceEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
MirroredEntity,
|
||||
new CustomSerializer(
|
||||
(v, insideString) => SerializerFactory.getSerializer(v.getTargetType()).write(v.get(), insideString),
|
||||
MirroredEntity
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
Number,
|
||||
new ToStringSerializer(Number)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
ObjectEntity,
|
||||
new ObjectSerializer()
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
ObjectReferenceEntity,
|
||||
new ToStringSerializer(ObjectReferenceEntity, false)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
PathSymbolEntity,
|
||||
new ToStringSerializer(PathSymbolEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
PinEntity,
|
||||
new Serializer(PinEntity, (entity, v) => `${entity.getLookbehind()} (${v})`, ",", true)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
PinReferenceEntity,
|
||||
new Serializer(PinReferenceEntity, undefined, " ", false, "", () => "")
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
PinTypeEntity,
|
||||
new Serializer(PinTypeEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
TerminalTypeEntity,
|
||||
new Serializer(TerminalTypeEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
RBSerializationVector2DEntity,
|
||||
new CustomSerializer(
|
||||
(value, insideString) => `X=${value.X} Y=${value.Y}`,
|
||||
RBSerializationVector2DEntity
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
RotatorEntity,
|
||||
new Serializer(RotatorEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
ScriptVariableEntity,
|
||||
new Serializer(ScriptVariableEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
String,
|
||||
new CustomSerializer(
|
||||
(value, insideString) => insideString
|
||||
? Utility.escapeString(value)
|
||||
: `"${Utility.escapeString(value)}"`,
|
||||
String
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
SimpleSerializationRotatorEntity,
|
||||
new CustomSerializer(
|
||||
(value, insideString) => `${value.P}, ${value.Y}, ${value.R}`,
|
||||
SimpleSerializationRotatorEntity
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
SimpleSerializationVector2DEntity,
|
||||
new CustomSerializer(
|
||||
(value, insideString) => `${value.X}, ${value.Y}`,
|
||||
SimpleSerializationVector2DEntity
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
SimpleSerializationVectorEntity,
|
||||
new CustomSerializer(
|
||||
(value, insideString) => `${value.X}, ${value.Y}, ${value.Z}`,
|
||||
SimpleSerializationVectorEntity
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
SimpleSerializationVector4DEntity,
|
||||
new CustomSerializer(
|
||||
(value, insideString) => `${value.X}, ${value.Y}, ${value.Z}, ${value.W}`,
|
||||
SimpleSerializationVector4DEntity
|
||||
)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
SymbolEntity,
|
||||
new ToStringSerializer(SymbolEntity)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
UnknownKeysEntity,
|
||||
new Serializer(UnknownKeysEntity, (entity, string) => `${entity.getLookbehind() ?? ""}(${string})`)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
VariableReferenceEntity,
|
||||
new Serializer(VariableReferenceEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
Vector2DEntity,
|
||||
new Serializer(Vector2DEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
VectorEntity,
|
||||
new Serializer(VectorEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
Vector4DEntity,
|
||||
new Serializer(Vector4DEntity, Serializer.bracketsWrapped)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user