forked from exilon/QuickLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick.AutoMapper.pas
351 lines (301 loc) · 10.2 KB
/
Quick.AutoMapper.pas
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
{ ***************************************************************************
Copyright (c) 2015-2018 Kike Pérez
Unit : Quick.AutoMapper
Description : Auto Mapper object properties
Author : Kike Pérez
Version : 1.1
Created : 25/08/2018
Modified : 23/09/2018
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.AutoMapper;
{$i QuickLib.inc}
interface
uses
SysUtils,
Generics.Collections,
//{$IFDEF FPC}
typinfo,
//{$ENDIF}
RTTI;
type
TCustomMapping = class
private
fMapDictionary : TDictionary<string,string>;
public
constructor Create;
destructor Destroy; override;
procedure AddMap(const aName, aMapName : string);
function GetMap(const aName : string; out vMapName : string) : Boolean;
end;
TObjMapper = class
public
class procedure Map(aSrcObj : TObject; aTgtObj : TObject; aCustomMapping: TCustomMapping = nil);
end;
TListMapper = class
public
class procedure Map(aSrcList, aTgtList: TObject; aCustomMapping: TCustomMapping);
end;
TObjListMapper = class
public
class procedure Map(aSrcObjList : TObject; aTgtObjList : TObject; aCustomMapping : TCustomMapping = nil);
end;
TMapper<T : class, constructor> = class
public
class function Map(aSrcObj : TObject; aCustomMapping: TCustomMapping = nil): T; overload;
class procedure Map(aSrcObj : TObject; aTgtObj : T; aCustomMapping : TCustomMapping = nil); overload;
end;
TAutoMapper<TClass1, TClass2 : class, constructor> = class
private
fCustomMapping : TCustomMapping;
public
constructor Create;
destructor Destroy; override;
property CustomMapping : TCustomMapping read fCustomMapping write fCustomMapping;
function Map(aSrcObj : TClass1) : TClass2; overload;
{$IFNDEF FPC}
function Map(aSrcObj : TClass2) : TClass1; overload;
{$ELSE}
//freepascal detects overload with generic types as duplicated function, added dummy field to avoid this
function Map(aSrcObj : TClass2; dummy : Boolean = True) : TClass1; overload;
{$ENDIF}
end;
EAutoMapperError = class(Exception);
implementation
{ TObjMapper }
class procedure TObjMapper.Map(aSrcObj : TObject; aTgtObj : TObject; aCustomMapping: TCustomMapping = nil);
var
ctx : TRttiContext;
rType : TRttiType;
tgtprop : TRttiProperty;
mapname : string;
obj : TObject;
clname : string;
begin
if aTgtObj = nil then aTgtObj := GetTypeData(aTgtObj.ClassInfo).classType.Create;
rType := ctx.GetType(aSrcObj.ClassInfo);
for tgtprop in ctx.GetType(aTgtObj.ClassInfo).GetProperties do
begin
if tgtprop.IsWritable then
begin
if tgtprop.Name = 'Agent'
then Sleep(0);
if not tgtprop.PropertyType.IsInstance then
begin
if Assigned(aCustomMapping) then
begin
if aCustomMapping.GetMap(tgtprop.Name,mapname) then
begin
if rType.GetProperty(mapname) = nil then raise EAutoMapperError.CreateFmt('No valid custom mapping (Source: %s - Target: %s)',[mapname,tgtprop.Name]);
{$IFNDEF FPC}
tgtprop.SetValue(aTgtObj,rType.GetProperty(mapname).GetValue(aSrcObj))
{$ELSE}
SetPropValue(aTgtObj,tgtprop.Name,GetPropValue(aSrcObj,mapname));
{$ENDIF}
end
else
begin
if rType.GetProperty(tgtprop.Name) <> nil then
try
{$IFNDEF FPC}
tgtprop.SetValue(aTgtObj,rType.GetProperty(tgtprop.Name).GetValue(aSrcObj));
{$ELSE}
SetPropValue(aTgtObj,tgtprop.Name,GetPropValue(aSrcObj,tgtprop.Name));
{$ENDIF}
except
on E : Exception do raise EAUtoMapperError.CreateFmt('Error mapping property "%s" : %s',[tgtprop.Name,e.message]);
end;
end;
end
else
begin
try
{$IFNDEF FPC}
if rType.GetProperty(tgtprop.Name) <> nil then tgtprop.SetValue(aTgtObj,rType.GetProperty(tgtprop.Name).GetValue(aSrcObj));
{$ELSE}
if rType.GetProperty(tgtprop.Name) <> nil then SetPropValue(aTgtObj,tgtprop.Name,GetPropValue(aSrcObj,tgtprop.Name));
{$ENDIF}
except
on E : Exception do raise EAUtoMapperError.CreateFmt('Error mapping property "%s" : %s',[tgtprop.Name,e.message]);
end;
end;
end
else
begin
obj := tgtprop.GetValue(aTgtObj).AsObject;
{$IFNDEF FPC}
if obj = nil then obj := TObject.Create;
{$ELSE}
if obj = nil then obj := GetObjectProp(aSrcObj,tgtprop.Name).ClassType.Create;
{$ENDIF}
if obj <> nil then
begin
{$IFNDEF FPC}
clname := rType.GetProperty(tgtprop.Name).GetValue(aSrcObj).AsObject.ClassName;
if clname.StartsWith('TObjectList') then TObjListMapper.Map(rType.GetProperty(tgtprop.Name).GetValue(aSrcObj).AsObject,obj,aCustomMapping)
else if clname.StartsWith('TList') then TListMapper.Map(rType.GetProperty(tgtprop.Name).GetValue(aSrcObj).AsObject,obj,aCustomMapping)
else TObjMapper.Map(rType.GetProperty(tgtprop.Name).GetValue(aSrcObj).AsObject,obj,aCustomMapping)
{$ELSE}
TObjMapper.Map(GetObjectProp(aSrcObj,tgtprop.Name),obj,aCustomMapping);
SetObjectProp(aTgtObj,tgtprop.Name,obj);
{$ENDIF}
end
else raise EAutoMapperError.CreateFmt('Target object "%s" not autocreated by class',[tgtprop.Name]);
end;
end;
end;
end;
class function TMapper<T>.Map(aSrcObj : TObject; aCustomMapping: TCustomMapping = nil) : T;
var
obj : T;
begin
obj := T.Create;
TObjMapper.Map(aSrcObj,obj,aCustomMapping);
Result := obj;
end;
class procedure TMapper<T>.Map(aSrcObj : TObject; aTgtObj : T; aCustomMapping : TCustomMapping = nil);
begin
TObjMapper.Map(aSrcObj, aTgtObj, aCustomMapping);
end;
{ TAutoMapper<TClass1, TClass2> }
constructor TAutoMapper<TClass1, TClass2>.Create;
begin
fCustomMapping := TCustomMapping.Create;
end;
destructor TAutoMapper<TClass1, TClass2>.Destroy;
begin
if Assigned(fCustomMapping) then fCustomMapping.Free;
inherited;
end;
function TAutoMapper<TClass1, TClass2>.Map(aSrcObj: TClass1): TClass2;
begin
Result := TMapper<TClass2>.Map(aSrcObj,fCustomMapping);
end;
{$IFNDEF FPC}
function TAutoMapper<TClass1, TClass2>.Map(aSrcObj: TClass2): TClass1;
{$ELSE}
function TAutoMapper<TClass1, TClass2>.Map(aSrcObj: TClass2; dummy : Boolean = True): TClass1;
{$ENDIF}
begin
Result := TMapper<TClass1>.Map(aSrcObj,fCustomMapping);
end;
{ TCustomMappingFields }
procedure TCustomMapping.AddMap(const aName, aMapName: string);
begin
//add map fields
fMapDictionary.Add(aName,aMapName);
//add reverse lookup
fMapDictionary.Add(aMapName,aName);
end;
constructor TCustomMapping.Create;
begin
fMapDictionary := TDictionary<string,string>.Create;
end;
destructor TCustomMapping.Destroy;
begin
fMapDictionary.Free;
inherited;
end;
function TCustomMapping.GetMap(const aName: string; out vMapName: string): Boolean;
begin
Result := fMapDictionary.TryGetValue(aName,vMapName);
end;
{ TListMapper }
class procedure TListMapper.Map(aSrcList, aTgtList: TObject; aCustomMapping: TCustomMapping);
{$IFNDEF FPC}
var
rtype: TRttiType;
rtype2 : TRttiType;
typinfo : PTypeInfo;
methToArray: TRttiMethod;
value: TValue;
valuecop : TValue;
obj : TObject;
i : Integer;
rprop : TRttiProperty;
ctx : TRttiContext;
begin
rtype := ctx.GetType(aSrcList.ClassInfo);
methToArray := rtype.GetMethod('ToArray');
if Assigned(methToArray) then
begin
value := methToArray.Invoke(aSrcList,[]);
Assert(value.IsArray);
rtype2 := ctx.GetType(aTgtList.ClassInfo);
rProp := rtype2.GetProperty('List');
typinfo := GetTypeData(rProp.PropertyType.Handle).DynArrElType^;
for i := 0 to value.GetArrayLength - 1 do
begin
if typinfo.Kind = tkClass then
begin
obj := typinfo.TypeData.ClassType.Create;
TObjMapper.Map(value.GetArrayElement(i).AsObject,obj,aCustomMapping);
TList<TObject>(aTgtList).Add(obj);
end
else if typinfo.Kind = tkRecord then
begin
valuecop := value.GetArrayElement(i);
//??
end
else
begin
valuecop := value.GetArrayElement(i);
case typinfo.Kind of
tkChar, tkString, tkWChar, tkWString : TList<string>(aTgtList).Add(valuecop.AsString);
tkInteger, tkInt64 : TList<Integer>(aTgtList).Add(valuecop.AsInt64);
tkFloat : TList<Extended>(aTgtList).Add(valuecop.AsExtended);
end;
end;
end;
end;
end;
{$ELSE}
begin
end;
{$ENDIF}
{ TObjListMapper }
class procedure TObjListMapper.Map(aSrcObjList, aTgtObjList: TObject; aCustomMapping: TCustomMapping);
{$IFNDEF FPC}
var
rtype: TRttiType;
rtype2 : TRttiType;
typinfo : PTypeInfo;
methToArray: TRttiMethod;
value: TValue;
obj : TObject;
i : Integer;
rprop : TRttiProperty;
ctx : TRttiContext;
begin
rtype := ctx.GetType(aSrcObjList.ClassInfo);
methToArray := rtype.GetMethod('ToArray');
if Assigned(methToArray) then
begin
value := methToArray.Invoke(aSrcObjList,[]);
Assert(value.IsArray);
rtype2 := ctx.GetType(aTgtObjList.ClassInfo);
rProp := rtype2.GetProperty('List');
typinfo := GetTypeData(rProp.PropertyType.Handle).DynArrElType^;
for i := 0 to value.GetArrayLength - 1 do
begin
obj := typinfo.TypeData.ClassType.Create;
TObjMapper.Map(value.GetArrayElement(i).AsObject,obj,aCustomMapping);
TObjectList<TObject>(aTgtObjList).Add(obj);
end;
end;
end;
{$ELSE}
begin
end;
{$ENDIF}
end.