1
1
import 'package:{ {pubName} }/_internal.dart'hide e;
2
+
2
3
import 'package:collection/collection.dart';
3
4
4
5
const $FreeFormObjectReflection =
5
6
MapReflection(NullableReflection(ObjectReflection()));
6
7
8
+ extension SerializationReflectionExtensions<T > on SerializationReflection<T > {
9
+ ModelReflection? getNearestModelReflection({
10
+ bool Function(ContainerReflection reflection) shouldVisitChildOf =
11
+ defaultShouldVisitChildOf,
12
+ } ) {
13
+ return getNearestReflectionOfType< ModelReflection> (shouldVisitChildOf: shouldVisitChildOf);
14
+ }
15
+ /// searches serialization reflection tree to find a reflection that matches a given type.
16
+ TReflection? getNearestReflectionOfType<
17
+ TReflection extends SerializationReflection<Object ? >>({
18
+ bool Function(ContainerReflection reflection) shouldVisitChildOf =
19
+ defaultShouldVisitChildOf,
20
+ } ) {
21
+ return getNearestReflectionWhere(
22
+ (reflection) => reflection is TReflection,
23
+ shouldVisitChildOf: shouldVisitChildOf,
24
+ ) as TReflection?;
25
+ }
26
+
27
+ static bool defaultShouldVisitChildOf(
28
+ ContainerReflection containerReflection) {
29
+ return containerReflection is! ListReflection &&
30
+ containerReflection is! SetReflection &&
31
+ containerReflection is! MapReflection;
32
+ }
33
+
34
+ SerializationReflection<Object ? >? getNearestReflectionWhere(
35
+ bool Function(SerializationReflection<Object ? > reflection) predicate, {
36
+ bool Function(ContainerReflection reflection) shouldVisitChildOf =
37
+ defaultShouldVisitChildOf,
38
+ } ) {
39
+ SerializationReflection< Object?> current = this;
40
+ while (true ) {
41
+ if (predicate(current)) {
42
+ return current;
43
+ }
44
+ if (current is ContainerReflection && shouldVisitChildOf(current)) {
45
+ current = current.subReflection;
46
+ continue;
47
+ }
48
+ return null;
49
+ }
50
+ }
51
+ }
52
+
7
53
abstract class ContainerReflection<T , TItem > extends PrimitiveReflection<T > {
8
54
const ContainerReflection(this.subReflection);
9
55
@@ -29,7 +75,7 @@ class MapReflection<T> extends ContainerReflection<Map<String, T>, T> {
29
75
src is Map< String, Object?> &&
30
76
src.values
31
77
.every((v) => subReflection.canDeserializeFunction(v, context)),
32
- onXml: (context) {
78
+ onXml: (context) {
33
79
if (src is! XmlNode) {
34
80
return false ;
35
81
}
@@ -448,4 +494,165 @@ class UndefinedWrapperReflection<T>
448
494
(src) => subReflection.cloneFunction(src),
449
495
);
450
496
}
451
- }
497
+ }
498
+
499
+ class XmlReflectionWrapper<T > extends ContainerReflection<T , T >
500
+ with HasXmlReflection {
501
+ const XmlReflectionWrapper(
502
+ super.subReflection, {
503
+ required this.xml,
504
+ } );
505
+
506
+ final XmlReflection xml;
507
+
508
+ @override
509
+ bool canDeserialize(
510
+ Object? src, [
511
+ SerializationContext context = const SerializationContext.json(),
512
+ ]) {
513
+ return context.split(
514
+ onJson: (context) => subReflection.canDeserializeFunction(src, context),
515
+ onXml: (context) {
516
+ if (src is MapEntry< XmlReflection, Object?> ) {
517
+ return subReflection.canDeserializeFunction(
518
+ src.value,
519
+ context.withXmlContainer(this),
520
+ );
521
+ }
522
+ return subReflection.canDeserializeFunction(
523
+ src,
524
+ context.withXmlContainer(this),
525
+ );
526
+ },
527
+ );
528
+ }
529
+
530
+ @override
531
+ T deserialize(
532
+ Object? src, [
533
+ SerializationContext context = const SerializationContext.json(),
534
+ ]) {
535
+ return context.split(
536
+ onJson: (context) {
537
+ return subReflection.deserializeFunction(src, context);
538
+ } ,
539
+ onXml: (context) {
540
+ if (src is MapEntry< XmlReflection, Object?> ) {
541
+ return subReflection.deserializeFunction(
542
+ src.value,
543
+ context.withXmlContainer(this),
544
+ );
545
+ }
546
+ return subReflection.deserializeFunction(
547
+ src,
548
+ context.withXmlContainer(this),
549
+ );
550
+ },
551
+ );
552
+ }
553
+
554
+ @override
555
+ Object? serialize(
556
+ T src, [
557
+ SerializationContext context = const SerializationContext.json(),
558
+ ]) {
559
+ return context.split(
560
+ onJson: (context) => subReflection.serializeFunction(src, context),
561
+ onXml: (context) {
562
+ return MapEntry(
563
+ xml,
564
+ subReflection.serializeFunction(
565
+ src,
566
+ context.withXmlContainer(this),
567
+ ),
568
+ );
569
+ } ,
570
+ );
571
+ }
572
+
573
+ @override
574
+ T empty() {
575
+ return subReflection.emptyFunction();
576
+ }
577
+
578
+ @override
579
+ T example([ExampleContext? context]) {
580
+ return subReflection.exampleFunction(context);
581
+ }
582
+
583
+ @override
584
+ T clone(T src) {
585
+ return subReflection.cloneFunction(src);
586
+ }
587
+
588
+ @override
589
+ Equality<T > get equality => subReflection.equality;
590
+ }
591
+
592
+ class EnumReflection<T extends Object, TDataType extends Object >
593
+ extends ContainerReflection<T , TDataType > {
594
+ const EnumReflection(
595
+ super.subReflection, {
596
+ required this.members,
597
+ } );
598
+
599
+ final List<EnumMemberReflection <T, TDataType >> members;
600
+
601
+ @override
602
+ T deserialize(Object? value,
603
+ [SerializationContext context = const SerializationContext.json()]) {
604
+ final deserialized = subReflection.deserializeFunction(value);
605
+ final res = members
606
+ .where((element) => element.oasValue == deserialized)
607
+ .firstOrNull;
608
+ if (res == null) {
609
+ throw ' Invalid enum value $value' ;
610
+ }
611
+ return res.value;
612
+ }
613
+
614
+ @override
615
+ bool canDeserialize(Object? value,
616
+ [SerializationContext context = const SerializationContext.json()]) {
617
+ if (! subReflection.canDeserializeFunction(value, context)) {
618
+ return false ;
619
+ }
620
+ final deserialized = subReflection.deserializeFunction(value, context);
621
+ return members.any((element) => element.oasValue == deserialized);
622
+ }
623
+
624
+ @override
625
+ Object? serialize(T value,
626
+ [SerializationContext context = const SerializationContext.json()]) {
627
+ return subReflection.serializeFunction(value as TDataType, context);
628
+ }
629
+
630
+ @override
631
+ T empty() {
632
+ return subReflection.emptyFunction() as T;
633
+ }
634
+
635
+ @override
636
+ T example([ExampleContext? context]) {
637
+ context ??= ExampleContext();
638
+ final member = members.elementAt(context.random.nextInt(members.length));
639
+ return member.value;
640
+ }
641
+
642
+ @override
643
+ T clone(T src) {
644
+ return subReflection.cloneFunction(src as TDataType) as T;
645
+ }
646
+ }
647
+
648
+ class EnumMemberReflection<T , TDataType > {
649
+ const EnumMemberReflection({
650
+ required this.dartName,
651
+ required this.oasValue,
652
+ required this.value,
653
+ } );
654
+
655
+ final String dartName;
656
+ final TDataType oasValue;
657
+ final T value;
658
+ }
0 commit comments