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