mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-28 03:24:43 +08:00
Calculated serialization type
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import SerializedType from "./SerializedType"
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*/
|
||||
@@ -31,13 +29,22 @@ export default class TypeInitialization {
|
||||
this.#value = v
|
||||
}
|
||||
|
||||
/** @type {Boolean} */
|
||||
#serialized
|
||||
get serialized() {
|
||||
return this.#serialized
|
||||
}
|
||||
set serialized(v) {
|
||||
this.#serialized = v
|
||||
}
|
||||
|
||||
static sanitize(value, targetType) {
|
||||
if (targetType === undefined) {
|
||||
targetType = value?.constructor
|
||||
}
|
||||
if (
|
||||
targetType
|
||||
&& targetType !== SerializedType
|
||||
// value is not of type targetType
|
||||
&& !(value?.constructor === targetType || value instanceof targetType)
|
||||
) {
|
||||
value = new targetType(value)
|
||||
@@ -49,22 +56,25 @@ export default class TypeInitialization {
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {(new () => T) | SerializedType | StringConstructor | NumberConstructor | BooleanConstructor} Constructor
|
||||
* @typedef {(new () => T) | StringConstructor | NumberConstructor | BooleanConstructor} Constructor
|
||||
* @param {Constructor|Array<Constructor>} type
|
||||
* @param {Boolean} showDefault
|
||||
* @param {any} value
|
||||
* @param {Boolean} serialized
|
||||
*/
|
||||
constructor(type, showDefault = true, value = undefined) {
|
||||
constructor(type, showDefault = true, value = undefined, serialized = false) {
|
||||
if (value === undefined) {
|
||||
if (type instanceof Array) {
|
||||
value = []
|
||||
} else if (type instanceof SerializedType) {
|
||||
} else if (serialized) {
|
||||
value = ""
|
||||
} else {
|
||||
value = TypeInitialization.sanitize(new type())
|
||||
}
|
||||
}
|
||||
this.#showDefault = showDefault
|
||||
this.#type = type
|
||||
this.#showDefault = showDefault
|
||||
this.#value = value
|
||||
this.#serialized = serialized
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user