Skip to content

Commit 58be9c9

Browse files
committed
api [nfc]: Pull out a method Anchor.toJson
1 parent ba5e3dd commit 58be9c9

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

lib/api/route/messages.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
9292
}) {
9393
return connection.get('getMessages', GetMessagesResult.fromJson, 'messages', {
9494
'narrow': resolveLegacyElements(narrow, connection),
95-
'anchor': switch (anchor) {
96-
NumericAnchor(:var messageId) => messageId,
97-
AnchorCode.newest => RawParameter('newest'),
98-
AnchorCode.oldest => RawParameter('oldest'),
99-
AnchorCode.firstUnread => RawParameter('first_unread'),
100-
},
95+
'anchor': RawParameter(anchor.toJson()),
10196
if (includeAnchor != null) 'include_anchor': includeAnchor,
10297
'num_before': numBefore,
10398
'num_after': numAfter,
@@ -112,17 +107,28 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
112107
sealed class Anchor {
113108
/// This const constructor allows subclasses to have const constructors.
114109
const Anchor();
110+
111+
String toJson();
115112
}
116113

117114
/// An anchor value for [getMessages] other than a specific message ID.
118115
///
119116
/// https://zulip.com/api/get-messages#parameter-anchor
120-
enum AnchorCode implements Anchor { newest, oldest, firstUnread }
117+
@JsonEnum(fieldRename: FieldRename.snake, alwaysCreate: true)
118+
enum AnchorCode implements Anchor {
119+
newest, oldest, firstUnread;
120+
121+
@override
122+
String toJson() => _$AnchorCodeEnumMap[this]!;
123+
}
121124

122125
/// A specific message ID, used as an anchor in [getMessages].
123126
class NumericAnchor extends Anchor {
124127
const NumericAnchor(this.messageId);
125128
final int messageId;
129+
130+
@override
131+
String toJson() => messageId.toString();
126132
}
127133

128134
@JsonSerializable(fieldRename: FieldRename.snake)

lib/api/route/messages.g.dart

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/api/route/messages_test.dart

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ void main() {
200200
});
201201
});
202202

203+
test('Anchor.toJson', () {
204+
void checkAnchor(Anchor anchor, String expected) {
205+
check(anchor.toJson()).equals(expected);
206+
}
207+
208+
checkAnchor(AnchorCode.newest, 'newest');
209+
checkAnchor(AnchorCode.oldest, 'oldest');
210+
checkAnchor(AnchorCode.firstUnread, 'first_unread');
211+
checkAnchor(const NumericAnchor(1), '1');
212+
checkAnchor(const NumericAnchor(999999999), '999999999');
213+
checkAnchor(const NumericAnchor(10000000000000000), '10000000000000000');
214+
});
215+
203216
group('getMessages', () {
204217
Future<GetMessagesResult> checkGetMessages(
205218
FakeApiConnection connection, {
@@ -262,27 +275,19 @@ void main() {
262275
});
263276
});
264277

265-
test('anchor', () {
278+
test('numeric anchor', () {
266279
return FakeApiConnection.with_((connection) async {
267-
Future<void> checkAnchor(Anchor anchor, String expected) async {
268-
connection.prepare(json: fakeResult.toJson());
269-
await checkGetMessages(connection,
270-
narrow: const AllMessagesNarrow().apiEncode(),
271-
anchor: anchor, numBefore: 10, numAfter: 20,
272-
expected: {
273-
'narrow': jsonEncode([]),
274-
'anchor': expected,
275-
'num_before': '10',
276-
'num_after': '20',
277-
});
278-
}
279-
280-
await checkAnchor(AnchorCode.newest, 'newest');
281-
await checkAnchor(AnchorCode.oldest, 'oldest');
282-
await checkAnchor(AnchorCode.firstUnread, 'first_unread');
283-
await checkAnchor(const NumericAnchor(1), '1');
284-
await checkAnchor(const NumericAnchor(999999999), '999999999');
285-
await checkAnchor(const NumericAnchor(10000000000000000), '10000000000000000');
280+
connection.prepare(json: fakeResult.toJson());
281+
await checkGetMessages(connection,
282+
narrow: const AllMessagesNarrow().apiEncode(),
283+
anchor: const NumericAnchor(42),
284+
numBefore: 10, numAfter: 20,
285+
expected: {
286+
'narrow': jsonEncode([]),
287+
'anchor': '42',
288+
'num_before': '10',
289+
'num_after': '20',
290+
});
286291
});
287292
});
288293
});

0 commit comments

Comments
 (0)