@@ -47,15 +47,53 @@ class ApiNarrowTopic extends ApiNarrowElement {
47
47
}
48
48
49
49
/// An [ApiNarrowElement] with the 'dm', or legacy 'pm-with', operator.
50
+ ///
51
+ /// An instance directly of this class must not be serialized with [jsonEncode] ,
52
+ /// and more generally its [operator] getter must not be called.
53
+ /// Instead, call [resolve] and use the object it returns.
50
54
class ApiNarrowDm extends ApiNarrowElement {
51
- @override String get operator => 'pm-with' ; // TODO(#146): use 'dm' where possible
55
+ @override String get operator {
56
+ assert (false ,
57
+ "The [operator] getter was called on a plain [ApiNarrowDm]. "
58
+ "Before passing to [jsonEncode] or otherwise getting [operator], "
59
+ "the [ApiNarrowDm] must be replaced by the result of [ApiNarrowDm.resolve]."
60
+ );
61
+ return "dm" ;
62
+ }
52
63
53
64
@override final List <int > operand;
54
65
55
66
ApiNarrowDm (this .operand, {super .negated});
56
67
57
- factory ApiNarrowDm .fromJson (Map <String , dynamic > json) => ApiNarrowDm (
58
- (json['operand' ] as List <dynamic >).map ((e) => e as int ).toList (),
59
- negated: json['negated' ] as bool ? ?? false ,
60
- );
68
+ factory ApiNarrowDm .fromJson (Map <String , dynamic > json) {
69
+ var operand = (json['operand' ] as List <dynamic >).map ((e) => e as int ).toList ();
70
+ var negated = json['negated' ] as bool ? ?? false ;
71
+ return (json['operator' ] == 'pm-with' )
72
+ ? ApiNarrowPmWith ._(operand, negated: negated)
73
+ : ApiNarrowDmModern ._(operand, negated: negated);
74
+ }
75
+
76
+ /// This element resolved, as either an [ApiNarrowDmModern] or an [ApiNarrowPmWith] .
77
+ ApiNarrowDm resolve ({required bool legacy}) {
78
+ return legacy ? ApiNarrowPmWith ._(operand, negated: negated)
79
+ : ApiNarrowDmModern ._(operand, negated: negated);
80
+ }
81
+ }
82
+
83
+ /// An [ApiNarrowElement] with the 'dm' operator (and not the legacy 'pm-with').
84
+ ///
85
+ /// To construct one of these, use [ApiNarrowDm.resolve] .
86
+ class ApiNarrowDmModern extends ApiNarrowDm {
87
+ @override String get operator => 'dm' ;
88
+
89
+ ApiNarrowDmModern ._(super .operand, {super .negated});
90
+ }
91
+
92
+ /// An [ApiNarrowElement] with the legacy 'pm-with' operator.
93
+ ///
94
+ /// To construct one of these, use [ApiNarrowDm.resolve] .
95
+ class ApiNarrowPmWith extends ApiNarrowDm {
96
+ @override String get operator => 'pm-with' ;
97
+
98
+ ApiNarrowPmWith ._(super .operand, {super .negated});
61
99
}
0 commit comments