-
Notifications
You must be signed in to change notification settings - Fork 471
[1/6] api: Audit enums to handle unknown values where appropriate #2339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
4983a7c
80bed4b
bb5b96a
f854928
9c56c05
1aa4aac
945522d
acf3ce5
be1721a
cce7d5b
e6bad69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1405,8 +1405,9 @@ class DeleteMessageEvent extends Event { | |
|
|
||
| final List<int> messageIds; | ||
| // final int messageId; // Not present; we support the bulk_message_deletion capability | ||
| // The server never actually sends "direct" here yet (it's "private" instead), | ||
| // but we accept both forms for forward-compatibility. | ||
| // The server never actually sends "channel" or "direct" here yet | ||
| // (it's "stream" or "private" instead), but we accept both the old | ||
| // and new forms for forward-compatibility. | ||
| @MessageTypeConverter() | ||
| final MessageType messageType; | ||
| final int? streamId; | ||
|
|
@@ -1423,7 +1424,7 @@ class DeleteMessageEvent extends Event { | |
| factory DeleteMessageEvent.fromJson(Map<String, dynamic> json) { | ||
| final result = _$DeleteMessageEventFromJson(json); | ||
| // Crunchy-shell validation | ||
| if (result.messageType == MessageType.stream) { | ||
| if (result.messageType == .channel) { | ||
| result.streamId as int; | ||
| result.topic as String; | ||
| } | ||
|
|
@@ -1434,30 +1435,6 @@ class DeleteMessageEvent extends Event { | |
| Map<String, dynamic> toJson() => _$DeleteMessageEventToJson(this); | ||
| } | ||
|
|
||
| /// As in [DeleteMessageEvent.messageType], | ||
| /// [UpdateMessageFlagsMessageDetail.type], | ||
| /// or [TypingEvent.messageType]. | ||
| @JsonEnum(alwaysCreate: true) | ||
| enum MessageType { | ||
| stream, | ||
| direct; | ||
| } | ||
|
|
||
| class MessageTypeConverter extends JsonConverter<MessageType, String> { | ||
| const MessageTypeConverter(); | ||
|
|
||
| @override | ||
| MessageType fromJson(String json) { | ||
| if (json == 'private') json = 'direct'; // TODO(server-future) | ||
| return $enumDecode(_$MessageTypeEnumMap, json); | ||
| } | ||
|
|
||
| @override | ||
| String toJson(MessageType object) { | ||
| return _$MessageTypeEnumMap[object]!; | ||
| } | ||
| } | ||
|
|
||
| /// A Zulip event of type `update_message_flags`. | ||
| /// | ||
| /// For the corresponding API docs, see subclasses. | ||
|
|
@@ -1536,8 +1513,9 @@ class UpdateMessageFlagsRemoveEvent extends UpdateMessageFlagsEvent { | |
| /// As in [UpdateMessageFlagsRemoveEvent.messageDetails]. | ||
| @JsonSerializable(fieldRename: FieldRename.snake) | ||
| class UpdateMessageFlagsMessageDetail { | ||
| // The server never actually sends "direct" here yet (it's "private" instead), | ||
| // but we accept both forms for forward-compatibility. | ||
| // The server never actually sends "channel" or "direct" here yet | ||
| // (it's "stream" or "private" instead), but we accept both the old | ||
| // and new forms for forward-compatibility. | ||
| @MessageTypeConverter() | ||
| final MessageType type; | ||
| final bool? mentioned; | ||
|
|
@@ -1557,10 +1535,10 @@ class UpdateMessageFlagsMessageDetail { | |
| final result = _$UpdateMessageFlagsMessageDetailFromJson(json); | ||
| // Crunchy-shell validation | ||
| switch (result.type) { | ||
| case MessageType.stream: | ||
| case .channel: | ||
| result.streamId as int; | ||
| result.topic as String; | ||
| case MessageType.direct: | ||
| case .direct: | ||
| result.userIds as List<int>; | ||
| } | ||
| return result; | ||
|
|
@@ -1614,6 +1592,8 @@ class TypingEvent extends Event { | |
| String get type => 'typing'; | ||
|
|
||
| final TypingOp op; | ||
| // The server never actually sends "channel" here yet (it's "stream" instead), | ||
| // but we accept both forms for forward-compatibility. | ||
|
Comment on lines
+1595
to
+1596
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's have a comment/dartdoc on |
||
| @MessageTypeConverter() | ||
| final MessageType messageType; | ||
| @JsonKey(readValue: _readSenderId) | ||
|
|
@@ -1647,10 +1627,10 @@ class TypingEvent extends Event { | |
| final result = _$TypingEventFromJson(json); | ||
| // Crunchy-shell validation | ||
| switch (result.messageType) { | ||
| case MessageType.stream: | ||
| case .channel: | ||
| result.streamId as int; | ||
| result.topic as String; | ||
| case MessageType.direct: | ||
| case .direct: | ||
| result.recipientIds as List<int>; | ||
| } | ||
| return result; | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1289,6 +1289,35 @@ sealed class Message<T extends Conversation> extends MessageBase<T> { | |||||
| Map<String, dynamic> toJson(); | ||||||
| } | ||||||
|
|
||||||
| /// As in [DeleteMessageEvent.messageType], | ||||||
| /// [UpdateMessageFlagsMessageDetail.type], | ||||||
| /// or [TypingEvent.messageType]. | ||||||
| @JsonEnum(alwaysCreate: true) | ||||||
| enum MessageType { | ||||||
| channel, | ||||||
| direct; | ||||||
|
|
||||||
| factory fromJson(String json) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Don't think we've used nameless constructors yet.
Suggested change
Also without the name, it's easy to confuse it as a method. |
||||||
| if (json == 'stream') json = 'channel'; // TODO(server-future) | ||||||
| if (json == 'private') json = 'direct'; // TODO(server-future) | ||||||
| return $enumDecode(_$MessageTypeEnumMap, json); | ||||||
|
Comment on lines
+1301
to
1303
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Now that there are more than one cases, let's use |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| class MessageTypeConverter extends JsonConverter<MessageType, String> { | ||||||
| const MessageTypeConverter(); | ||||||
|
|
||||||
| @override | ||||||
| MessageType fromJson(String json) { | ||||||
| return MessageType.fromJson(json); | ||||||
| } | ||||||
|
|
||||||
| @override | ||||||
| String toJson(MessageType object) { | ||||||
| return _$MessageTypeEnumMap[object]!; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// https://zulip.com/api/update-message-flags#available-flags | ||||||
| @JsonEnum(fieldRename: FieldRename.snake, alwaysCreate: true) | ||||||
| enum MessageFlag { | ||||||
|
|
||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This too is technically out of scope for #1982, but it's fine to have.