如何在 protobuf 消息中表示 UUID? [英] How do I represent a UUID in a protobuf message?

查看:114
本文介绍了如何在 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.

推荐答案

您可能应该使用 stringbytes 来表示 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