如何在 protobuf 消息中表示 UUID?

编程入门 行业动态 更新时间:2024-10-17 17:22:02
本文介绍了如何在 protobuf 消息中表示 UUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将 UUID 附加到我的 protobuf 用户消息示例中的字段.

I want to attach a UUID to a field in my protobuf User message example.

message User { // field containing id as UUID type required string email; optional string name; }

我知道 protobuf 消息还不支持 UUID 类型.我读过最好的方法是使用 UUID 消息类型.

I know that protobuf messages do not yet support the UUID type. I've read that the best approach is to have a UUID message type.

所以我猜我的用户消息会导入我的 UUID 消息原型定义并将其用作字段类型,如下所示:

So I'm guessing my User message would import my UUID message proto definition and use it as a field type like so:

import "myproject/UUID.proto"; message User { required UUID id; required string email; optional string name; }

我的问题是,UUID 消息会是什么样子,我将如何对其进行编码/解码?我的目标是 Java/Scala 和 C# 兼容性.

My question is, how will the UUID message look like, and how will I encode/decode it? I'm aiming for Java/Scala and C# compatibility.

推荐答案

您可能应该使用 string 或 bytes 来表示 UUID.如果将 UUID 保持为人类可读的格式(例如 "de305d54-75b4-431b-adb2-eb6b9e546014")最方便,请使用 string 或使用 bytes 如果您要存储 128 位原始值.(如果您不确定,您可能需要 string.)

You should probably use string or bytes to represent a UUID. Use string if it is most convenient to keep the UUID in human-readable format (e.g. "de305d54-75b4-431b-adb2-eb6b9e546014") or use bytes if you are storing the 128-bit value raw. (If you aren't sure, you probably want string.)

将值包装在名为 UUID 的消息类型中有助于使代码更具自文档性,但会产生一些性能开销并且不是严格要求的.如果你想这样做,定义类型如下:

Wrapping the value in a message type called UUID can be helpful to make the code more self-documenting but will have some performance overhead and isn't strictly required. If you want to do this, define the type like:

message UUID { required string value = 1; }

或:

message UUID { required bytes value = 1; }

更多推荐

如何在 protobuf 消息中表示 UUID?

本文发布于:2023-11-30 09:25:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   如何在   protobuf   UUID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!