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

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