How do I represent a UUID in a protobuf message?

后端 未结 3 747
离开以前
离开以前 2021-01-03 17:25

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


        
相关标签:
3条回答
  • 2021-01-03 18:01

    If anything, you want to use string to avoid problems with endianness. Note that a UUID and a MS GUID that have the same string representation (and therefore are the same "id") have, however, different byte-stream order (big-endian vs little-endian). If you use bytes in the protocol to communicate between Java using UUID and C# using System.Guid, you could end up with flipped IDs.

    0 讨论(0)
  • 2021-01-03 18:04

    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;
    }
    
    0 讨论(0)
  • 2021-01-03 18:11

    I don't have enough reputation points to make a comment, so I have to write this as an answer.

    Use a string, not a byte array unlike what some other commenters are saying. According to MS (https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/protobuf-data-types), "Don't use a bytes field for Guid values. Problems with endianness (Wikipedia definition) can result in erratic behavior when Protobuf is interacting with other platforms, such as Java."

    0 讨论(0)
提交回复
热议问题