Skip to content

Add type field to Message and Update #93

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

Closed
wojpawlik opened this issue Feb 10, 2021 · 11 comments
Closed

Add type field to Message and Update #93

wojpawlik opened this issue Feb 10, 2021 · 11 comments

Comments

@wojpawlik
Copy link

https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions

@levlam
Copy link
Contributor

levlam commented Feb 10, 2021

This field is redundant in general. You can do that in Telegraf.JS on a framework level if you need this,

@wojpawlik
Copy link
Author

Extra code running on every update, for straying away from the API? I'll pass.

@levlam
Copy link
Contributor

levlam commented Feb 10, 2021

Extra redundant data sent to everyone over network would be much worse, so if it is needed for TypeScript, it can be added there.

@wojpawlik
Copy link
Author

error_code and ok already duplicate info available as HTTP status code.

@wojpawlik
Copy link
Author

And to comply with the API, any extra field I'd add would have to be inaccessible to library users, limiting its usefulness.

@levlam
Copy link
Contributor

levlam commented Feb 10, 2021

error_code and ok already duplicate info available as HTTP status code.

Not really. HTTP is used as a transport and API should be possible to use with different trasports. Moreover, special error_code values for specific errors can be introduced in the future and there can be transport layer errors, which result in a valid HTTP response without payload.

@tdlib tdlib deleted a comment from 0964770939 Feb 11, 2021
@tdlib tdlib deleted a comment from 0964770939 Feb 11, 2021
@tdlib tdlib deleted a comment from 0964770939 Feb 11, 2021
@tdlib tdlib deleted a comment from 0964770939 Feb 11, 2021
@wojpawlik
Copy link
Author

I feel like it's just exposing information you already have, in a more convenient format. I didn't expect push-back.

Let me try to make a proper case:

These extra fields would help users narrow down the update type (telegraf/telegraf#1319 (comment)).

They'd also help TypeScript: since "type" cannot equal "message" and "callback_query" at the same time, tt.Update.MessageUpdate & tt.Update.CallbackQueryUpdate becomes contradictory (never), so it's safe to simplify tt.Update & tt.Update.MessageUpdate into tt.Update.MessageUpdate.

Adding the discriminants on my end is not worth the hassle:

I cannot simply name it "type", because if you decide to add "type" as well, with different semantics, I'd be forced to make a breaking change. So I'd have to use a symbol property.

It's actually impossible to programmatically determine update type in a future-proof way. I'd have to use a allowlist or denylist of fields, which would get outdated with new API versions and break existing bots.

It's even worse for Message, since it has way more fields, and it's nested at one of 4 places (more in the future?) within Update.

@levlam
Copy link
Contributor

levlam commented Feb 12, 2021

These extra fields are redundant. For example, we wouldn't add separate fields Message.inline_keyboard and Message.keyboard additionally to Message.reply_markup, because that would be a lot of redundant data, even the fields can be useful for someone. But they can be easily added on a bot's side.

I cannot simply name it "type"

You can name it "message_type"/"update_type" or "_type". These names are safe.

It's actually impossible to programmatically determine update type in a future-proof way.

You don't need to do that. You wouldn't be able to match them to a specific type anyway. You can mark all messages without content or with content of unknown type as "unknown" or "other".

@wojpawlik
Copy link
Author

You can name it "message_type"/"update_type" or "_type". These names are safe.

_type is good. Is there a general rule for what names are safe?

You wouldn't be able to match them to a specific type anyway.

I would, actually. Types can be updated without updating the lib. Exceptions: new API methods, and hardcoded UpdateTypes allowlist, which could be moved to typegram in a breaking release, or eliminated if this is merged.

@levlam
Copy link
Contributor

levlam commented Feb 13, 2021

_type is good. Is there a general rule for what names are safe?

No, but Bot API never had fields beginning with underscore.
It is also very unlikely that a type X will have a new field with prefix X in the future.
Also, all fields are snake_case, so any name not in snake_case is also safe.

@wojpawlik
Copy link
Author

Adding a phantom discriminator didn't fix my issue actually... thank you for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants