In protobuf can I constrain a map's key to certain values?

Viewed 234

So protobuf does not allow string enums to be keys in a map I want to constrain a map to only have the keys a, b, or c. I'm working in Kotlin and can use validators like [(validate.rules).string = {pattern: "^a|b|c$"}]

message payload {
  map<desired_constrained_key_here, string> my_map = 1;
}

Can this be done? How can I do it? I don't see this in the protobuf docs.

1 Answers

No, that is not possible; there is no syntax to express that, and no enforcement in any implementation.

However, the following is both possible and much more efficient, but requires different usage:

message payload {
  string a = 1;
  string b = 2;
  string c = 3;
}

This is, ultimately, another way of representing a map between the 3 optional keys a, b, c - and a string values for each.

Related