-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserialize_property.ts
149 lines (129 loc) · 4.41 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2018-2022 Gamebridge.ai authors. All rights reserved. MIT license.
import { Serializable, SERIALIZABLE_CLASS_MAP } from "./serializable.ts";
import { SerializePropertyOptionsMap } from "./serialize_property_options_map.ts";
import {
FromJSONStrategy,
ToJSONStrategy,
} from "./strategy/compose_strategy.ts";
import { ERROR_SYMBOL_PROPERTY_NAME } from "./error_messages.ts";
/** options to use when (de)serializing values */
export class SerializePropertyOptions {
public fromJSONStrategy?: FromJSONStrategy;
public toJSONStrategy?: ToJSONStrategy;
constructor(
public propertyKey: string | symbol,
public serializedKey: string,
fromJSONStrategy?: FromJSONStrategy,
toJSONStrategy?: ToJSONStrategy,
) {
if (fromJSONStrategy) {
this.fromJSONStrategy = fromJSONStrategy;
}
if (toJSONStrategy) {
this.toJSONStrategy = toJSONStrategy;
}
}
}
/** Function to transform a property name into a serialized key programmatically */
export type ToSerializedKeyStrategy = (propertyName: string) => string;
/** string/symbol property name or options for (de)serializing values */
export type SerializePropertyArgument =
| string
| ToSerializedKeyStrategy
| {
serializedKey?: string | ToSerializedKeyStrategy;
fromJSONStrategy?: FromJSONStrategy;
toJSONStrategy?: ToJSONStrategy;
};
/** converted interface for `SerializePropertyArgument` */
interface SerializePropertyArgumentObject {
serializedKey: string;
fromJSONStrategy?: FromJSONStrategy;
toJSONStrategy?: ToJSONStrategy;
}
/** Property wrapper that adds `SerializeProperty` options to the class map */
export function SerializeProperty(
args?: string | SerializePropertyArgument,
): PropertyDecorator {
return (
target: unknown,
propertyName: string | symbol,
) => {
const decoratorArguments = args ?? {};
const decoratorArgumentOptions = getDecoratorArgumentOptions(
decoratorArguments,
target,
propertyName,
);
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,
);
}
serializablePropertiesMap?.set(
new SerializePropertyOptions(
propertyName,
decoratorArgumentOptions.serializedKey,
decoratorArgumentOptions.fromJSONStrategy,
decoratorArgumentOptions.toJSONStrategy,
),
);
};
}
/** Parses the arguments provided to SerializeProperty and
* returns an object used to create a key mapping
*/
function getDecoratorArgumentOptions(
decoratorArguments: SerializePropertyArgument,
target: unknown,
propertyName: string | symbol,
): SerializePropertyArgumentObject {
// Direct mapping to string
if (typeof decoratorArguments === "string") {
return { serializedKey: decoratorArguments };
}
// Property key transform function
if (typeof decoratorArguments === "function") {
return {
serializedKey: decoratorArguments(String(propertyName)),
};
}
// 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_SYMBOL_PROPERTY_NAME);
}
// Property key transform function with additional options
if (typeof decoratorArguments.serializedKey === "function") {
return {
serializedKey: decoratorArguments.serializedKey(String(propertyName)),
fromJSONStrategy: decoratorArguments.fromJSONStrategy,
toJSONStrategy: decoratorArguments.toJSONStrategy,
};
}
if (typeof decoratorArguments.serializedKey === "string") {
return decoratorArguments as SerializePropertyArgumentObject;
}
// Use inherited tsTransformKey strategy or default no change transform
// to transform property key decoratorArguments.serializedKey will override
return {
serializedKey: (target as Serializable).tsTransformKey(
String(propertyName),
),
fromJSONStrategy: decoratorArguments.fromJSONStrategy,
toJSONStrategy: decoratorArguments.toJSONStrategy,
};
}