Attributes initialization refactoring (#19)

This commit is contained in:
barsdeveloper
2024-03-24 17:30:50 +01:00
committed by GitHub
parent 5973570911
commit cc9e3d833a
93 changed files with 4134 additions and 4082 deletions

View File

@@ -1,10 +1,11 @@
import Parsernostrum from "parsernostrum"
import Configuration from "../Configuration.js"
import Utility from "../Utility.js"
import AttributeInfo from "../entity/AttributeInfo.js"
import IEntity from "../entity/IEntity.js"
import MirroredEntity from "../entity/MirroredEntity.js"
import Parsernostrum from "parsernostrum"
import Serializable from "./Serializable.js"
import Union from "../entity/Union.js"
import Utility from "../Utility.js"
import Serializable from "./Serializable.js"
export default class Grammar {
@@ -66,16 +67,10 @@ export default class Grammar {
/* --- Factory --- */
/**
* @template {AttributeTypeDescription} T
* @param {T} type
* @template T
* @param {AttributeInfo<T>} attribute
*/
static grammarFor(
attribute,
type = attribute?.constructor === Object
? attribute.type
: attribute?.constructor,
defaultGrammar = this.unknownValue
) {
static grammarFor(attribute, type = attribute?.type, defaultGrammar = this.unknownValue) {
let result = defaultGrammar
if (type instanceof Array) {
if (attribute?.inlined) {
@@ -103,6 +98,9 @@ export default class Grammar {
case Boolean:
result = this.boolean
break
case null:
result = this.null
break
case Number:
result = this.number
break
@@ -114,11 +112,11 @@ export default class Grammar {
break
default:
if (/** @type {AttributeConstructor<any>} */(type)?.prototype instanceof Serializable) {
return /** @type {typeof Serializable} */(type).grammar
result = /** @type {typeof Serializable} */(type).grammar
}
}
}
if (attribute?.constructor === Object) {
if (attribute) {
if (attribute.serialized && type.constructor !== String) {
if (result == this.unknownValue) {
result = this.string
@@ -137,7 +135,7 @@ export default class Grammar {
* @template {AttributeConstructor<Attribute>} T
* @param {T} entityType
* @param {String[]} key
* @returns {AttributeInformation}
* @returns {AttributeInfo}
*/
static getAttribute(entityType, key) {
let result
@@ -191,13 +189,14 @@ export default class Grammar {
* @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
*/
static createEntityGrammar = (entityType, acceptUnknownKeys = true, entriesSeparator = this.commaSeparation) =>
Parsernostrum.seq(
static createEntityGrammar(entityType, acceptUnknownKeys = true, entriesSeparator = this.commaSeparation) {
const lookbehind = entityType.attributes.lookbehind.default
return Parsernostrum.seq(
Parsernostrum.reg(
entityType.lookbehind instanceof Union
? new RegExp(`(${entityType.lookbehind.values.reduce((acc, cur) => acc + "|" + cur)})\\s*\\(\\s*`)
: entityType.lookbehind.constructor == String && entityType.lookbehind.length
? new RegExp(`(${entityType.lookbehind})\\s*\\(\\s*`)
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
),
@@ -218,7 +217,7 @@ export default class Grammar {
let missingKey
// Check missing values
if (
Object.keys(/** @type {AttributeInformation} */(entityType.attributes))
Object.keys(/** @type {AttributeDeclarations} */(entityType.attributes))
.filter(key => entityType.attributes[key].expected)
.find(key => !totalKeys.includes(key) && (missingKey = key))
) {
@@ -230,6 +229,7 @@ export default class Grammar {
}
return Parsernostrum.success().map(() => new entityType(values))
})
}
/* --- Entity --- */

View File

@@ -1,7 +1,7 @@
import Configuration from "../Configuration.js"
import Grammar from "./Grammar.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"

View File

@@ -1,7 +1,8 @@
import Grammar from "./Grammar.js"
import IEntity from "../entity/IEntity.js"
import SerializerFactory from "./SerializerFactory.js"
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 {
@@ -54,7 +55,11 @@ export default class Serializer {
let grammar = Grammar.grammarFor(undefined, this.entityType)
const parseResult = grammar.run(value)
if (!parseResult.status) {
throw new Error(`Error when trying to parse the entity ${this.entityType.prototype.constructor.name}.`)
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
}
@@ -85,7 +90,7 @@ export default class Serializer {
if (attributes[key]?.quoted) {
keyValue = `"${keyValue}"`
}
const isSerialized = Utility.isSerialized(entity, key)
const isSerialized = AttributeInfo.getAttribute(entity, key, "serialized")
if (first) {
first = false
} else {
@@ -140,10 +145,21 @@ export default class Serializer {
return serializer.doWrite(value, insideString, indentation)
}
/**
* @param {IEntity} entity
* @param {String} key
*/
showProperty(entity, key) {
const attribute = /** @type {EntityConstructor} */(this.entityType).attributes[key]
if (attribute?.constructor === Object && attribute.ignored) {
return false
if (entity instanceof IEntity) {
if (
AttributeInfo.getAttribute(entity, key, "ignored")
|| AttributeInfo.getAttribute(entity, key, "silent") && Utility.equals(
AttributeInfo.getAttribute(entity, key, "default"),
entity[key]
)
) {
return false
}
}
return true
}

View File

@@ -1,5 +1,5 @@
import Serializer from "./Serializer.js"
import Utility from "../Utility.js"
import Serializer from "./Serializer.js"
/**
* @template {AttributeConstructor<Attribute>} T

View File

@@ -1,11 +1,11 @@
import Parsernostrum from "parsernostrum"
import Utility from "../Utility.js"
import ByteEntity from "../entity/ByteEntity.js"
import ColorChannelEntity from "../entity/ColorChannelEntity.js"
import CustomSerializer from "./CustomSerializer.js"
import EnumDisplayValueEntity from "../entity/EnumDisplayValueEntity.js"
import EnumEntity from "../entity/EnumEntity.js"
import FormatTextEntity from "../entity/FormatTextEntity.js"
import FunctionReferenceEntity from "../entity/FunctionReferenceEntity.js"
import Grammar from "./Grammar.js"
import GuidEntity from "../entity/GuidEntity.js"
import IdentifierEntity from "../entity/IdentifierEntity.js"
import Integer64Entity from "../entity/Integer64Entity.js"
@@ -18,27 +18,28 @@ import MacroGraphReferenceEntity from "../entity/MacroGraphReferenceEntity.js"
import MirroredEntity from "../entity/MirroredEntity.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import ObjectReferenceEntity from "../entity/ObjectReferenceEntity.js"
import ObjectSerializer from "./ObjectSerializer.js"
import Parsernostrum from "parsernostrum"
import PathSymbolEntity from "../entity/PathSymbolEntity.js"
import PinEntity from "../entity/PinEntity.js"
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
import RBSerializationVector2DEntity from "../entity/RBSerializationVector2DEntity.js"
import RotatorEntity from "../entity/RotatorEntity.js"
import Serializer from "./Serializer.js"
import SerializerFactory from "./SerializerFactory.js"
import SimpleSerializationRotatorEntity from "../entity/SimpleSerializationRotatorEntity.js"
import SimpleSerializationVector2DEntity from "../entity/SimpleSerializationVector2DEntity.js"
import SimpleSerializationVectorEntity from "../entity/SimpleSerializationVectorEntity.js"
import SymbolEntity from "../entity/SymbolEntity.js"
import TerminalTypeEntity from "../entity/TerminalTypeEntity.js"
import ToStringSerializer from "./ToStringSerializer.js"
import Union from "../entity/Union.js"
import UnknownKeysEntity from "../entity/UnknownKeysEntity.js"
import Utility from "../Utility.js"
import VariableReferenceEntity from "../entity/VariableReferenceEntity.js"
import Vector2DEntity from "../entity/Vector2DEntity.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"
import PinTypeEntity from "../entity/PinTypeEntity.js"
Grammar.unknownValue =
Parsernostrum.alt(
@@ -48,8 +49,8 @@ Grammar.unknownValue =
Parsernostrum.str("None").map(() => new ObjectReferenceEntity({ type: "None" })),
Grammar.null,
Grammar.number,
Grammar.string,
ObjectReferenceEntity.fullReferenceGrammar,
Grammar.string,
LocalizedTextEntity.createGrammar(),
InvariantTextEntity.createGrammar(),
FormatTextEntity.createGrammar(),
@@ -244,6 +245,11 @@ export default function initializeSerializerFactory() {
new Serializer(PinReferenceEntity, undefined, " ", false, "", () => "")
)
SerializerFactory.registerSerializer(
PinTypeEntity,
new Serializer(PinTypeEntity)
)
SerializerFactory.registerSerializer(
TerminalTypeEntity,
new Serializer(TerminalTypeEntity, Serializer.bracketsWrapped)