Type system fixes

This commit is contained in:
barsdeveloper
2022-04-11 23:35:28 +02:00
parent 4b045b4c70
commit f71edf1a3c
7 changed files with 55 additions and 57 deletions

View File

@@ -7,13 +7,13 @@ export default class IEntity {
static attributes = {}
constructor(options) {
constructor(values) {
// @ts-expect-error
const attributes = this.constructor.attributes
if (options.constructor !== Object && Object.getOwnPropertyNames(attributes).length == 1) {
if (values.constructor !== Object && Object.getOwnPropertyNames(attributes).length == 1) {
// Where there is just one attribute, option can be the value of that attribute
options = {
[Object.getOwnPropertyNames(attributes)[0]]: options
values = {
[Object.getOwnPropertyNames(attributes)[0]]: values
}
}
/**
@@ -21,17 +21,21 @@ export default class IEntity {
* @param {Object} target
* @param {Object} properties
*/
const defineAllAttributes = (prefix, target, properties) => {
const defineAllAttributes = (prefix, target, properties, values) => {
let fullKey = prefix.concat("")
const last = fullKey.length - 1
for (let property of Object.getOwnPropertyNames(properties)) {
fullKey[last] = property
let defaultValue = properties[property]
const defaultType = (defaultValue instanceof Function) ? defaultValue : defaultValue?.constructor
const defaultType = (defaultValue instanceof TypeInitialization)
? defaultValue.type
: (defaultValue instanceof Function)
? defaultValue
: defaultValue?.constructor
// Not instanceof because all objects are instenceof Object, exact match needed
if (defaultType === Object) {
target[property] = {}
defineAllAttributes(fullKey, target[property], properties[property])
defineAllAttributes(fullKey, target[property], properties[property], values[property])
continue
}
/*
@@ -41,14 +45,14 @@ export default class IEntity {
* - A type: the default value will be default constructed object without arguments.
* - A proper value.
*/
const value = Utility.objectGet(options, fullKey)
const value = Utility.objectGet(values, fullKey)
if (value !== undefined) {
target[property] = TypeInitialization.sanitize(value, defaultType)
continue
}
if (defaultValue instanceof TypeInitialization) {
if (!defaultValue.showDefault) {
target[property] = undefined // to preserve the order
target[property] = undefined // Declare undefined to preserve the order or attributes
continue
}
defaultValue = defaultValue.value
@@ -63,10 +67,6 @@ export default class IEntity {
target[property] = TypeInitialization.sanitize(defaultValue, defaultType)
}
}
defineAllAttributes([], this, attributes)
}
empty() {
return true
defineAllAttributes([], this, attributes, values)
}
}

View File

@@ -14,8 +14,4 @@ export default class ObjectReferenceEntity extends IEntity {
/** @type {String} */ this.type
/** @type {String} */ this.path
}
empty() {
return false
}
}