Simplify attribute prefix

This commit is contained in:
barsdeveloper
2023-04-09 13:58:17 +02:00
parent 53d79a16f5
commit ce1f875f46
9 changed files with 37 additions and 90 deletions

View File

@@ -27,11 +27,10 @@ describe("Entity initialization", () => {
new Serializer( new Serializer(
Entity2, Entity2,
v => `{\n${v}\n}`, v => `{\n${v}\n}`,
" ",
"\n", "\n",
false, false,
": ", ": ",
undefined k => ` ${k}`
) )
) )
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
@@ -39,11 +38,9 @@ describe("Entity initialization", () => {
new Serializer( new Serializer(
Entity1, Entity1,
v => `Entity1(${v})`, v => `Entity1(${v})`,
"",
", ", ", ",
false, false,
"=", "=",
undefined
) )
) )
}) })
@@ -151,11 +148,10 @@ describe("Entity initialization", () => {
new Serializer( new Serializer(
Entity3, Entity3,
v => `[[\n${v}\n]]`, v => `[[\n${v}\n]]`,
" ",
"\n", "\n",
false, false,
": ", ": ",
undefined k => ` ${k}`
) )
) )
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
@@ -163,11 +159,9 @@ describe("Entity initialization", () => {
new Serializer( new Serializer(
Entity1, Entity1,
v => `Entity1(${v})`, v => `Entity1(${v})`,
"",
", ", ", ",
false, false,
"=", "=",
undefined
) )
) )
}) })
@@ -294,11 +288,10 @@ describe("Entity initialization", () => {
new Serializer( new Serializer(
Entity4, Entity4,
v => `Begin\n${v}\nEnd`, v => `Begin\n${v}\nEnd`,
" ",
"\n", "\n",
false, false,
" => ", " => ",
k => `\${${k}}` k => ` \${${k}}`
) )
) )
}) })

55
dist/ueblueprint.js vendored
View File

