-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathentity-serializer.ts
212 lines (196 loc) · 6.42 KB
/
entity-serializer.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { createLogger } from '@sap-cloud-sdk/util';
import {
Constructable,
EdmTypeField,
OneToOneLink,
Link,
ComplexTypeField,
CollectionField,
EntityBase,
ComplexTypeNamespace,
isComplexTypeNameSpace,
EdmTypeShared,
isEdmType,
PropertyMetadata
} from '../odata-common';
import { toStaticPropertyFormat } from './name-converter';
const logger = createLogger({
package: 'core',
messageContext: 'entity-serializer'
});
/**
* Interface representing the return type of the builder function [[entitySerializer]].
*/
export interface EntitySerializer<
EntityT extends EntityBase = any,
ComplexTypeNamespaceT extends ComplexTypeNamespace<any> = any
> {
serializeEntity: (
entity: EntityT,
entityConstructor: Constructable<EntityT>
) => Record<string, any>;
serializeComplexType: (
fieldValue: any,
complexTypeNameSpace: ComplexTypeNamespaceT
) => any;
serializeEntityNonCustomFields: (
entity: EntityT,
entityConstructor: Constructable<EntityT>
) => Record<string, any>;
}
type TsToEdmType = (
value: any,
edmType: EdmTypeShared<'v2'> | EdmTypeShared<'v4'>
) => any;
/**
* Constructs an entitySerializer given the OData v2 or v4 specific tsToEdm method.
* The concrete serializers are created in odata/v2/entity-serializer.ts and odata/v4/entity-serializer.ts
* @param tsToEdm - Converters ts input to edm values
* @returns a entity serializer as defined by [[EntitySerializer]]
*/
export function entitySerializer(tsToEdm: TsToEdmType): EntitySerializer {
/**
* Converts an instance of an entity class into a JSON payload to be sent to an OData service.
*
* @param entity - An instance of an entity.
* @param entityConstructor - The constructor function of that entity.
* @returns JSON.
*/
function serializeEntity<EntityT extends EntityBase>(
entity: EntityT,
entityConstructor: Constructable<EntityT>
): Record<string, any> {
return {
...serializeEntityNonCustomFields(entity, entityConstructor),
...entity.getCustomFields()
};
}
function serializeField(field: any, fieldValue: any): any {
if (fieldValue === null || fieldValue === undefined) {
return null;
}
if (field instanceof EdmTypeField) {
return tsToEdm(fieldValue, field.edmType);
}
if (field instanceof OneToOneLink) {
return serializeEntityNonCustomFields(fieldValue, field._linkedEntity);
}
if (field instanceof Link) {
return fieldValue.map(linkedEntity =>
serializeEntityNonCustomFields(linkedEntity, field._linkedEntity)
);
}
if (field instanceof ComplexTypeField) {
if (field._complexType) {
return serializeComplexType(fieldValue, field._complexType);
}
return serializeComplexTypeFieldLegacy(field, fieldValue);
}
if (field instanceof CollectionField) {
return serializeCollection(fieldValue, field._fieldType);
}
}
/**
* Converts an instance of an entity class into a JSON payload to be sent to an OData service, ignoring custom fields.
*
* @param entity - An instance of an entity.
* @param entityConstructor - The constructor function of that entity.
* @returns JSON.
*/
function serializeEntityNonCustomFields<EntityT extends EntityBase>(
entity: EntityT,
entityConstructor: Constructable<EntityT>
): Record<string, any> {
if (!entity) {
return {};
}
return Object.keys(entity).reduce((serialized, key) => {
const field = entityConstructor[toStaticPropertyFormat(key)];
const fieldValue = entity[key];
const serializedValue = serializeField(field, fieldValue);
if (typeof serializedValue === 'undefined') {
logger.warn(
`Could not serialize value for unknown field: ${field}. Skipping field.`
);
return serialized;
}
return { ...serialized, [field._fieldName]: serializedValue };
}, {});
}
// TODO: get rid of this function in v2.0
function serializeComplexTypeFieldLegacy<EntityT extends EntityBase>(
complexTypeField: ComplexTypeField<EntityT>,
fieldValue: any
): any {
logger.warn(
'It seems that you are using an outdated OData client. To make this warning disappear, please regenerate your client using the latest version of the SAP Cloud SDK generator.'
);
return Object.entries(complexTypeField)
.filter(
([propertyKey, propertyValue]) =>
(propertyValue instanceof EdmTypeField ||
propertyValue instanceof ComplexTypeField) &&
typeof fieldValue[propertyKey] !== 'undefined'
)
.reduce(
(complexTypeObject, [propertyKey, propertyValue]) => ({
...complexTypeObject,
[propertyValue._fieldName]:
propertyValue instanceof EdmTypeField
? tsToEdm(fieldValue[propertyKey], propertyValue.edmType)
: serializeComplexTypeFieldLegacy(
propertyValue,
fieldValue[propertyKey]
)
}),
{}
);
}
function serializeComplexTypeProperty(
propertyValue: any,
propertyMetadata: PropertyMetadata
): any {
if (propertyMetadata.isCollection) {
return serializeCollection(propertyValue, propertyMetadata.type);
}
if (isComplexTypeNameSpace(propertyMetadata.type)) {
return serializeComplexType(propertyValue, propertyMetadata.type);
}
return tsToEdm(propertyValue, propertyMetadata.type);
}
function serializeComplexType<
ComplexTypeNamespaceT extends ComplexTypeNamespace<any>
>(fieldValue: any, complexType: ComplexTypeNamespaceT): any {
return complexType._propertyMetadata
.map(property => ({
...(typeof fieldValue[property.name] !== 'undefined' && {
[property.originalName]: serializeComplexTypeProperty(
fieldValue[property.name],
property
)
})
}))
.reduce(
(complexTypeObject, property) => ({
...complexTypeObject,
...property
}),
{}
);
}
function serializeCollection<
FieldT extends EdmTypeShared<'any'> | Record<string, any>
>(fieldValue: any[], fieldType: FieldT) {
if (isEdmType(fieldType)) {
return fieldValue.map(val => tsToEdm(val, fieldType));
}
if (isComplexTypeNameSpace(fieldType)) {
return fieldValue.map(val => serializeComplexType(val, fieldType));
}
}
return {
serializeEntity,
serializeComplexType,
serializeEntityNonCustomFields
};
}