-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialize_property.ts
109 lines (93 loc) · 3.3 KB
/
serialize_property.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2018-2020 Gamebridge.ai authors. All rights reserved. MIT license.
import {
SERIALIZABLE_CLASS_MAP,
SerializePropertyOptions,
FromJsonStrategy,
ToJsonStrategy,
} from "./serializable.ts";
import { SerializePropertyOptionsMap } from "./serialize_property_options_map.ts";
export const ERROR_MESSAGE_SYMBOL_PROPERTY_NAME =
"The key name cannot be inferred from a symbol. A value for serializedName must be provided";
/** string/symbol property name or options for (de)serializing values */
export declare type SerializePropertyArgument =
| string
| {
serializedKey?: string;
fromJsonStrategy?:
| FromJsonStrategy
| (FromJsonStrategy | FromJsonStrategy[])[];
toJsonStrategy?:
| ToJsonStrategy
| (ToJsonStrategy | ToJsonStrategy[])[];
};
interface SerializePropertyArgumentObject {
serializedKey: string;
fromJsonStrategy?:
| FromJsonStrategy
| (FromJsonStrategy | FromJsonStrategy[])[];
toJsonStrategy?:
| ToJsonStrategy
| (ToJsonStrategy | ToJsonStrategy[])[];
}
/** Property wrapper that adds serializable options to the class map
* using the original propertyName as the map key
*/
export function SerializeProperty(): PropertyDecorator;
/** Property wrapper that adds serializable options to the class map
* using the provided string as the map key
*/
export function SerializeProperty(arg: string): PropertyDecorator;
/** Property wrapper that adds serializable options to the class map
* using options, `serializedName` as the key or `propertyName` if
* `serializedName` is not set
*/
export function SerializeProperty(
arg: SerializePropertyArgument,
): PropertyDecorator;
/** Property wrapper that adds serializable options to the class map */
export function SerializeProperty(
decoratorArguments: SerializePropertyArgument = {},
): PropertyDecorator {
return (target: unknown, propertyName: string | symbol) => {
let decoratorArgumentOptions: SerializePropertyArgumentObject;
if (typeof decoratorArguments === "string") {
decoratorArgumentOptions = { serializedKey: decoratorArguments };
} else {
// We can't use symbols as keys when serializing
// a serializedName must be provided if the property isn't a string
if (
!decoratorArguments.serializedKey &&
typeof propertyName === "symbol"
) {
throw new Error(ERROR_MESSAGE_SYMBOL_PROPERTY_NAME);
}
decoratorArgumentOptions = {
serializedKey: propertyName as string,
...decoratorArguments,
};
}
let serializablePropertiesMap = SERIALIZABLE_CLASS_MAP.get(target);
// Initialize the map for this class
if (!serializablePropertiesMap) {
// If the parent has a serialization map then inherit it
const parentMap = SERIALIZABLE_CLASS_MAP.get(
Object.getPrototypeOf(target),
);
SERIALIZABLE_CLASS_MAP.set(
target,
new SerializePropertyOptionsMap(parentMap),
);
serializablePropertiesMap = SERIALIZABLE_CLASS_MAP.get(
target,
) as SerializePropertyOptionsMap;
}
serializablePropertiesMap.set(
new SerializePropertyOptions(
propertyName,
decoratorArgumentOptions.serializedKey,
decoratorArgumentOptions.fromJsonStrategy,
decoratorArgumentOptions.toJsonStrategy,
),
);
};
}