mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-05-13 15:27:30 +08:00
Fix other interface classes naming convention
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import Grammar from "./Grammar"
|
||||
import Serializer from "./Serializer"
|
||||
import ISerializer from "./ISerializer"
|
||||
|
||||
export default class GeneralSerializer extends Serializer {
|
||||
export default class GeneralSerializer extends ISerializer {
|
||||
|
||||
constructor(wrap, entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
|
||||
wrap = wrap ?? (v => `(${v})`)
|
||||
@@ -10,7 +10,7 @@ export default class GeneralSerializer extends Serializer {
|
||||
}
|
||||
|
||||
read(value) {
|
||||
let grammar = Grammar.getGrammarForType(Serializer.grammar, this.entityType)
|
||||
let grammar = Grammar.getGrammarForType(ISerializer.grammar, this.entityType)
|
||||
const parseResult = grammar.parse(value)
|
||||
if (!parseResult.status) {
|
||||
console.error("Error when trying to parse the entity " + this.entityType.prototype.constructor.name)
|
||||
|
||||
158
js/serialization/Serializer.js → js/serialization/ISerializer.js
Executable file → Normal file
158
js/serialization/Serializer.js → js/serialization/ISerializer.js
Executable file → Normal file
@@ -1,79 +1,79 @@
|
||||
import IEntity from "../entity/IEntity"
|
||||
import Grammar from "./Grammar"
|
||||
import Parsimmon from "parsimmon"
|
||||
import SerializerFactory from "./SerializerFactory"
|
||||
import TypeInitialization from "../entity/TypeInitialization"
|
||||
import Utility from "../Utility"
|
||||
|
||||
export default class Serializer {
|
||||
|
||||
static grammar = Parsimmon.createLanguage(new Grammar())
|
||||
|
||||
constructor(entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
|
||||
this.entityType = entityType
|
||||
this.prefix = prefix ?? ""
|
||||
this.separator = separator ?? ","
|
||||
this.trailingSeparator = trailingSeparator ?? false
|
||||
this.attributeValueConjunctionSign = attributeValueConjunctionSign ?? "="
|
||||
this.attributeKeyPrinter = attributeKeyPrinter ?? (k => k.join("."))
|
||||
}
|
||||
|
||||
writeValue(value) {
|
||||
if (value === null) {
|
||||
return "()"
|
||||
}
|
||||
const serialize = v => SerializerFactory.getSerializer(Utility.getType(v)).write(v)
|
||||
// This is an exact match (and not instanceof) to hit also primitive types (by accessing value.constructor they are converted to objects automatically)
|
||||
switch (value?.constructor) {
|
||||
case Function:
|
||||
return this.writeValue(value())
|
||||
case Boolean:
|
||||
return Utility.FirstCapital(value.toString())
|
||||
case Number:
|
||||
return value.toString()
|
||||
case String:
|
||||
return `"${value}"`
|
||||
}
|
||||
if (value instanceof Array) {
|
||||
return `(${value.map(v => serialize(v) + ",")})`
|
||||
}
|
||||
if (value instanceof IEntity) {
|
||||
return serialize(value)
|
||||
}
|
||||
}
|
||||
|
||||
subWrite(key, object) {
|
||||
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 += (result.length ? this.separator : "")
|
||||
+ this.subWrite(fullKey, value)
|
||||
} else if (this.showProperty(fullKey, value)) {
|
||||
result += (result.length ? this.separator : "")
|
||||
+ this.prefix
|
||||
+ this.attributeKeyPrinter(fullKey)
|
||||
+ this.attributeValueConjunctionSign
|
||||
+ this.writeValue(value)
|
||||
}
|
||||
}
|
||||
if (this.trailingSeparator && result.length && fullKey.length === 0) {
|
||||
// append separator at the end if asked and there was printed content
|
||||
result += this.separator
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
showProperty(attributeKey, attributeValue) {
|
||||
const attributes = this.entityType.attributes
|
||||
const attribute = Utility.objectGet(attributes, attributeKey)
|
||||
if (attribute instanceof TypeInitialization) {
|
||||
return !Utility.equals(attribute.value, attributeValue) || attribute.showDefault
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
import IEntity from "../entity/IEntity"
|
||||
import Grammar from "./Grammar"
|
||||
import Parsimmon from "parsimmon"
|
||||
import SerializerFactory from "./SerializerFactory"
|
||||
import TypeInitialization from "../entity/TypeInitialization"
|
||||
import Utility from "../Utility"
|
||||
|
||||
export default class ISerializer {
|
||||
|
||||
static grammar = Parsimmon.createLanguage(new Grammar())
|
||||
|
||||
constructor(entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
|
||||
this.entityType = entityType
|
||||
this.prefix = prefix ?? ""
|
||||
this.separator = separator ?? ","
|
||||
this.trailingSeparator = trailingSeparator ?? false
|
||||
this.attributeValueConjunctionSign = attributeValueConjunctionSign ?? "="
|
||||
this.attributeKeyPrinter = attributeKeyPrinter ?? (k => k.join("."))
|
||||
}
|
||||
|
||||
writeValue(value) {
|
||||
if (value === null) {
|
||||
return "()"
|
||||
}
|
||||
const serialize = v => SerializerFactory.getSerializer(Utility.getType(v)).write(v)
|
||||
// This is an exact match (and not instanceof) to hit also primitive types (by accessing value.constructor they are converted to objects automatically)
|
||||
switch (value?.constructor) {
|
||||
case Function:
|
||||
return this.writeValue(value())
|
||||
case Boolean:
|
||||
return Utility.FirstCapital(value.toString())
|
||||
case Number:
|
||||
return value.toString()
|
||||
case String:
|
||||
return `"${value}"`
|
||||
}
|
||||
if (value instanceof Array) {
|
||||
return `(${value.map(v => serialize(v) + ",")})`
|
||||
}
|
||||
if (value instanceof IEntity) {
|
||||
return serialize(value)
|
||||
}
|
||||
}
|
||||
|
||||
subWrite(key, object) {
|
||||
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 += (result.length ? this.separator : "")
|
||||
+ this.subWrite(fullKey, value)
|
||||
} else if (this.showProperty(fullKey, value)) {
|
||||
result += (result.length ? this.separator : "")
|
||||
+ this.prefix
|
||||
+ this.attributeKeyPrinter(fullKey)
|
||||
+ this.attributeValueConjunctionSign
|
||||
+ this.writeValue(value)
|
||||
}
|
||||
}
|
||||
if (this.trailingSeparator && result.length && fullKey.length === 0) {
|
||||
// append separator at the end if asked and there was printed content
|
||||
result += this.separator
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
showProperty(attributeKey, attributeValue) {
|
||||
const attributes = this.entityType.attributes
|
||||
const attribute = Utility.objectGet(attributes, attributeKey)
|
||||
if (attribute instanceof TypeInitialization) {
|
||||
return !Utility.equals(attribute.value, attributeValue) || attribute.showDefault
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import ObjectEntity from "../entity/ObjectEntity"
|
||||
import PinEntity from "../entity/PinEntity"
|
||||
import Serializer from "./Serializer"
|
||||
import ISerializer from "./ISerializer"
|
||||
import SerializerFactory from "./SerializerFactory"
|
||||
|
||||
export default class ObjectSerializer extends Serializer {
|
||||
export default class ObjectSerializer extends ISerializer {
|
||||
|
||||
constructor() {
|
||||
super(ObjectEntity, " ", "\n", false)
|
||||
@@ -21,7 +21,7 @@ export default class ObjectSerializer extends Serializer {
|
||||
}
|
||||
|
||||
read(value) {
|
||||
const parseResult = Serializer.grammar.Object.parse(value)
|
||||
const parseResult = ISerializer.grammar.Object.parse(value)
|
||||
if (!parseResult.status) {
|
||||
console.error("Error when trying to parse the object.")
|
||||
return parseResult
|
||||
@@ -35,7 +35,7 @@ export default class ObjectSerializer extends Serializer {
|
||||
* @returns {ObjectEntity[]}
|
||||
*/
|
||||
readMultiple(value) {
|
||||
const parseResult = Serializer.grammar.MultipleObject.parse(value)
|
||||
const parseResult = ISerializer.grammar.MultipleObject.parse(value)
|
||||
if (!parseResult.status) {
|
||||
console.error("Error when trying to parse the object.")
|
||||
return parseResult
|
||||
|
||||
Reference in New Issue
Block a user