Inline attributes semantics and tests

This commit is contained in:
barsdeveloper
2023-04-09 10:41:50 +02:00
parent 0a40f0b33c
commit 53d79a16f5
22 changed files with 508 additions and 382 deletions

View File

@@ -47,7 +47,7 @@ export default class Configuration {
static gridShrinkThreshold = 4 // exceding size factor threshold to cause a shrink event
static gridSize = 16 // px
static hexColorRegex = /^\s*#(?<r>[0-9a-fA-F]{2})(?<g>[0-9a-fA-F]{2})(?<b>[0-9a-fA-F]{2})([0-9a-fA-F]{2})?|#(?<rs>[0-9a-fA-F])(?<gs>[0-9a-fA-F])(?<bs>[0-9a-fA-F])\s*$/
static keysSeparator = "+"
static keysSeparator = /[\.\(\)]/
static knotOffset = [-26, -16]
static linkCurveHeight = 15 // px
static linkCurveWidth = 80 // px

View File

@@ -111,7 +111,7 @@ export default class PinElement extends IElement {
* @return {new () => PinTemplate}
*/
static getTypeTemplate(pinEntity) {
if (pinEntity.PinType$bIsReference && !pinEntity.PinType$bIsConst) {
if (pinEntity.PinType.bIsReference && !pinEntity.PinType.bIsConst) {
return PinElement.#inputPinTemplates["MUTABLE_REFERENCE"]
}
if (pinEntity.getType() === "exec") {

View File

@@ -4,13 +4,9 @@ import UnionType from "./UnionType.js"
import Utility from "../Utility.js"
/**
* @typedef {(entity: IEntity) => AnyValue} ValueSupplier
* @typedef {IEntity | String | Number | BigInt | Boolean} AnySimpleValue
* @typedef {AnySimpleValue | AnySimpleValue[]} AnyValue
* @typedef {{
* [key: String]: AttributeInformation
* }} AttributeDeclarations
* @typedef {typeof IEntity} EntityConstructor
* @typedef {(entity: IEntity) => AnyValue} ValueSupplier
* @typedef {{
* type?: AnyValueConstructor<AnyValue> | AnyValueConstructor<AnyValue>[] | UnionType | ComputedType,
* default?: AnyValue | ValueSupplier,
@@ -22,6 +18,10 @@ import Utility from "../Utility.js"
* inlined?: Boolean,
* predicate?: (value: AnyValue) => Boolean,
* }} AttributeInformation
* @typedef {{
* [key: String]: AttributeInformation
* }} AttributeDeclarations
* @typedef {typeof IEntity} EntityConstructor
*/
/**
@@ -45,7 +45,8 @@ export default class IEntity {
}
constructor(values = {}, suppressWarns = false) {
const attributes = /** @type {typeof IEntity} */(this.constructor).attributes
const Self = /** @type {EntityConstructor} */(this.constructor)
const attributes = Self.attributes
if (values.constructor !== Object && Object.keys(attributes).length === 1) {
// Where there is just one attribute, option can be the value of that attribute
values = {
@@ -57,14 +58,14 @@ export default class IEntity {
const allAttributesNames = Utility.mergeArrays(attributesNames, valuesNames)
for (let attributeName of allAttributesNames) {
let value = values[attributeName]
let attribute = /** @type {AttributeInformation} */(attributes[attributeName])
let attribute = attributes[attributeName]
if (!suppressWarns) {
if (!(attributeName in attributes)) {
const typeName = value instanceof Array ? `[${value[0].constructor.name}]` : value.constructor.name
console.warn(
`UEBlueprint: Attribute ${attributeName} (of type ${typeName}) in the serialized data is not defined in `
+ `${this.constructor.name}.attributes`
`UEBlueprint: Attribute ${attributeName} (of type ${typeName}) in the serialized data is not `
+ `defined in ${Self.name}.attributes`
)
} else if (
valuesNames.length > 0
@@ -72,8 +73,7 @@ export default class IEntity {
&& !(!attribute.showDefault || attribute.ignored)
) {
console.warn(
`UEBlueprint: ${this.constructor.name} will add attribute ${attributeName} not `
+ "defined in the serialized data"
`UEBlueprint: ${Self.name} will add attribute ${attributeName} missing from the serialized data`
)
}
}
@@ -115,8 +115,8 @@ export default class IEntity {
set(v) {
if (!attribute.predicate?.(v)) {
console.warn(
`UEBlueprint: Tried to assign attribute ${attributeName} to `
+ `${this.constructor.name} not satisfying the predicate`
`UEBlueprint: Tried to assign attribute ${attributeName} to`
+ `${Self.name} not satisfying the predicate`
)
return
}
@@ -172,7 +172,9 @@ export default class IEntity {
for (const attributeName in attributes) {
const attribute = /** @type {AttributeInformation} */(attributes[attributeName])
if (attribute.type === undefined && !(attribute.default instanceof Function)) {
attribute.type = Utility.getType(attribute.default)
attribute.type = attribute.default instanceof Array
? [Utility.getType(attribute.default[0])]
: Utility.getType(attribute.default)
}
attributes[attributeName] = {
...IEntity.defaultAttribute,

View File

@@ -1,17 +0,0 @@
/** @typedef {import("./IEntity").AnyValueConstructor<*>} AnyValueConstructor */
export default class IndexedArray {
#type
get type() {
return this.#type
}
value = []
/** @param {AnyValueConstructor} type */
constructor(type, value = []) {
this.#type = type
this.value = value
}
}

View File

@@ -414,7 +414,7 @@ export default class ObjectEntity extends IEntity {
}
getDelegatePin() {
return this.CustomProperties?.find(pin => pin.PinType$PinCategory === "delegate")
return this.CustomProperties?.find(pin => pin.PinType.PinCategory === "delegate")
}
nodeDisplayName() {

View File

@@ -3,7 +3,6 @@ import ComputedType from "./ComputedType.js"
import Configuration from "../Configuration.js"
import EnumEntity from "./EnumEntity.js"
import FormatTextEntity from "./FormatTextEntity.js"
import FunctionReferenceEntity from "./FunctionReferenceEntity.js"
import GuidEntity from "./GuidEntity.js"
import IEntity from "./IEntity.js"
import Integer64Entity from "./Integer64Entity.js"
@@ -11,13 +10,12 @@ import IntegerEntity from "./IntegerEntity.js"
import LinearColorEntity from "./LinearColorEntity.js"
import LocalizedTextEntity from "./LocalizedTextEntity.js"
import ObjectReferenceEntity from "./ObjectReferenceEntity.js"
import PathSymbolEntity from "./PathSymbolEntity.js"
import PinReferenceEntity from "./PinReferenceEntity.js"
import PinTypeEntity from "./PinTypeEntity.js"
import RotatorEntity from "./RotatorEntity.js"
import SimpleSerializationRotatorEntity from "./SimpleSerializationRotatorEntity.js"
import SimpleSerializationVector2DEntity from "./SimpleSerializationVector2DEntity.js"
import SimpleSerializationVectorEntity from "./SimpleSerializationVectorEntity.js"
import TerminalTypeEntity from "./TerminalTypeEntity.js"
import UnionType from "./UnionType.js"
import Utility from "../Utility.js"
import Vector2DEntity from "./Vector2DEntity.js"
@@ -71,40 +69,9 @@ export default class PinEntity extends IEntity {
type: String,
showDefault: false,
},
PinType$PinCategory: {
default: "",
},
PinType$PinSubCategory: {
default: "",
},
PinType$PinSubCategoryObject: {
type: ObjectReferenceEntity,
},
PinType$PinSubCategoryMemberReference: {
type: FunctionReferenceEntity,
default: null,
},
PinType$PinValueType: {
type: TerminalTypeEntity,
default: null,
},
PinType$ContainerType: {
type: PathSymbolEntity,
},
PinType$bIsReference: {
default: false,
},
PinType$bIsConst: {
default: false,
},
PinType$bIsWeakPointer: {
default: false,
},
PinType$bIsUObjectWrapper: {
default: false,
},
PinType$bSerializeAsSinglePrecisionFloat: {
default: false,
PinType: {
type: PinTypeEntity,
inlined: true,
},
LinkedTo: {
type: [PinReferenceEntity],
@@ -167,17 +134,7 @@ export default class PinEntity extends IEntity {
/** @type {LocalizedTextEntity | String} */ this.PinFriendlyName
/** @type {String} */ this.PinToolTip
/** @type {String} */ this.Direction
/** @type {String} */ this.PinType$PinCategory
/** @type {String} */ this.PinType$PinSubCategory
/** @type {ObjectReferenceEntity} */ this.PinType$PinSubCategoryObject
/** @type {FunctionReferenceEntity} */ this.PinType$PinSubCategoryMemberReference
/** @type {TerminalTypeEntity} */ this.PinType$PinValueType
/** @type {PathSymbolEntity} */ this.PinType$ContainerType
/** @type {Boolean} */ this.PinType$bIsReference
/** @type {Boolean} */ this.PinType$bIsConst
/** @type {Boolean} */ this.PinType$bIsWeakPointer
/** @type {Boolean} */ this.PinType$bIsUObjectWrapper
/** @type {Boolean} */ this.PinType$bIsUObjectWrapper
/** @type {PinTypeEntity} */ this.PinType
/** @type {PinReferenceEntity[]} */ this.LinkedTo
/** @type {T} */ this.DefaultValue
/** @type {String} */ this.AutogeneratedDefaultValue
@@ -192,12 +149,12 @@ export default class PinEntity extends IEntity {
}
getType() {
const subCategory = this.PinType$PinSubCategoryObject
if (this.PinType$PinCategory === "struct" || this.PinType$PinCategory === "object") {
const subCategory = this.PinType.PinSubCategoryObject
if (this.PinType.PinCategory === "struct" || this.PinType.PinCategory === "object") {
return subCategory.path
}
if (
this.PinType$PinCategory === "byte"
this.PinType.PinCategory === "byte"
&& (
subCategory.type === Configuration.nodeType.enum
|| subCategory.type === Configuration.nodeType.userDefinedEnum
@@ -205,7 +162,7 @@ export default class PinEntity extends IEntity {
) {
return "enum"
}
return this.PinType$PinCategory
return this.PinType.PinCategory
}
getEntityType(alternative = false) {
@@ -234,16 +191,16 @@ export default class PinEntity extends IEntity {
/** @param {PinEntity} other */
copyTypeFrom(other) {
this.PinType$PinCategory = other.PinType$PinCategory
this.PinType$PinSubCategory = other.PinType$PinSubCategory
this.PinType$PinSubCategoryObject = other.PinType$PinSubCategoryObject
this.PinType$PinSubCategoryMemberReference = other.PinType$PinSubCategoryMemberReference
this.PinType$PinValueType = other.PinType$PinValueType
this.PinType$ContainerType = other.PinType$ContainerType
this.PinType$bIsReference = other.PinType$bIsReference
this.PinType$bIsConst = other.PinType$bIsConst
this.PinType$bIsWeakPointer = other.PinType$bIsWeakPointer
this.PinType$bIsUObjectWrapper = other.PinType$bIsUObjectWrapper
this.PinType.PinCategory = other.PinType.PinCategory
this.PinType.PinSubCategory = other.PinType.PinSubCategory
this.PinType.PinSubCategoryObject = other.PinType.PinSubCategoryObject
this.PinType.PinSubCategoryMemberReference = other.PinType.PinSubCategoryMemberReference
this.PinType.PinValueType = other.PinType.PinValueType
this.PinType.ContainerType = other.PinType.ContainerType
this.PinType.bIsReference = other.PinType.bIsReference
this.PinType.bIsConst = other.PinType.bIsConst
this.PinType.bIsWeakPointer = other.PinType.bIsWeakPointer
this.PinType.bIsUObjectWrapper = other.PinType.bIsUObjectWrapper
this.PinType$bSerializeAsSinglePrecisionFloat = other.PinType$bSerializeAsSinglePrecisionFloat
}
@@ -255,7 +212,7 @@ export default class PinEntity extends IEntity {
}
isExecution() {
return this.PinType$PinCategory === "exec"
return this.PinType.PinCategory === "exec"
}
isHidden() {
@@ -313,13 +270,13 @@ export default class PinEntity extends IEntity {
}
getSubCategory() {
return this.PinType$PinSubCategoryObject.path
return this.PinType.PinSubCategoryObject.path
}
/** @return {CSSResult} */
pinColor() {
return Configuration.pinColor[this.getType()]
?? Configuration.pinColor[this.PinType$PinCategory.toLowerCase()]
?? Configuration.pinColor[this.PinType.PinCategory.toLowerCase()]
?? Configuration.pinColor["default"]
}
}

View File

@@ -1,10 +1,11 @@
import ByteEntity from "../entity/ByteEntity.js"
import Configuration from "../Configuration.js"
import EnumEntity from "../entity/EnumEntity.js"
import FormatTextEntity from "../entity/FormatTextEntity.js"
import FunctionReferenceEntity from "../entity/FunctionReferenceEntity.js"
import GuidEntity from "../entity/GuidEntity.js"
import IdentifierEntity from "../entity/IdentifierEntity.js"
import IndexedArray from "../entity/IndexedArray.js"
import IEntity from "../entity/IEntity.js"
import Integer64Entity from "../entity/Integer64Entity.js"
import IntegerEntity from "../entity/IntegerEntity.js"
import InvariantTextEntity from "../entity/InvariantTextEntity.js"
@@ -273,16 +274,44 @@ export default class Grammar {
return result
}
/**
* @param {EntityConstructor} entityType
* @param {String[]} key
* @returns {AttributeInformation}
*/
static getAttribute(entityType, key) {
let result
let type
if (entityType instanceof UnionType) {
for (let t of entityType.types) {
if (result = this.getAttribute(t, key)) {
return result
}
}
}
if (entityType instanceof IEntity.constructor) {
result = entityType.attributes[key[0]]
type = result?.type
} else if (entityType instanceof Array) {
result = entityType[key[0]]
type = result
}
if (key.length > 1) {
return this.getAttribute(type, key.slice(1))
}
return result
}
static createAttributeGrammar(entityType, valueSeparator = this.equalSeparation) {
return P.seq(
this.attributeName,
valueSeparator,
).chain(([attributeName, _1]) => {
attributeName = Utility.encodeKeyName(attributeName)
const attributeKey = attributeName.split(Configuration.keysSeparator)
return this
.grammarFor(entityType.attributes[attributeName], undefined)
.grammarFor(this.getAttribute(entityType, attributeKey))
.map(attributeValue =>
values => values[attributeName] = attributeValue
values => Utility.objectSet(values, attributeKey, attributeValue, true)
)
})
}
@@ -563,7 +592,7 @@ export default class Grammar {
})
)
static indexedArrayEntry = P.lazy(() => {
static inlinedArrayEntry = P.lazy(() => {
return P.seq(
this.symbol,
this.regexMap(
@@ -574,8 +603,7 @@ export default class Grammar {
.chain(([symbol, _1]) =>
this.grammarFor(ObjectEntity.attributes[symbol])
.map(currentValue =>
values => (values[symbol] ??= new IndexedArray(currentValue.constructor))
.value.push(currentValue)
values => (values[symbol] ??= []).push(currentValue)
)
)
})
@@ -588,7 +616,7 @@ export default class Grammar {
P.alt(
this.customProperty,
this.createAttributeGrammar(ObjectEntity),
this.indexedArrayEntry
this.inlinedArrayEntry
)
)
.map(([_0, entry]) => entry)

View File

@@ -1,5 +1,4 @@
import Grammar from "./Grammar.js"
import IndexedArray from "../entity/IndexedArray.js"
import SerializerFactory from "./SerializerFactory.js"
import Utility from "../Utility.js"
@@ -14,13 +13,13 @@ export default class Serializer {
/** @type {(v: String, entityType: AnyValueConstructor) => String} */
static bracketsWrapped = ((v, entityType) => `(${v})`)
/** @type {(v: String, entityType: AnyValueConstructor) => String} */
static notWrapped = ((v, entityType) => v)
/** @type {(v: String) => String} */
static same = (v => v)
/** @param {AnyValueConstructor} entityType */
constructor(
entityType,
wrap = Serializer.bracketsWrapped,
wrap = Serializer.same,
attributePrefix = "",
attributeSeparator = ",",
trailingSeparator = false,
@@ -85,40 +84,34 @@ export default class Serializer {
Object.keys(attributes),
Object.keys(entity)
)
let first = true
for (const key of keys) {
const value = entity[key]
if (value !== undefined && this.showProperty(entity, key)) {
const isSerialized = Utility.isSerialized(entity, key)
result += (result.length ? attributeSeparator : "")
if (first) {
first = false
} else {
result += attributeSeparator
}
if (attributes[key]?.inlined) {
result += this.doWrite(
value,
insideString,
Serializer.notWrapped,
`${attributePrefix}${key}.`,
attributeSeparator,
trailingSeparator,
attributeValueConjunctionSign,
attributeKeyPrinter
)
continue
}
if (value instanceof IndexedArray) {
result += this.doWrite(
value,
insideString,
wrap,
Serializer.same,
attributePrefix,
attributeSeparator,
trailingSeparator,
false,
attributeValueConjunctionSign,
index => `(${index})`
attributes[key].type instanceof Array
? k => attributeKeyPrinter(`${key}(${k})`)
: k => attributeKeyPrinter(`${key}.${k}`),
)
continue
}
result +=
attributePrefix
+ Utility.decodeKeyName(this.attributeKeyPrinter(key))
+ attributeKeyPrinter(key)
+ this.attributeValueConjunctionSign
+ (
isSerialized
@@ -127,7 +120,7 @@ export default class Serializer {
)
}
}
if (this.trailingSeparator && result.length) {
if (trailingSeparator && result.length) {
// append separator at the end if asked and there was printed content
result += this.attributeSeparator
}

View File

@@ -28,7 +28,7 @@ export default class SerializerFactory {
/**
* @template {AnyValue} T
* @param {new () => T} entity
* @param {new (...any) => T} entity
* @returns {Serializer<T>}
*/
static getSerializer(entity) {

View File

@@ -45,7 +45,7 @@ export default class EventNodeTemplate extends NodeTemplate {
createDelegatePinElement() {
const pin = /** @type {PinElementConstructor} */(ElementFactory.getConstructor("ueb-pin")).newObject(
this.element.getPinEntities().find(v => !v.isHidden() && v.PinType$PinCategory === "delegate"),
this.element.getPinEntities().find(v => !v.isHidden() && v.PinType.PinCategory === "delegate"),
new MinimalPinTemplate(),
this.element
)
@@ -55,7 +55,7 @@ export default class EventNodeTemplate extends NodeTemplate {
createPinElements() {
return this.element.getPinEntities()
.filter(v => !v.isHidden() && v.PinType$PinCategory !== "delegate")
.filter(v => !v.isHidden() && v.PinType.PinCategory !== "delegate")
.map(pinEntity => /** @type {PinElementConstructor} */(ElementFactory.getConstructor("ueb-pin"))
.newObject(pinEntity, undefined, this.element)
)

View File

@@ -76,12 +76,12 @@ export default class PinTemplate extends ITemplate {
}
renderIcon() {
switch (this.element.entity.PinType$ContainerType.toString()) {
switch (this.element.entity.PinType.ContainerType.toString()) {
case "Array": return SVGIcon.array
case "Set": return SVGIcon.set
case "Map": return SVGIcon.map
}
if (this.element.entity.PinType$PinCategory.toLocaleLowerCase() === "delegate") {
if (this.element.entity.PinType.PinCategory.toLocaleLowerCase() === "delegate") {
return SVGIcon.delegate
}
return SVGIcon.genericPin