Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I represent a UUID in a protobuf message?

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; } 

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.

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; } 

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.

like image 941
Edward Maxedon Avatar asked Mar 31 '16 22:03

Edward Maxedon


People also ask

What is a descriptor in Protobuf?

descriptor. Descriptors essentially contain exactly the information found in a . proto file, in types that make this information accessible in Python.

What is a Protobuf message?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

What is Protobuf tag?

These tags are used to identify your fields in the message binary format, and should not be changed once your message type is in use.

Why are Protobuf fields numbered?

Field numbers are an important part of Protobuf. They're used to identify fields in the binary encoded data, which means they can't change from version to version of your service. The advantage is that backward compatibility and forward compatibility are possible.


1 Answers

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.)

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; } 

or:

message UUID {   required bytes value = 1; } 
like image 103
Kenton Varda Avatar answered Sep 18 '22 13:09

Kenton Varda