-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_wrapper.ts
176 lines (152 loc) · 4.93 KB
/
model_wrapper.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { SchemeType } from './constants'
import { convertModel } from './converter'
import { CommonFieldCreator } from './field_declaration'
import { AllKeysAre, InKeyOfWithType } from './global_types'
import { checkType, error, isObject } from './helpers'
import { preparePropDeclarations, PropDeclaration } from './prop_declaration'
declare interface SerializedObject {
deserialize(): DeserializedObject
}
declare interface DeserializedObject {
[originalField: string]: any
}
declare type SerializedStructure<T> = InKeyOfWithType<
DeclarationsInstance<T>,
any
> &
SerializedObject
export declare interface ModelWrapper<T> {
modelConfiguration: ModelConfiguration
new (originalModel: object): SerializedStructure<T>
serialize(originalModel: AllKeysAre<any>): SerializedStructure<T>
deserialize(usageModel: AllKeysAre<any>): DeserializedObject
getUsagePropertyNames(): string[]
getOriginalPropertyNames(): string[]
getPropertiesMap(reverseNames?: boolean): AllKeysAre<string>
}
export declare interface ModelOptions {
defaultValues: boolean
warnings: boolean
}
const DEFAULT_MODEL_OPTIONS: ModelOptions = {
defaultValues: false,
warnings: true
}
export declare interface ModelConfiguration {
options: ModelOptions
declarations: PropDeclaration[]
}
export const createModelConfig = <T>(
objectWithDeclarations: AllKeysAre<PropDeclaration | CommonFieldCreator>,
modelOptions?: Partial<ModelOptions>
): ModelConfiguration => ({
declarations: preparePropDeclarations<T>(objectWithDeclarations),
options: {
...DEFAULT_MODEL_OPTIONS,
...(modelOptions || {})
}
})
const serializeObject = <T>(
modelConfiguration: ModelConfiguration,
structure: AllKeysAre<any>
): SerializedStructure<T> => {
if (!isObject(structure)) {
structure = {}
}
const serializedObject = Object.create({
deserialize: () => convertModel(serializedObject, modelConfiguration, true)
})
return Object.assign(
serializedObject,
convertModel(structure, modelConfiguration, false)
) as SerializedStructure<T>
}
const deserializeObject = <T>(
modelConfiguration: ModelConfiguration,
structure: AllKeysAre<any>
): DeserializedObject => {
checkType(structure, 'object', 'Usage model')
if (!modelConfiguration) {
error(
'This model is never serialized.' +
'Before using deserialize() needs to call serialize() or create new instance of model'
)
}
return convertModel(structure, modelConfiguration, true)
}
const getPropertyNames = (
{ declarations }: ModelConfiguration,
getOnlyUsageProperties: boolean
) => {
return declarations.reduce((names: string[], declaration) => {
if (declaration.scheme.schemeType !== SchemeType.SERIALIZERS) {
names.push(
declaration.scheme[getOnlyUsageProperties ? 'to' : 'from'].name
)
}
return names
}, [])
}
const getPropertiesMap = (
{ declarations }: ModelConfiguration,
reverseNames?: boolean
): AllKeysAre<string> =>
declarations.reduce((namesMap: AllKeysAre<string>, declaration) => {
if (declaration.scheme.schemeType !== SchemeType.SERIALIZERS) {
const propertiesNames = [
declaration.scheme.to.name,
declaration.scheme.from.name
]
const [keyName, value] = reverseNames
? propertiesNames.reverse()
: propertiesNames
namesMap[keyName] = value
}
return namesMap
}, {})
declare type DeclarationsInstance<C> = C extends (new () => any)
? InstanceType<C>
: C
// TODO: complete it
// declare type SerializedData<
// C,
// D = InKeyOfWithType<DeclarationsInstance<C>, PropDeclaration>
// > = {
// [K in keyof D]: D[K]
// }
export const createModel = <T extends object | (new () => any)>(
Model: T,
partialModelOptions?: Partial<ModelOptions>
): ModelWrapper<T> => {
const declarationInstance =
typeof Model === 'function'
? new (Model as any)()
: new (class Model {
// it is hack to create unique instances
constructor(context: any) {
Object.assign(this, { ...context })
}
})(Model)
const modelConfiguration: ModelConfiguration = createModelConfig<T>(
declarationInstance,
partialModelOptions
)
return class ModelWrapper {
static modelConfiguration = modelConfiguration
static serialize = (originalModel: object) =>
serializeObject<T>(modelConfiguration, originalModel)
static deserialize = (usageModel: object) =>
deserializeObject<T>(modelConfiguration, usageModel)
static getUsagePropertyNames = () =>
getPropertyNames(modelConfiguration, true)
static getOriginalPropertyNames = () =>
getPropertyNames(modelConfiguration, false)
static getPropertiesMap = (reverseNames?: boolean) =>
getPropertiesMap(modelConfiguration, reverseNames)
// instance methods
deserialize: any
constructor(originalModel: object) {
return serializeObject<T>(modelConfiguration, originalModel)
}
} as any // TODO: find a good solution to remove this
}