-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMapper.cls
1873 lines (1716 loc) · 54.1 KB
/
Mapper.cls
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* MIT License
*
* Copyright (c) 2018 Click to Cloud Pty Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
**/
/**
* A Mapper is an object that maps the source object to the destination object
*
* Mappers in fact convert from objects to objects
*
* Rules of how to map each field can be applied to the Mapper
*
* Also Mappers are subclasses of Funcs, which means that Mappers can be used as Funcs
*
* */
public class Mapper extends Func {
// Convert an object to Mapper.DTO
public static final String TO_DTO = 'DTO';
// Convert an object to Map<String, Object>
public static final String TO_MAP = 'Map';
// Convert an object to SObject
public static final String TO_SOBJECT = 'SObject';
// Convert an object to Object
public static final String TO_OBJECT = 'Object';
// Convert an object to JSON string
public static final String TO_JSON = 'JSON';
// If constructing using this value, empty keys will be ignored during the mapping
public static final Boolean IGNORE_NULLS = true;
// The names for the input objects, used in case of converting multiple objects to one
private List<String> inputNames = null;
// The mappers for each field
private Map<String, FieldMapper> fieldMappers = new Map<String, FieldMapper>();
// Keep track of which result should the Mapper convert the object to
private String convertTo = TO_DTO;
// Keep track of the type of the object, should it be converted to SObject or Object
private Type convertToType = null;
// Ignore empty keys during the mapping
private Boolean ignoreNulls = false;
/**
* Default constructor
* */
public Mapper() {
super(-1);
}
/**
* Constructor with ignore nulls configuration
* */
public Mapper(Boolean ignoreNulls) {
this();
this.ignoreNulls = ignoreNulls;
}
/**
* Rename inputs as the given names, so that they can be referenced in the field mapper key expressions
*
* Example:
* new Mapper()
* .inputAs(new List<String>{ 'a', 'b' })
* .mapField('x', 'a.name')
* .mapField('y', 'b.name')
* .run(objA, objB);
*
* @param names The new names for the inputs
* @return Mapper
* */
public Mapper inputAs(List<String> names) {
this.inputNames = names;
return this;
}
/**
* Rename inputs as the given names, so that they can be referenced in the field mapper key expressions
*
* Example:
* new Mapper()
* .inputAs('a', 'b')
* .mapField('x', 'a.name')
* .mapField('y', 'b.name')
* .run(objA, objB);
*
* @param name1 The name of the first object to be converted
* @param name2 The name of the second object to be converted
* @return Mapper
* */
public Mapper inputAs(String name1, String name2) {
return this.inputAs(new List<String>{ name1, name2 });
}
/**
* Rename inputs as the given names, so that they can be referenced in the field mapper key expressions
*
* Example:
* new Mapper()
* .inputAs('a', 'b', 'c')
* .mapField('x', 'a.name')
* .mapField('y', 'b.name')
* .mapField('z', 'c.name')
* .run(objA, objB, objC);
*
* @param name1 The name of the first object to be converted
* @param name2 The name of the second object to be converted
* @param name3 The name of the third object to be converted
* @return Mapper
* */
public Mapper inputAs(String name1, String name2, String name3) {
return this.inputAs(new List<String>{ name1, name2, name3 });
}
/**
* Map the field using the FieldMapper
*
* Example:
* new Mapper()
* .mapField('name', new Mapper.FieldMapper('Name'))
* // Map an object with 'Name' to an object with 'name'
*
* @param fieldName The target object field name
* @param fMapper The field mapper
* @return Mapper
* */
public Mapper mapField(String fieldName, Mapper.FieldMapper fMapper) {
if(fMapper != null) {
fMapper.setFieldName(fieldName);
fMapper.setInputNames(this.inputNames);
this.fieldMappers.put(fieldName, fMapper);
}
return this;
}
/**
* Map the field using the keys and transformer Func
*
* Example:
* new Mapper()
* .mapField('name', new List<String>{ 'Name', 'Description' }, R.concat)
* // Map an object with 'Name' and 'Description' to an object with 'name', concatenating
* // values from 'Name' and 'Description'
*
* @param fieldName The target object field name
* @param keys The keys of the fields in the source object
* @param transformer The transforming Func
* @return Mapper
* */
public Mapper mapField(String fieldName, List<String> keys, Func transformer) {
return mapField(fieldName, new FieldMapper(keys, transformer));
}
/**
* Map the field using the keys and transformer Func
*
* Example:
* new Mapper()
* .mapField('name', 'Name', 'Description', R.concat)
* // Map an object with 'Name' and 'Description' to an object with 'name', concatenating
* // values from 'Name' and 'Description'
*
* @param fieldName The target object field name
* @param key1 The first key field
* @param key2 The second key field
* @param transformer The transforming Func
* @return Mapper
* */
public Mapper mapField(String fieldName, String key1, String key2, Func transformer) {
return this.mapField(fieldName, new List<String>{ key1, key2 }, transformer);
}
/**
* Map the field using the key and transformer Func
*
* Example:
* new Mapper()
* .mapField('name', 'Name', R.prepend.apply('Mr.'))
* // Map an object with 'Name' to an object with 'name', prepending 'Mr.' to the value
*
* @param fieldName The target object field name
* @param key The key field
* @param transformer The transforming Func
* @return Mapper
* */
public Mapper mapField(String fieldName, String key, Func transformer) {
return this.mapField(fieldName, new List<String>{ key }, transformer);
}
/**
* Map the field using the transformer Func
*
* Example:
* new Mapper()
* .mapField('Name', R.prepend.apply('Mr.'))
* // Map an object with 'Name' to an object with 'Name', prepending 'Mr.' to the value
*
* @param fieldName The target object field name
* @param transformer The transforming Func
* @return Mapper
* */
public Mapper mapField(String fieldName, Func transformer) {
return this.mapField(fieldName, new List<String>(), transformer);
}
/**
* Map the field using the key
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* // Map an object with 'Name' to an object with 'name'
*
* @param fieldName The target object field name
* @param key The key field
* @return Mapper
* */
public Mapper mapField(String fieldName, String key) {
return this.mapField(fieldName, key, null);
}
/**
* Map the field using the key same as the field name
*
* Example:
* new Mapper()
* .mapField('Name')
* // Map an object with 'Name' to an object with 'Name'
*
* @param fieldName The target object field name
* @return Mapper
* */
public Mapper mapField(String fieldName) {
return this.mapField(fieldName, new List<String>(), null);
}
/**
* Map the fields using the field mappers
* The keys in the mapping will be used as the new field name
* The values in the mapping will be used to map the field
* The value can be a FieldMapper, String or Func
* If the value is a FieldMapper, it will be used in mapping the field
* If the value is a String, it will simply copy the value of the field to new field
* If the value is a Func, it will convert the original value to the new value under the same field name
*
* Example:
* new Mapper()
* .mapFields(new Map<String, Object>{
* 'name' => 'Name',
* 'description' => 'Description'
* })
* // Map an object with 'Name' and 'Description' to an object with 'name' and 'description'
*
* @param mapping The field mappers
* @return Mapper
* */
public Mapper mapFields(Map<String, Object> mapping) {
for(String key : mapping.keySet()) {
Object val = mapping.get(key);
if(val instanceof FieldMapper) {
mapField(key, (FieldMapper)val);
}
else if(val instanceof String) {
mapField(key, (String)val);
}
else if(val instanceof Func) {
mapField(key, (Func)val);
}
else {
mapField(key);
}
}
return this;
}
/**
* Create a reversed mapper from the mapper
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* .reverseTo(Mapper.TO_SOBJECT, Account.class)
* // Create a reversed mapper that converts the object back to an Account
*
* @param convertToType The type of object to convert the object to
* @param objectType The type of object if converted to SObject/Object
* @return Mapper
* */
public Mapper reverseTo(String convertToType, Type objectType) {
Mapper reversed = new Mapper(ignoreNulls);
for(FieldMapper fMapper : this.fieldMappers.values()) {
FieldMapper reversedFieldMapper = fMapper.reverse();
reversed.mapField(reversedFieldMapper.getFieldName(), reversedFieldMapper);
}
if(convertToType == TO_DTO) {
reversed.toDTO();
}
else if(convertToType == TO_MAP) {
reversed.toMap();
}
else if(convertToType == TO_JSON) {
reversed.toJSON();
}
else if(convertToType == TO_SOBJECT) {
reversed.toSObject(objectType);
}
else if(convertToType == TO_OBJECT) {
reversed.toObject(objectType);
}
else {
reversed.toDTO();
}
return reversed;
}
/**
* Create a reversed mapper from the mapper
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* .reverseTo(Mapper.TO_MAP)
* // Create a reversed mapper that converts the object back to a map
*
* @param convertToType The type of object to convert the object to
* @return Mapper
* */
public Mapper reverseTo(String convertToType) {
return this.reverseTo(convertToType, null);
}
/**
* Create a reversed mapper from the mapper
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* .reverseToDTO()
* // Create a reversed mapper that converts the object back to a Mapper.DTO
*
* @return Mapper
* */
public Mapper reverseToDTO() {
return this.reverseTo(TO_DTO);
}
/**
* Create a reversed mapper from the mapper
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* .reverseToMap()
* // Create a reversed mapper that converts the object back to a map
*
* @return Mapper
* */
public Mapper reverseToMap() {
return this.reverseTo(TO_MAP);
}
/**
* Create a reversed mapper from the mapper
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* .reverseToJSON()
* // Create a reversed mapper that converts the object back to a JSON string
*
* @return Mapper
* */
public Mapper reverseToJSON() {
return this.reverseTo(TO_JSON);
}
/**
* Create a reversed mapper from the mapper
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* .reverseToObject(AccountDTO.class)
* // Create a reversed mapper that converts the object back to an AccountDTO
*
* @param objectType The object type of the Object
* @return Mapper
* */
public Mapper reverseToObject(Type objectType) {
return this.reverseTo(TO_OBJECT, objectType);
}
/**
* Create a reversed mapper from the mapper
*
* Example:
* new Mapper()
* .mapField('name', 'Name')
* .reverseToSObject(Account.class)
* // Create a reversed mapper that converts the object back to an Account
*
* @param objectType The object type of the SObject
* @return Mapper
* */
public Mapper reverseToSObject(Type objectType) {
return this.reverseTo(TO_SOBJECT, objectType);
}
/**
* Specify the type of object that the Mapper will convert to
*
* Example:
* new Mapper()
* .mapField('Name', 'name')
* .to(Mapper.TO_SOBJECT, Account.class)
* // Create a mapper that converts the object to an Account
*
* @param type The type of object to convert to
* @param objectType The type of object when converted to SObject/Object
* @return Mapper
* */
public Mapper to(String type, Type objectType) {
this.convertTo = type;
this.convertToType = objectType;
return this;
}
/**
* Specify the type of object that the Mapper will convert to
*
* Example:
* new Mapper()
* .mapField('Name', 'name')
* .to(Mapper.TO_MAP)
* // Create a mapper that converts the object to a map
*
* @param type The type of object to convert to
* @return Mapper
* */
public Mapper to(String type) {
return this.to(type, null);
}
/**
* Specify the type of object that the Mapper will convert to
*
* Example:
* new Mapper()
* .mapField('Name', 'name')
* .toDTO()
* // Create a mapper that converts the object to a Mapper.DTO
*
* @return Mapper
* */
public Mapper toDTO() {
return to(TO_DTO);
}
/**
* Specify the type of object that the Mapper will convert to
*
* Example:
* new Mapper()
* .mapField('Name', 'name')
* .toMap()
* // Create a mapper that converts the object to a map
*
* @return Mapper
* */
public Mapper toMap() {
return to(TO_MAP);
}
/**
* Specify the type of object that the Mapper will convert to
*
* Example:
* new Mapper()
* .mapField('Name', 'name')
* .toSObject(Account.class)
* // Create a mapper that converts the object to an Account
*
* @param objectType The object type of the SObject
* @return Mapper
* */
public Mapper toSObject(Type objectType) {
return to(TO_SOBJECT, objectType);
}
/**
* Specify the type of object that the Mapper will convert to
*
* Example:
* new Mapper()
* .mapField('Name', 'name')
* .toObject(AccountDTO.class)
* // Create a mapper that converts the object to an AccountDTO
*
* @param objectType The object type of the Object
* @return Mapper
* */
public Mapper toObject(Type objectType) {
return to(TO_OBJECT, objectType);
}
/**
* Specify the type of object that the Mapper will convert to
*
* Example:
* new Mapper()
* .mapField('Name', 'name')
* .toJSON()
* // Create a mapper that converts the object to a JSON string
*
* @return Mapper
* */
public Mapper toJSON() {
return to(TO_JSON);
}
private Object doConvert(DTO d) {
if(this.convertTo == TO_DTO) {
return d;
}
else if(this.convertTo == TO_MAP) {
return d.toMap();
}
else if(this.convertTo == TO_SOBJECT) {
return d.toSObject(this.convertToType);
}
else if(this.convertTo == TO_OBJECT) {
return d.toObject(this.convertToType);
}
else if(this.convertTo == TO_JSON) {
return d.toJSON();
}
else {
return d;
}
}
/**
* Run the mapper
*
* @param args The list of objects as the source
* @return Object
* */
public override Object execN(List<Object> args) {
DTO d = new DTO();
DTO orig = new DTO(args);
for(String fieldName : this.fieldMappers.keySet()) {
FieldMapper fm = this.fieldMappers.get(fieldName);
if (ignoreNulls && !orig.containsPath(fm.keys)) {
// Ignore empty keys
continue;
}
Object value = fm.transform(args);
d.setObject(fieldName, value);
}
return this.doConvert(d);
}
/**
* The field mapper
* */
public class FieldMapper {
// The input names from the Mapper
private List<String> inputNames;
// The field name of this field mapper
private String fieldName;
// The keys used to map the field
private List<String> keys;
// The transforming Func
private Func transformer;
/**
* Construct an instance from keys and transformer
*
* @param keys The keys
* @param transformer The transforming Func
* */
public FieldMapper(List<String> keys, Func transformer) {
this.keys = keys;
this.transformer = transformer;
if(this.transformer == null) {
this.transformer = R.identity;
}
}
/**
* Construct an instance from key and transformer
*
* @param key The key
* @param transformer The transforming Func
* */
public FieldMapper(String key, Func transformer) {
this(new List<String>{ key }, transformer);
}
/**
* Construct an instance from the key
*
* @param key The key
* */
public FieldMapper(String key) {
this(key, null);
}
/**
* Construct an instance from the first key, the second key and the transformer
*
* @param key1 The first key
* @param key2 The second key
* @param transformer The transforming Func
* */
public FieldMapper(String key1, String key2, Func transformer) {
this(new List<String>{ key1, key2 }, transformer);
}
void setInputNames(List<String> inputNames) {
this.inputNames = inputNames;
}
void setFieldName(String fieldName) {
this.fieldName = fieldName;
if(this.keys == null || this.keys.isEmpty()) {
this.keys = new List<String>{ this.fieldName };
}
}
/**
* Get the field name
*
* @return String
* */
public String getFieldName() {
return this.fieldName;
}
FieldMapper reverse() {
if(this.inputNames != null && !this.inputNames.isEmpty()) {
throw new MapperException('Cannot reverse mapper with input names set');
}
if(this.keys.size() != 1) {
throw new MapperException('Cannot reverse field mapper with multiple key inputs');
}
if(this.transformer != R.identity) {
throw new MapperException('Cannot reverse field mapper with transformer func set');
}
FieldMapper reversed = new FieldMapper(this.fieldName);
reversed.setFieldName(this.keys.get(0));
return reversed;
}
private Object getKeyValue(String key, List<Object> args) {
if(this.inputNames == null || this.inputNames.isEmpty()) {
DTO d = new DTO(args.get(0));
return d.getObject(key);
}
else {
Integer index = key.indexOf('.');
if(index < 0) {
throw new MapperException('Unprefixed key found for ' + key);
}
String prefix = key.substring(0, index);
String realKey = key.substring(index + 1);
Integer which = this.inputNames.indexOf(prefix);
if(which < 0) {
throw new MapperException('Invalid input name for ' + prefix);
}
DTO d = new DTO(args.get(which));
return d.getObject(realKey);
}
}
Object transform(List<Object> args) {
List<Object> fnArgs = new List<Object>();
for(String key : keys) {
fnArgs.add(getKeyValue(key, args));
}
return this.transformer.runN(fnArgs);
}
}
/**
* The DTO object to easy manipulate structures of nesting maps
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.setObject('person.name', 'Adam');
* Map<String, Object> person = d.getMap('person');
* String name = d.getString('person.name');
*
* */
public class DTO {
// Holding the map data
@TestVisible
private Map<String, Object> data;
/**
* Construct an empty instance
* */
public DTO() {
this.data = new Map<String, Object>();
}
/**
* Construct an instance from another DTO
*
* @param other The other DTO
* */
public DTO(DTO other) {
this((Object)other);
}
/**
* Construct an instance from a map
*
* @param mMap The source of map
* */
public DTO(Map<String, Object> mMap) {
this((Object)mMap);
}
/**
* Construct an instance from a JSON string
*
* @param jsonStr The JSON string
* */
public DTO(String jsonStr) {
this((Object)jsonStr);
}
/**
* Construct an instance from an SObject
*
* @param sObj The SObject
* */
public DTO(SObject sObj) {
this((Object)sObj);
}
/**
* Construct an instance from a list
*
* @param sObj The SObject
* */
public DTO(List<Object> sObj) {
this((Object)sObj);
}
/**
* Construct an instance from an Object
*
* @param obj The object
* */
public DTO(Object obj) {
if(obj instanceof DTO) {
this.data = new Map<String, Object>(((DTO)obj).data);
}
else if(obj instanceof Map<String, Object>) {
this.data = new Map<String, Object>((Map<String, Object>)obj);
}
else if(obj instanceof String) {
this.data = (Map<String, Object>)JSON.deserializeUntyped((String)obj);
}
else if(obj instanceof SObject) {
this.data = ((SObject)obj).getPopulatedFieldsAsMap();
}
else if(obj instanceof List<Object>) {
this.data = new Map<String, Object>();
for (Object item : (List<Object>)obj) {
this.data.putAll(new DTO(item).data);
}
}
else {
this.data = (Map<String, Object>)JSON.deserializeUntyped(JSON.serialize(obj));
}
}
/**
* Get the key set
*
* @return Set<String>
* */
public Set<String> keySet() {
return this.data.keySet();
}
/**
* Check if the path is contained
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.containsPath(new List<String>{ 'person', 'name' });
*
* @param path The path
* @return Boolean
* */
public Boolean containsPath(List<String> path) {
return this.containsPath(this.data, path);
}
/**
* Check if the path is contained
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.containsPath('person.name');
*
* @param path The path
* @return Boolean
* */
public Boolean containsPath(String path) {
if(String.isEmpty(path)) {
return false;
}
return this.containsPath(path.split('\\.'));
}
/**
* Get the string representation
*
* @return String
* */
public override String toString() {
return String.valueOf(this.data);
}
/**
* Convert this DTO to map
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.setObject('person.name', 'Adam');
* Map<String, Object> data = d.toMap();
*
* @return Map<String, Object>
* */
public Map<String, Object> toMap() {
return new Map<String, Object>(this.data);
}
/**
* Convert this DTO to JSON string
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.setObject('person.name', 'Adam');
* String jsonStr = d.toJSON();
*
* @return String
* */
public String toJSON() {
return JSON.serialize(this.data);
}
/**
* Convert this DTO to SObject
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.setObject('Name', 'Adam');
* Account acc = (Account)d.toSObject(Account.class);
*
* @param sObjType The type of the SObject
* @return SObject
* */
public SObject toSObject(Type sObjType) {
return (SObject)JSON.deserialize(this.toJSON(), sObjType);
}
/**
* Convert this DTO to Object
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.setObject('Name', 'Adam');
* AccountDTO acc = (AccountDTO)d.toObject(AccountDTO.class);
*
* @param objType The type of the Object
* @return Object
* */
public Object toObject(Type objType) {
return JSON.deserialize(this.toJSON(), objType);
}
private Boolean containsPath(Object data, List<String> path) {
String key = path.get(0);
if(data instanceof Map<String, Object>) {
Map<String, Object> mMap = (Map<String, Object>)data;
List<String> nextPath = R.of(path).drop(1).toStringList();
return nextPath.isEmpty() ? mMap.containsKey(key) : containsPath(mMap.get(key), nextPath);
}
else {
return false;
}
}
private Object getObject(Object data, List<String> path) {
String key = path.get(0);
if(data instanceof Map<String, Object>) {
Map<String, Object> mMap = (Map<String, Object>)data;
Object next = mMap.get(key);
List<String> nextPath = R.of(path).drop(1).toStringList();
return nextPath.isEmpty() ? next : getObject(next, nextPath);
}
else if(data instanceof SObject) {
Object next = new DTO(data).getObject(key);
List<String> nextPath = R.of(path).drop(1).toStringList();
return nextPath.isEmpty() ? next : getObject(next, nextPath);
}
else {
return null;
}
}
private void setObject(Object data, List<String> path, Object value) {
String key = path.get(0);
if(data instanceof Map<String, Object>) {
Map<String, Object> mMap = (Map<String, Object>)data;
Object next = mMap.get(key);
List<String> nextPath = R.of(path).drop(1).toStringList();
if(nextPath.isEmpty()) {
mMap.put(key, value);
}
else {
if(!(next instanceof Map<String, Object>)) {
next = new Map<String, Object>();
mMap.put(key, next);
}
setObject(next, nextPath, value);
}
}
}
/**
* Get object from the path
*
* Example:
* Mapper.DTO d = new Mapper.DTO();
* d.getObject(new List<String>{ 'person', 'name' });
*
* @param path The path
* @return Object
* */
public Object getObject(List<String> path) {
if(path == null || path.isEmpty()) {
return null;
}
return getObject(this.data, path);
}