Several serialization and deserialization fixes

This commit is contained in:
barsdeveloper
2023-04-15 12:20:53 +02:00
parent d10589f0bd
commit baf40a9094
17 changed files with 366 additions and 149 deletions

View File

@@ -27,7 +27,7 @@ export default class CustomSerializer extends Serializer {
* @param {Boolean} insideString
* @returns {String}
*/
doWrite(entity, insideString = false) {
doWrite(entity, insideString, indentation = "") {
let result = this.#objectWriter(entity, insideString)
return result
}

View File

@@ -29,7 +29,7 @@ import SymbolEntity from "../entity/SymbolEntity.js"
import TerminalTypeEntity from "../entity/TerminalTypeEntity.js"
import UnionType from "../entity/UnionType.js"
import UnknownKeysEntity from "../entity/UnknownKeysEntity.js"
import UserDefinedPinEntity from "../entity/UserDefinedPinEntity.js"
import UnknownPinEntity from "../entity/UnknownPinEntity.js"
import Utility from "../Utility.js"
import VariableReferenceEntity from "../entity/VariableReferenceEntity.js"
import Vector2DEntity from "../entity/Vector2DEntity.js"
@@ -71,7 +71,7 @@ export default class Grammar {
static DotSeparatedSymbols = Grammar.separatedBy(this.Symbol.source, "\\.")
static PathFragment = Grammar.separatedBy(this.Symbol.source, "[\\.:]")
static PathSpaceFragment = Grammar.separatedBy(this.Symbol.source, "[\\.:\\ ]")
static Path = new RegExp(`(?:\\/${this.PathFragment.source}){2,}`)
static Path = new RegExp(`(?:\\/${this.PathFragment.source}){2,}`) // Multiple (2+) /PathFragment
static PathOptSpace = new RegExp(`(?:\\/${this.PathSpaceFragment.source}){2,}`)
}
@@ -100,12 +100,21 @@ export default class Grammar {
static colorValue = this.byteNumber
static word = P.regex(Grammar.Regex.Word)
static pathQuotes = Grammar.regexMap(
new RegExp(`"(${Grammar.Regex.PathOptSpace.source}|${Grammar.Regex.Symbol.source})"|'"(${Grammar.Regex.PathOptSpace.source}|${Grammar.Regex.Symbol.source})"'`),
new RegExp(
`'(` + Grammar.Regex.PathOptSpace.source + `|` + Grammar.Regex.PathFragment.source + `)'`
+ `|"(` + Grammar.Regex.PathOptSpace.source + `|` + Grammar.Regex.PathFragment.source + `)"`
+ `|'"(` + Grammar.Regex.PathOptSpace.source + `|` + Grammar.Regex.PathFragment.source + `)"'`
),
([_0, a, b, c]) => a ?? b ?? c
)
static path = Grammar.regexMap(
new RegExp(`(${Grammar.Regex.Path.source})|"(${Grammar.Regex.PathOptSpace.source})"|'"(${Grammar.Regex.PathOptSpace.source})"'`),
([_0, a, b, c]) => a ?? b ?? c
new RegExp(
`(` + Grammar.Regex.Path.source + `)`
+ `|'(` + Grammar.Regex.PathOptSpace.source + `)'`
+ `|"(` + Grammar.Regex.PathOptSpace.source + `)"`
+ `|'"(` + Grammar.Regex.PathOptSpace.source + `)"'`
),
([_0, a, b, c, d]) => a ?? b ?? c ?? d
)
static symbol = P.regex(Grammar.Regex.Symbol)
static attributeName = P.regex(Grammar.Regex.DotSeparatedSymbols)
@@ -250,8 +259,8 @@ export default class Grammar {
case UnknownKeysEntity:
result = this.unknownKeysEntity
break
case UserDefinedPinEntity:
result = this.userDefinedPinEntity
case UnknownPinEntity:
result = this.unknownPinEntity
break
case VariableReferenceEntity:
result = this.variableReferenceEntity
@@ -527,8 +536,6 @@ export default class Grammar {
static symbolEntity = P.lazy(() => this.symbol.map(v => new SymbolEntity(v)))
static userDefinedPinEntity = P.lazy(() => this.createEntityGrammar(UserDefinedPinEntity))
static variableReferenceEntity = P.lazy(() => this.createEntityGrammar(VariableReferenceEntity))
static vector2DEntity = P.lazy(() => this.createEntityGrammar(Vector2DEntity, false))
@@ -554,14 +561,33 @@ export default class Grammar {
)
.map(([lookbehind, attributes, _2]) => {
let values = {}
attributes.forEach(attributeSetter => attributeSetter(values))
if (lookbehind.length) {
values.lookbehind = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
return new UnknownKeysEntity(values)
})
)
static unknownPinEntity = P.lazy(() =>
P.seq(
this.regexMap(
new RegExp(`${this.Regex.Symbol.source}\\s*\\(\\s*`),
result => result[1] ?? ""
),
this.createAttributeGrammar(this.unknownPinEntity).sepBy1(this.commaSeparation),
P.regex(/\s*(?:,\s*)?\)/)
)
.map(([lookbehind, attributes, _2]) => {
let values = {}
if (lookbehind.length) {
values.lookbehind = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
return new UnknownPinEntity(values)
})
)
static unknownValue = P.lazy(() =>
P.alt(
// Remember to keep the order, otherwise parsing might fail
@@ -607,10 +633,18 @@ export default class Grammar {
v => v[1]
)
)
.chain(([symbol, _1]) =>
.chain(([symbol, index]) =>
this.grammarFor(ObjectEntity.attributes[symbol])
.map(currentValue =>
values => (values[symbol] ??= []).push(currentValue)
values => {
(values[symbol] ??= [])[index] = currentValue
if (!ObjectEntity.attributes[symbol]?.inlined) {
if (!values.attributes) {
IEntity.defineAttributes(values, {})
}
Utility.objectSet(values, ["attributes", symbol, "inlined"], true, true)
}
}
)
)
)

View File

@@ -1,13 +1,14 @@
import Configuration from "../Configuration.js"
import Grammar from "./Grammar.js"
import Serializer from "./Serializer.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import PinEntity from "../entity/PinEntity.js"
import Serializer from "./Serializer.js"
import SerializerFactory from "./SerializerFactory.js"
export default class ObjectSerializer extends Serializer {
constructor() {
super(ObjectEntity, undefined, "\n", false, undefined, k => ` ${k}`)
super(ObjectEntity, undefined, "\n", true, undefined, Serializer.same)
}
showProperty(entity, key) {
@@ -21,6 +22,11 @@ export default class ObjectSerializer extends Serializer {
return super.showProperty(entity, key)
}
/** @param {ObjectEntity} value */
write(value, insideString = false) {
return this.doWrite(value, insideString) + "\n"
}
/** @param {String} value */
doRead(value) {
const parseResult = Grammar.objectEntity.parse(value)
@@ -50,24 +56,48 @@ export default class ObjectSerializer extends Serializer {
doWrite(
entity,
insideString,
indentation = "",
wrap = this.wrap,
attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
attributeKeyPrinter = this.attributeKeyPrinter
attributeKeyPrinter = this.attributeKeyPrinter,
) {
const moreIndentation = indentation + Configuration.indentation
if (!(entity instanceof ObjectEntity)) {
return super.doWrite(entity, insideString)
return super.doWrite(
entity,
insideString,
indentation,
wrap,
attributeSeparator,
trailingSeparator,
attributeValueConjunctionSign,
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
)
}
let result = `Begin Object Class=${entity.Class.path} Name=${this.doWriteValue(entity.Name, insideString)}\n`
+ super.doWrite(entity, insideString)
let result = indentation + "Begin Object"
+ (entity.Class.path ? ` Class=${entity.Class.path}` : "")
+ (entity.Name ? ` Name=${this.doWriteValue(entity.Name, insideString)}` : "")
+ "\n"
+ super.doWrite(
entity,
insideString,
moreIndentation,
wrap,
attributeSeparator,
true,
attributeValueConjunctionSign,
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
)
+ entity.CustomProperties.map(pin =>
this.attributeSeparator
+ " CustomProperties "
+ moreIndentation
+ attributeKeyPrinter("CustomProperties ")
+ SerializerFactory.getSerializer(PinEntity).doWrite(pin, insideString)
)
.join("")
+ "\nEnd Object\n"
+ indentation + "End Object"
return result
}
}

View File

@@ -12,15 +12,20 @@ import Utility from "../Utility.js"
/** @template {AnyValue} T */
export default class Serializer {
/** @type {(v: String) => String} */
static bracketsWrapped = (v => `(${v})`)
/** @type {(v: String) => String} */
static same = v => v
/** @type {(entity: AnyValue, serialized: String) => String} */
static notWrapped = (entity, serialized) => serialized
/** @type {(entity: AnyValue, serialized: String) => String} */
static bracketsWrapped = (entity, serialized) => `(${serialized})`
/** @param {AnyValueConstructor} entityType */
constructor(
entityType,
wrap = Serializer.same,
/** @type {(entity: T, serialized: String) => String} */
wrap = (entity, serialized) => serialized,
attributeSeparator = ",",
trailingSeparator = false,
attributeValueConjunctionSign = "=",
@@ -67,7 +72,8 @@ export default class Serializer {
*/
doWrite(
entity,
insideString,
insideString = false,
indentation = "",
wrap = this.wrap,
attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator,
@@ -95,7 +101,8 @@ export default class Serializer {
result += this.doWrite(
value,
insideString,
Serializer.same,
indentation,
Serializer.notWrapped,
attributeSeparator,
false,
attributeValueConjunctionSign,
@@ -105,13 +112,17 @@ export default class Serializer {
)
continue
}
result +=
attributeKeyPrinter(keyValue)
+ this.attributeValueConjunctionSign
const keyPrinted = attributeKeyPrinter(keyValue)
const indentationPrinted = attributeSeparator.includes("\n") ? indentation : ""
result += (
keyPrinted.length
? (indentationPrinted + keyPrinted + this.attributeValueConjunctionSign)
: ""
)
+ (
isSerialized
? `"${this.doWriteValue(value, true)}"`
: this.doWriteValue(value, insideString)
? `"${this.doWriteValue(value, true, indentation)}"`
: this.doWriteValue(value, insideString, indentation)
)
}
}
@@ -119,11 +130,11 @@ export default class Serializer {
// append separator at the end if asked and there was printed content
result += attributeSeparator
}
return wrap(result, entity.constructor)
return wrap(entity, result)
}
/** @param {Boolean} insideString */
doWriteValue(value, insideString) {
doWriteValue(value, insideString, indentation = "") {
const type = Utility.getType(value)
// @ts-expect-error
const serializer = SerializerFactory.getSerializer(type)
@@ -133,10 +144,7 @@ export default class Serializer {
+ "check initializeSerializerFactory.js"
)
}
return serializer.doWrite(
value,
insideString
)
return serializer.doWrite(value, insideString, indentation)
}
showProperty(entity, key) {
@@ -147,7 +155,7 @@ export default class Serializer {
if (attribute.ignored) {
return false
}
return !Utility.equals(attribute.value, value) || attribute.showDefault
return !Utility.equals(attribute.default, value) || attribute.showDefault
}
return true
}

View File

@@ -21,7 +21,7 @@ export default class ToStringSerializer extends Serializer {
* @param {T} entity
* @param {Boolean} insideString
*/
doWrite(entity, insideString) {
doWrite(entity, insideString, indentation = "") {
return !insideString && entity.constructor === String
? `"${Utility.escapeString(entity.toString())}"` // String will have quotes if not inside a string already
: Utility.escapeString(entity.toString())

View File

@@ -121,7 +121,7 @@ export default function initializeSerializerFactory() {
SerializerFactory.registerSerializer(
InvariantTextEntity,
new Serializer(InvariantTextEntity, v => `${InvariantTextEntity.lookbehind}(${v})`, ", ", false, "", _ => "")
new Serializer(InvariantTextEntity, (entity, v) => `${InvariantTextEntity.lookbehind}(${v})`, ", ", false, "", _ => "")
)
SerializerFactory.registerSerializer(
@@ -136,7 +136,7 @@ export default function initializeSerializerFactory() {
SerializerFactory.registerSerializer(
LocalizedTextEntity,
new Serializer(LocalizedTextEntity, v => `${LocalizedTextEntity.lookbehind}(${v})`, ", ", false, "", _ => "")
new Serializer(LocalizedTextEntity, (entity, v) => `${LocalizedTextEntity.lookbehind}(${v})`, ", ", false, "", _ => "")
)
SerializerFactory.registerSerializer(
@@ -173,12 +173,12 @@ export default function initializeSerializerFactory() {
SerializerFactory.registerSerializer(
PinEntity,
new Serializer(PinEntity, v => `${PinEntity.lookbehind} (${v})`, ",", true)
new Serializer(PinEntity, (entity, v) => `${PinEntity.lookbehind} (${v})`, ",", true)
)
SerializerFactory.registerSerializer(
PinReferenceEntity,
new Serializer(PinReferenceEntity, Serializer.same, " ", false, "", _ => "")
new Serializer(PinReferenceEntity, undefined, " ", false, "", _ => "")
)
SerializerFactory.registerSerializer(
@@ -239,7 +239,7 @@ export default function initializeSerializerFactory() {
SerializerFactory.registerSerializer(
UnknownKeysEntity,
new Serializer(UnknownKeysEntity, (string, entity) => `${entity.lookbehind ?? ""}(${string})`)
new Serializer(UnknownKeysEntity, (entity, string) => `${entity.lookbehind ?? ""}(${string})`)
)
SerializerFactory.registerSerializer(