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

55
dist/ueblueprint.js vendored
View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -121,7 +121,7 @@ export default function initializeSerializerFactory() {
SerializerFactory.registerSerializer(
InvariantTextEntity,
new Serializer(InvariantTextEntity, v => `${InvariantTextEntity.lookbehind}(${v})`, "", ", ", false, "", _ => "")
new Serializer(InvariantTextEntity, 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, 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, v => `${PinEntity.lookbehind} (${v})`, ",", true)
)
SerializerFactory.registerSerializer(
PinReferenceEntity,
new Serializer(PinReferenceEntity, v => v, "", " ", false, "", _ => "")
new Serializer(PinReferenceEntity, Serializer.same, " ", false, "", _ => "")
)
SerializerFactory.registerSerializer(