@@ -642,7 +642,7 @@ class Utility {
return false return false
} }
/** /**
* @param {null | AnyValue | TypeInformation} value * @param {null | AnyValue | TypeInformation} value
* @returns {AnyValueConstructor} * @returns {AnyValueConstructor}
*/ */
@@ -785,16 +785,6 @@ class Utility {
.join(" ") .join(" ")
} }
/** @param {String} value */
static encodeKeyName(value) {
return value.replaceAll(".", "$")
}
/** @param {String} value */
static decodeKeyName(value) {
return value.replaceAll("$", ".")
}
/** @param {String} value */ /** @param {String} value */
static getIdFromReference(value) { static getIdFromReference(value) {
return value return value
@@ -903,7 +893,7 @@ class Utility {
/** /**
* @template {AnyValue} T * @template {AnyValue} T
* @typedef {(new () => T) | EntityConstructor | StringConstructor | NumberConstructor | BigIntConstructor * @typedef {(new () => T) | EntityConstructor | StringConstructor | NumberConstructor | BigIntConstructor
* | BooleanConstructor | ArrayConstructor} AnyValueConstructor * | BooleanConstructor | ArrayConstructor} AnyValueConstructor
*/ */
@@ -3972,24 +3962,22 @@ class Grammar {
/** @template {AnyValue} T */ /** @template {AnyValue} T */
class Serializer { class Serializer {
/** @type {(v: String, entityType: AnyValueConstructor) => String} */
static bracketsWrapped = ((v, entityType) => `(${v})`)
/** @type {(v: String) => String} */ /** @type {(v: String) => String} */
static same = (v => v) static bracketsWrapped = (v => `(${v})`)
/** @type {(v: String) => String} */
static same = v => v
/** @param {AnyValueConstructor} entityType */ /** @param {AnyValueConstructor} entityType */
constructor( constructor(
entityType, entityType,
wrap = Serializer.same, wrap = Serializer.same,
attributePrefix = "",
attributeSeparator = ",", attributeSeparator = ",",
trailingSeparator = false, trailingSeparator = false,
attributeValueConjunctionSign = "=", attributeValueConjunctionSign = "=",
attributeKeyPrinter = k => k attributeKeyPrinter = Serializer.same
) { ) {
this.entityType = entityType; this.entityType = entityType;
this.wrap = wrap; this.wrap = wrap;
this.attributePrefix = attributePrefix;
this.attributeSeparator = attributeSeparator; this.attributeSeparator = attributeSeparator;
this.trailingSeparator = trailingSeparator; this.trailingSeparator = trailingSeparator;
this.attributeValueConjunctionSign = attributeValueConjunctionSign; this.attributeValueConjunctionSign = attributeValueConjunctionSign;
@@ -4010,7 +3998,6 @@ class Serializer {
} }
/** /**
* @protected
* @param {String} value * @param {String} value
* @returns {T} * @returns {T}
*/ */
@@ -4024,7 +4011,6 @@ class Serializer {
} }
/** /**
* @protected
* @param {T} entity * @param {T} entity
* @param {Boolean} insideString * @param {Boolean} insideString
* @returns {String} * @returns {String}
@@ -4033,7 +4019,6 @@ class Serializer {
entity, entity,
insideString, insideString,
wrap = this.wrap, wrap = this.wrap,
attributePrefix = this.attributePrefix,
attributeSeparator = this.attributeSeparator, attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator, trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign, attributeValueConjunctionSign = this.attributeValueConjunctionSign,
@@ -4060,7 +4045,6 @@ class Serializer {
value, value,
insideString, insideString,
Serializer.same, Serializer.same,
attributePrefix,
attributeSeparator, attributeSeparator,
false, false,
attributeValueConjunctionSign, attributeValueConjunctionSign,
@@ -4071,8 +4055,7 @@ class Serializer {
continue continue
} }
result += result +=
attributePrefix attributeKeyPrinter(key)
+ attributeKeyPrinter(key)
+ this.attributeValueConjunctionSign + this.attributeValueConjunctionSign
+ ( + (
isSerialized isSerialized
@@ -4088,10 +4071,7 @@ class Serializer {
return wrap(result, entity.constructor) return wrap(result, entity.constructor)
} }
/** /** @param {Boolean} insideString */
* @protected
* @param {Boolean} insideString
*/
doWriteValue(value, insideString) { doWriteValue(value, insideString) {
const type = Utility.getType(value); const type = Utility.getType(value);
// @ts-expect-error // @ts-expect-error
@@ -4125,7 +4105,7 @@ class Serializer {
class ObjectSerializer extends Serializer { class ObjectSerializer extends Serializer {
constructor() { constructor() {
super(ObjectEntity, undefined, " ", "\n", false); super(ObjectEntity, undefined, "\n", false, undefined, k => ` ${k}`);
} }
showProperty(entity, key) { showProperty(entity, key) {
@@ -4161,7 +4141,6 @@ class ObjectSerializer extends Serializer {
} }
/** /**
* @protected
* @param {ObjectEntity} entity * @param {ObjectEntity} entity
* @param {Boolean} insideString * @param {Boolean} insideString
* @returns {String} * @returns {String}
@@ -4170,7 +4149,6 @@ class ObjectSerializer extends Serializer {
entity, entity,
insideString, insideString,
wrap = this.wrap, wrap = this.wrap,
attributePrefix = this.attributePrefix,
attributeSeparator = this.attributeSeparator, attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator, trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign, attributeValueConjunctionSign = this.attributeValueConjunctionSign,
@@ -4183,9 +4161,8 @@ class ObjectSerializer extends Serializer {
+ super.doWrite(entity, insideString) + super.doWrite(entity, insideString)
+ entity.CustomProperties.map(pin => + entity.CustomProperties.map(pin =>
this.attributeSeparator this.attributeSeparator
+ this.attributePrefix + " CustomProperties "
+ "CustomProperties " + SerializerFactory.getSerializer(PinEntity).doWrite(pin, insideString)
+ SerializerFactory.getSerializer(PinEntity).write(pin)
) )
.join("") .join("")
+ "\nEnd Object\n"; + "\nEnd Object\n";
@@ -9411,7 +9388,7 @@ class VectorPinTemplate extends INumericPinTemplate {
* @typedef {import("lit").CSSResult} CSSResult * @typedef {import("lit").CSSResult} CSSResult
* @typedef {typeof PinElement} PinElementConstructor * @typedef {typeof PinElement} PinElementConstructor
*/ */
/** /**
* @template T * @template T
* @typedef {import("parsimmon").Success<T>} Success * @typedef {import("parsimmon").Success<T>} Success
*/ */
@@ -10227,7 +10204,7 @@ function initializeSerializerFactory() {
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
InvariantTextEntity, InvariantTextEntity,
new Serializer(InvariantTextEntity, v => `${InvariantTextEntity.lookbehind}(${v})`, "", ", ", false, "", _ => "") new Serializer(InvariantTextEntity, v => `${InvariantTextEntity.lookbehind}(${v})`, ", ", false, "", _ => "")
); );
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
@@ -10242,7 +10219,7 @@ function initializeSerializerFactory() {
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
LocalizedTextEntity, LocalizedTextEntity,
new Serializer(LocalizedTextEntity, v => `${LocalizedTextEntity.lookbehind}(${v})`, "", ", ", false, "", _ => "") new Serializer(LocalizedTextEntity, v => `${LocalizedTextEntity.lookbehind}(${v})`, ", ", false, "", _ => "")
); );
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
@@ -10279,12 +10256,12 @@ function initializeSerializerFactory() {
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
PinEntity, PinEntity,
new Serializer(PinEntity, v => `${PinEntity.lookbehind} (${v})`, "", ",", true) new Serializer(PinEntity, v => `${PinEntity.lookbehind} (${v})`, ",", true)
); );
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(
PinReferenceEntity, PinReferenceEntity,
new Serializer(PinReferenceEntity, v => v, "", " ", false, "", _ => "") new Serializer(PinReferenceEntity, Serializer.same, " ", false, "", _ => "")
); );
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(

File diff suppressed because one or more lines are too long

View File

@@ -198,7 +198,7 @@ export default class Utility {
return false return false
} }
/** /**
* @param {null | AnyValue | TypeInformation} value * @param {null | AnyValue | TypeInformation} value
* @returns {AnyValueConstructor} * @returns {AnyValueConstructor}
*/ */
@@ -341,16 +341,6 @@ export default class Utility {
.join(" ") .join(" ")
} }
/** @param {String} value */
static encodeKeyName(value) {
return value.replaceAll(".", "$")
}
/** @param {String} value */
static decodeKeyName(value) {
return value.replaceAll("$", ".")
}
/** @param {String} value */ /** @param {String} value */
static getIdFromReference(value) { static getIdFromReference(value) {
return value return value

View File

@@ -27,7 +27,7 @@ import VectorPinTemplate from "../template/pin/VectorPinTemplate.js"
* @typedef {import("lit").CSSResult} CSSResult * @typedef {import("lit").CSSResult} CSSResult
* @typedef {typeof PinElement} PinElementConstructor * @typedef {typeof PinElement} PinElementConstructor
*/ */
/** /**
* @template T * @template T
* @typedef {import("parsimmon").Success<T>} Success * @typedef {import("parsimmon").Success<T>} Success
*/ */

View File

@@ -26,7 +26,7 @@ import Utility from "../Utility.js"
/** /**
* @template {AnyValue} T * @template {AnyValue} T
* @typedef {(new () => T) | EntityConstructor | StringConstructor | NumberConstructor | BigIntConstructor * @typedef {(new () => T) | EntityConstructor | StringConstructor | NumberConstructor | BigIntConstructor
* | BooleanConstructor | ArrayConstructor} AnyValueConstructor * | BooleanConstructor | ArrayConstructor} AnyValueConstructor
*/ */

View File

@@ -7,7 +7,7 @@ import SerializerFactory from "./SerializerFactory.js"
export default class ObjectSerializer extends Serializer { export default class ObjectSerializer extends Serializer {
constructor() { constructor() {
super(ObjectEntity, undefined, " ", "\n", false) super(ObjectEntity, undefined, "\n", false, undefined, k => ` ${k}`)
} }
showProperty(entity, key) { showProperty(entity, key) {
@@ -43,7 +43,6 @@ export default class ObjectSerializer extends Serializer {
} }
/** /**
* @protected
* @param {ObjectEntity} entity * @param {ObjectEntity} entity
* @param {Boolean} insideString * @param {Boolean} insideString
* @returns {String} * @returns {String}
@@ -52,7 +51,6 @@ export default class ObjectSerializer extends Serializer {
entity, entity,
insideString, insideString,
wrap = this.wrap, wrap = this.wrap,
attributePrefix = this.attributePrefix,
attributeSeparator = this.attributeSeparator, attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator, trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign, attributeValueConjunctionSign = this.attributeValueConjunctionSign,
@@ -65,9 +63,8 @@ export default class ObjectSerializer extends Serializer {
+ super.doWrite(entity, insideString) + super.doWrite(entity, insideString)
+ entity.CustomProperties.map(pin => + entity.CustomProperties.map(pin =>
this.attributeSeparator this.attributeSeparator
+ this.attributePrefix + " CustomProperties "
+ "CustomProperties " + SerializerFactory.getSerializer(PinEntity).doWrite(pin, insideString)
+ SerializerFactory.getSerializer(PinEntity).write(pin)
) )
.join("") .join("")
+ "\nEnd Object\n" + "\nEnd Object\n"

View File

@@ -11,24 +11,22 @@ import Utility from "../Utility.js"
/** @template {AnyValue} T */ /** @template {AnyValue} T */
export default class Serializer { export default class Serializer {
/** @type {(v: String, entityType: AnyValueConstructor) => String} */
static bracketsWrapped = ((v, entityType) => `(${v})`)
/** @type {(v: String) => String} */ /** @type {(v: String) => String} */
static same = (v => v) static bracketsWrapped = (v => `(${v})`)
/** @type {(v: String) => String} */
static same = v => v
/** @param {AnyValueConstructor} entityType */ /** @param {AnyValueConstructor} entityType */
constructor( constructor(
entityType, entityType,
wrap = Serializer.same, wrap = Serializer.same,
attributePrefix = "",
attributeSeparator = ",", attributeSeparator = ",",
trailingSeparator = false, trailingSeparator = false,
attributeValueConjunctionSign = "=", attributeValueConjunctionSign = "=",
attributeKeyPrinter = k => k attributeKeyPrinter = Serializer.same
) { ) {
this.entityType = entityType this.entityType = entityType
this.wrap = wrap this.wrap = wrap
this.attributePrefix = attributePrefix
this.attributeSeparator = attributeSeparator this.attributeSeparator = attributeSeparator
this.trailingSeparator = trailingSeparator this.trailingSeparator = trailingSeparator
this.attributeValueConjunctionSign = attributeValueConjunctionSign this.attributeValueConjunctionSign = attributeValueConjunctionSign
@@ -49,7 +47,6 @@ export default class Serializer {
} }
/** /**
* @protected
* @param {String} value * @param {String} value
* @returns {T} * @returns {T}
*/ */
@@ -63,7 +60,6 @@ export default class Serializer {
} }
/** /**
* @protected
* @param {T} entity * @param {T} entity
* @param {Boolean} insideString * @param {Boolean} insideString
* @returns {String} * @returns {String}
@@ -72,7 +68,6 @@ export default class Serializer {
entity, entity,
insideString, insideString,
wrap = this.wrap, wrap = this.wrap,
attributePrefix = this.attributePrefix,
attributeSeparator = this.attributeSeparator, attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator, trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign, attributeValueConjunctionSign = this.attributeValueConjunctionSign,
@@ -99,7 +94,6 @@ export default class Serializer {
value, value,
insideString, insideString,
Serializer.same, Serializer.same,
attributePrefix,
attributeSeparator, attributeSeparator,
false, false,
attributeValueConjunctionSign, attributeValueConjunctionSign,
@@ -110,8 +104,7 @@ export default class Serializer {
continue continue
} }
result += result +=
attributePrefix attributeKeyPrinter(key)
+ attributeKeyPrinter(key)
+ this.attributeValueConjunctionSign + this.attributeValueConjunctionSign
+ ( + (
isSerialized isSerialized
@@ -127,10 +120,7 @@ export default class Serializer {
return wrap(result, entity.constructor) return wrap(result, entity.constructor)
} }
/** /** @param {Boolean} insideString */
* @protected
* @param {Boolean} insideString
*/
doWriteValue(value, insideString) { doWriteValue(value, insideString) {
const type = Utility.getType(value) const type = Utility.getType(value)
// @ts-expect-error // @ts-expect-error

View File

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