GRPC原始Buff/URI类型

发布于 2025-02-05 06:39:07 字数 314 浏览 2 评论 0原文

是否可以在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命 2025-02-12 06:39:07

存储在字符串中是一个完全可行的解决方案。但是,如果您想稍微降低有效载荷尺寸并使实例化一点更安全,则可以删除URL的架构部分(例如:https://)。

To do that you could do the following:

message Url {
    enum Schema {
        UNSPECIFIED = 0;
        HTTP = 1;
        HTTPS = 2;
        // more if needed
    }

    Schema schema = 1;
    string rest = 2;
}

and then you can use it in your message like this:

message NotificationResponse {
    string title = 1;
    Url url = 2;
}
  • This would exclude these non necessary character in the string and reduce the payload size (remove http(s) and ://)。枚举比字符串更有效地序列化。
  • 这将使实例化更加安全,因为您至少可以限制您和其他开发人员可以使用的模式(在我的示例中,没有FTP,甚至仅限于HTTPS以进行安全安全)。

不过,这给您带来了不便。您必须在客户端代码中重新加以串联,但是在我看来,这是值得的,因为串联非常微不足道(更改枚举值为文本值并添加://)。

注意:对域名(.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:

message Url {
    enum Schema {
        UNSPECIFIED = 0;
        HTTP = 1;
        HTTPS = 2;
        // more if needed
    }

    Schema schema = 1;
    string rest = 2;
}

and then you can use it in your message like this:

message NotificationResponse {
    string title = 1;
    Url url = 2;
}
  • This would exclude these non necessary character in the string and reduce the payload size (remove http(s) and ://). Enum are serialized more efficiently than string.
  • This would make instantiation safer because you can restrict at least the schema that you and other developers can use (in my example, no ftp or even restrict to only https for security).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文