property renaming
This commit is contained in:
parent
1e3d8588ac
commit
548c206afb
5 changed files with 19 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
|||
import { Serde } from '../decorator'
|
||||
import { Constructor, staticImplements } from '../utils'
|
||||
import { GenericVisitor } from './generic'
|
||||
import { Deserialize, Deserializer } from './interface'
|
||||
|
@ -10,6 +11,7 @@ export function deserialize<T, C extends Constructor>(constructor: C) {
|
|||
return deserializer.deserializeAny(visitor)
|
||||
}
|
||||
}
|
||||
Deserializable[Serde] = (constructor as any)[Serde]
|
||||
|
||||
return Deserializable
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ import { ContainerOptions, PropertyOptions, SerdeOptions } from "./options"
|
|||
|
||||
export const Serde = Symbol('Serde')
|
||||
|
||||
|
||||
function decorateContainer(options: ContainerOptions, constructor: any) {
|
||||
if (constructor[Serde] == null) {
|
||||
constructor[Serde] = new SerdeOptions(constructor, options)
|
||||
|
@ -14,11 +13,11 @@ function decorateContainer(options: ContainerOptions, constructor: any) {
|
|||
}
|
||||
|
||||
function decorateProperty(options: PropertyOptions, target: any, property: PropertyKey) {
|
||||
let constructor
|
||||
let constructor: any
|
||||
if (typeof target === 'function') {
|
||||
constructor = target.constructor
|
||||
} else {
|
||||
constructor = target
|
||||
} else {
|
||||
constructor = target.constructor
|
||||
}
|
||||
|
||||
if (constructor[Serde] == null) {
|
||||
|
@ -26,6 +25,7 @@ function decorateProperty(options: PropertyOptions, target: any, property: Prope
|
|||
}
|
||||
|
||||
constructor[Serde].properties.set(property, options)
|
||||
return target
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
import { Serde } from '../decorator'
|
||||
import { SerdeOptions } from '../options'
|
||||
import { SerdeOptions, Stage } from '../options'
|
||||
import { ifNull, isFunction, isIterable, Nullable, orElse } from '../utils'
|
||||
import { IterableSerializer, ObjectSerializer, Serializable, Serializer } from './interface'
|
||||
|
||||
const unhandledType = (serializer: any, value: any) => new TypeError(`'${serializer.constructor.name}' has no method for value type '${typeof value}'`)
|
||||
|
||||
function serializeEntries<T, K extends Serializable, V extends Serializable, E extends Iterable<[K, V]>>(serializer: ObjectSerializer<T>, value: E) {
|
||||
function serializeEntries<T, K extends Serializable, V extends Serializable, E extends Iterable<[K, V]>>(serializer: ObjectSerializer<T>, value: E, options?: SerdeOptions) {
|
||||
let state
|
||||
|
||||
for (const [key, val] of value) {
|
||||
state = serializer.serializeKey(key)
|
||||
const name = options?.getPropertyName(key as string, Stage.Serialize) ?? key
|
||||
state = serializer.serializeKey(name)
|
||||
state = serializer.serializeValue(val)
|
||||
}
|
||||
|
||||
|
@ -17,7 +18,7 @@ function serializeEntries<T, K extends Serializable, V extends Serializable, E e
|
|||
}
|
||||
|
||||
function serializeObject<T, K extends PropertyKey, V extends Serializable, R extends Record<K, V>>(serializer: ObjectSerializer<T>, value: R, options?: SerdeOptions) {
|
||||
return serializeEntries(serializer, Object.entries(value) as Iterable<[K, V]>)
|
||||
return serializeEntries(serializer, Object.entries(value) as Iterable<[K, V]>, options)
|
||||
}
|
||||
|
||||
function serializeIter<T, V extends Iterable<any>>(serializer: IterableSerializer<T>, value: V) {
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import { Serde } from '../decorator'
|
||||
import { Constructor } from '../utils'
|
||||
import { serializeWith } from './impl'
|
||||
import { isGenericSerializer, Serializer } from './interface'
|
||||
|
||||
export function serialize<T extends Constructor>(constructor: T) {
|
||||
return class Serializable extends constructor implements Serializable {
|
||||
class Serializable extends constructor implements Serializable {
|
||||
static name = constructor.name
|
||||
serialize<U>(serializer: Serializer<U>): U {
|
||||
// shortcut for serializers with only the serializeAny method
|
||||
|
@ -14,4 +15,8 @@ export function serialize<T extends Constructor>(constructor: T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
Serializable[Serde] = (constructor as any)[Serde]
|
||||
|
||||
return Serializable
|
||||
}
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
"lib": ["es2020", "dom"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
||||
// "libReplacement": true, /* Enable lib replacement. */
|
||||
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
"experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
||||
//"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
||||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
||||
|
|
Loading…
Add table
Reference in a new issue