Simple entities serialization fixed

This commit is contained in:
barsdeveloper
2024-06-04 14:40:47 +02:00
parent c05e6d3cc9
commit e16822760f
11 changed files with 266 additions and 128 deletions

View File

@@ -243,12 +243,20 @@ export default class IEntity {
}
const thisKeys = Object.keys(this)
const otherKeys = Object.keys(other)
if (thisKeys.length != otherKeys.length || !(this instanceof other.constructor) && !(other instanceof this.constructor)) {
if (
thisKeys.length !== otherKeys.length
|| this.lookbehind != other.lookbehind
|| !(this instanceof other.constructor) && !(other instanceof this.constructor)
) {
return false
}
for (let i = 0; i < thisKeys.length; ++i) {
const a = this[thisKeys[i]]
const b = other[otherKeys[i]]
const k = thisKeys[i]
if (!otherKeys.includes(k)) {
return false
}
const a = this[k]
const b = other[k]
if (a instanceof IEntity) {
if (!a.equals(b)) {
return false
@@ -298,7 +306,7 @@ export default class IEntity {
}
result += serialization
}
if (Self.trailing && result.length) {
if (this.trailing && result.length) {
result += Self.attributeSeparator
}
return Self.wrap(this, result)

View File

@@ -10,7 +10,7 @@ export default class RotatorEntity extends IEntity {
P: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(),
}
static grammar = Grammar.createEntityGrammar(this).label("RotatorEntity")
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("RotatorEntity")
constructor(values) {
super(values)

View File

@@ -4,6 +4,7 @@ import IEntity from "./IEntity.js"
export default class UnknownKeysEntity extends IEntity {
/** @type {P<UnknownKeysEntity>} */
static grammar = P.seq(
// Lookbehind
P.reg(new RegExp(`(${Grammar.Regex.Path.source}|${Grammar.Regex.Symbol.source}\\s*)?\\(\\s*`), 1),

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import IEntity from "./IEntity.js"
import NumberEntity from "./NumberEntity.js"
@@ -9,7 +10,8 @@ export default class Vector2DEntity extends IEntity {
X: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(),
}
static grammar = Grammar.createEntityGrammar(this).label("Vector2DEntity")
/** @type {P<Vector2DEntity>} */
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector2DEntity")
constructor(values) {
super(values)

View File

@@ -11,7 +11,7 @@ export default class Vector4DEntity extends IEntity {
Z: NumberEntity.withDefault(),
W: NumberEntity.withDefault(),
}
static grammar = Grammar.createEntityGrammar(this).label("Vector4DEntity")
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector4DEntity")
constructor(values) {
super(values)

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import IEntity from "./IEntity.js"
import NumberEntity from "./NumberEntity.js"
@@ -10,7 +11,8 @@ export default class VectorEntity extends IEntity {
Y: NumberEntity.withDefault(),
Z: NumberEntity.withDefault(),
}
static grammar = Grammar.createEntityGrammar(this).label("VectorEntity")
/** @type {P<VectorEntity>} */
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("VectorEntity")
constructor(values) {
super(values)

View File

@@ -182,7 +182,7 @@ export default class Grammar {
).chain(([attributeName, _1]) => {
const attributeKey = attributeName.split(Configuration.keysSeparator)
const attributeValue = this.getAttribute(entityType, attributeKey)
return attributeValue.grammar.map(attributeValue =>
return (attributeValue?.grammar ?? IEntity.unknownEntityGrammar).map(attributeValue =>
values => {
handleObjectSet(values, attributeKey, attributeValue)
Utility.objectSet(values, attributeKey, attributeValue)
@@ -192,11 +192,11 @@ export default class Grammar {
}
/**
* @template {IEntity} T
* @param {new (...args: any) => T} entityType
* @return {Parsernostrum<T>}
* @template {typeof IEntity} T
* @param {T} entityType
* @return {Parsernostrum<InstanceType<T>>}
*/
static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation) {
static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation, complete = false) {
const lookbehind = entityType.lookbehind instanceof Array ? entityType.lookbehind.join("|") : entityType.lookbehind
return Parsernostrum.seq(
Parsernostrum.reg(new RegExp(String.raw`(${lookbehind})\s*\(\s*`), 1),
@@ -217,7 +217,12 @@ export default class Grammar {
if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) {
entityType = entityType.withLookbehind(lookbehind)
}
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))
})
}

View File

@@ -67,14 +67,14 @@ export default function initializeSerializerFactory() {
PinReferenceEntity.grammar,
Vector4DEntity.grammar,
VectorEntity.grammar,
Vector2DEntity.grammar,
RotatorEntity.grammar,
LinearColorEntity.grammar,
Vector2DEntity.grammar,
UnknownKeysEntity.grammar,
SymbolEntity.grammar,
ArrayEntity.of(PinReferenceEntity).grammar,
ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar,
Parsernostrum.lazy(() => ArrayEntity.createGrammar(Grammar.unknownValue)),
Parsernostrum.lazy(() => ArrayEntity.createGrammar(IEntity.unknownEntityGrammar)),
)
SerializerFactory.registerSerializer(