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

@@ -39,8 +39,11 @@ export default class Utility {
* @returns {any} The value in from corresponding to the keys or defaultValue otherwise
*/
static objectGet(target, keys, defaultValue = undefined) {
if (target === undefined) {
return undefined
}
if (!(keys instanceof Array)) {
console.error("Expected keys to be an array.")
throw new TypeError("Expected keys to be an array.")
}
if (keys.length == 0 || !(keys[0] in target) || target[keys[0]] === undefined) {
return defaultValue
@@ -61,7 +64,7 @@ export default class Utility {
*/
static objectSet(target, keys, value, create = false, defaultDictType = Object) {
if (!(keys instanceof Array)) {
console.error("Expected keys to be an array.")
throw new TypeError("Expected keys to be an array.")
}
if (keys.length == 1) {
if (create || keys[0] in target || target[keys[0]] === undefined) {

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
}
}

View File

@@ -20,8 +20,7 @@ export default class GeneralSerializer extends ISerializer {
let grammar = Grammar.getGrammarForType(ISerializer.grammar, this.entityType)
const parseResult = grammar.parse(value)
if (!parseResult.status) {
console.error("Error when trying to parse the entity " + this.entityType.prototype.constructor.name)
return parseResult
throw new Error(`Error when trying to parse the entity ${this.entityType.prototype.constructor.name}.`)
}
return parseResult.value
}

View File

@@ -102,7 +102,8 @@ export default class Grammar {
let result = new entityType()
attributes.forEach(attributeSetter => attributeSetter(result))
return result
})
}
)
/* --- General --- */
@@ -194,7 +195,8 @@ export default class Grammar {
r.String,
r.Guid,
r.Reference,
r.LocalizedText)
r.LocalizedText
)
PinReference = r => P.seqMap(
r.PathSymbol, // Goes into objectNAme
@@ -239,9 +241,9 @@ export default class Grammar {
.sepBy1(P.whitespace),
P.seq(r.MultilineWhitespace, P.string("End"), P.whitespace, P.string("Object")),
(_, attributes, __) => {
let result = new ObjectEntity()
attributes.forEach(attributeSetter => attributeSetter(result))
return result
let values = {}
attributes.forEach(attributeSetter => attributeSetter(values))
return new ObjectEntity(values)
}
)

View File

@@ -25,8 +25,7 @@ export default class ObjectSerializer extends ISerializer {
read(value) {
const parseResult = ISerializer.grammar.Object.parse(value)
if (!parseResult.status) {
console.error("Error when trying to parse the object.")
return parseResult
throw new Error("Error when trying to parse the object.")
}
return parseResult.value
}