mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-28 03:24:43 +08:00
Grammar refactoring WIP
This commit is contained in:
@@ -1,50 +1,81 @@
|
||||
import Parsimmon from "parsimmon"
|
||||
import PinGrammar from "./PinGrammar"
|
||||
import Grammar from "./Grammar"
|
||||
import Utility from "../Utility"
|
||||
import TypeInitialization from "../entity/TypeInitialization"
|
||||
import ObjectReferenceEntity from "../entity/ObjectReferenceEntity"
|
||||
import Guid from "../Guid"
|
||||
|
||||
|
||||
export default class Serializer {
|
||||
|
||||
static writeValue(value) {
|
||||
if (value?.constructor?.name === 'Function') {
|
||||
return this.writeValue(value())
|
||||
}
|
||||
// No quotes
|
||||
static grammar = Parsimmon.createLanguage(new Grammar())
|
||||
|
||||
writeValue(value) {
|
||||
if (value === null) {
|
||||
return '()'
|
||||
return "()"
|
||||
}
|
||||
if (value?.constructor?.name === 'Boolean') {
|
||||
return value ? 'True' : 'False'
|
||||
}
|
||||
if (value?.constructor?.name === 'ETypesNames' || value?.constructor?.name === 'FGuid') {
|
||||
return value.toString()
|
||||
}
|
||||
// Quotes
|
||||
if (value?.constructor?.name === 'String') {
|
||||
return `"${value}"`
|
||||
switch (value?.constructor) {
|
||||
case Function:
|
||||
return this.writeValue(value())
|
||||
case Boolean:
|
||||
return Utility.FirstCapital(value.toString())
|
||||
case ObjectReferenceEntity:
|
||||
case Guid:
|
||||
return value.toString()
|
||||
case String:
|
||||
return `"${value}"`
|
||||
}
|
||||
}
|
||||
|
||||
static subWrite(prefix, object) {
|
||||
/**
|
||||
*
|
||||
* @param {String[]} prefix
|
||||
* @param {Object} object
|
||||
* @param {String} separator
|
||||
* @returns
|
||||
*/
|
||||
subWrite(key, object, separator = "\n", prefix = "") {
|
||||
let result = ""
|
||||
prefix += prefix != "" ? "." : ""
|
||||
const fullPropertyName = prefix + property
|
||||
let fullKey = key.concat("")
|
||||
const last = fullKey.length - 1
|
||||
for (const property in object) {
|
||||
if (object[property]?.constructor?.name === 'Object') {
|
||||
result += Serializer.subWrite(fullPropertyName, object[property])
|
||||
} else if (!object.constructor.optionalKeys.contains(fullPropertyName)) {
|
||||
result += `${fullPropertyName}=${Serializer.writeValue(object[property])},`
|
||||
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) {
|
||||
//Parsimmon.length
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representing the object (serialization)
|
||||
* @param {*} object
|
||||
* @returns The serialized string
|
||||
*/
|
||||
write(object) {
|
||||
return ''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user