mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
import Grammar from "./Grammar"
|
|
import GuidEntity from "../entity/GuidEntity"
|
|
import ObjectReferenceEntity from "../entity/ObjectReferenceEntity"
|
|
import Parsimmon from "parsimmon"
|
|
import TypeInitialization from "../entity/TypeInitialization"
|
|
import Utility from "../Utility"
|
|
|
|
|
|
export default class Serializer {
|
|
|
|
static grammar = Parsimmon.createLanguage(new Grammar())
|
|
|
|
writeValue(value) {
|
|
if (value === null) {
|
|
return "()"
|
|
}
|
|
switch (value?.constructor) {
|
|
case Function:
|
|
return this.writeValue(value())
|
|
case Boolean:
|
|
return Utility.FirstCapital(value.toString())
|
|
case ObjectReferenceEntity:
|
|
case GuidEntity:
|
|
return value.toString()
|
|
case String:
|
|
return `"${value}"`
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {String[]} prefix
|
|
* @param {Object} object
|
|
* @param {String} separator
|
|
* @returns
|
|
*/
|
|
subWrite(key, object, separator = "\n", prefix = "") {
|
|
let result = ""
|
|
let fullKey = key.concat("")
|
|
const last = fullKey.length - 1
|
|
for (const property in object) {
|
|
fullKey[last] = property
|
|
const value = object[property]
|
|
if (object[property]?.constructor === Object) {
|
|
// Recursive call when finding an object
|
|
result += this.subWrite(fullKey, value, separator, prefix)
|
|
} else if (this.showProperty(fullKey, value)) {
|
|
result += prefix + fullKey.join(".") + "=" + this.writeValue(value) + separator
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
getAttributes() {
|
|
return PinEntity.attributes
|
|
}
|
|
|
|
showProperty(attributeKey, attributeValue) {
|
|
const attributes = this.getAttributes()
|
|
const attribute = Utility.objectGet(attributes, attributeKey)
|
|
if (attribute instanceof TypeInitialization) {
|
|
return !Utility.equals(attribute.value, attributeValue) || attribute.showDefault
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {String} value
|
|
*/
|
|
read(value) {
|
|
}
|
|
|
|
/**
|
|
* Returns a string representing the object (serialization)
|
|
* @param {*} object
|
|
* @returns The serialized string
|
|
*/
|
|
write(object) {
|
|
return ''
|
|
}
|
|
} |