mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
Attributes initialization refactoring (#19)
This commit is contained in:
@@ -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 --- */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user