GRPC原始Buff/URI类型
是否可以在GRPC消息中使用URL或URI数据类型?如果不是,最好的方法是什么?
我正在为此使用GRPC和协议缓冲区,并且运行了一个后端GO应用程序来触发在Flutter应用中显示的弹出通知。该通知具有一个链接,该链接将用户在我的Flutter应用中单击时将用户转到网页。
现在,我正在为此使用一个字符串,这样:
message NotificationResponse{
string title = 1;
string url = 2;
}
我似乎找不到将URL/URI用作类型的方法。有这样的东西吗?
Is there a way to use Url or Uri data type inside a message of gRPC? And if not what is the best way to do this?
I am using gRPC and Protocol Buffers for this and I run a backend go app to trigger popup notifications that display in my Flutter app. The notification has a link that takes the user to a webpage when clicked on in my Flutter app.
Right now I am using a String for this, like so:
message NotificationResponse{
string title = 1;
string url = 2;
}
I can't seem to find a way to use Url/Uri as a type. Is there such a thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存储在字符串中是一个完全可行的解决方案。但是,如果您想稍微降低有效载荷尺寸并使实例化一点更安全,则可以删除URL的架构部分(例如:https://)。
To do that you could do the following:
and then you can use it in your message like this:
http(s)
and://
)。枚举比字符串更有效地序列化。不过,这给您带来了不便。您必须在客户端代码中重新加以串联,但是在我看来,这是值得的,因为串联非常微不足道(更改枚举值为文本值并添加
://
)。注意:对域名(.com,.net,...)进行相同的枚举技巧不会那么琐碎,并且会迫使您将路径和主机存储在不同的字段中(因为它会增加有效载荷,因此不值得)。
让我知道您是否需要更多帮助。
Storing in a string is a totally viable solution. But if you would like to reduce the payload size a little and make the instantiation little bit safer, you could remove the schema part of the url (eg: https://).
To do that you could do the following:
and then you can use it in your message like this:
http(s)
and://
). Enum are serialized more efficiently than string.That has one inconvenience though. You will have to concatenate that back in your client code, but in my opinion this is worth the effort since the concatenation is pretty trivial (change enum value to is text value and add
://
).Note: doing the same enum trick for Domain Name (.com, .net, ...) would not be as trivial and would force you to store the path and the host in different field (not worth it since it increase payload).
Let me know if you need more help.