Reputation: 33
I want to use an IP field in a proto3 message. Which type in proto3 should I use to represent the same. The field will be used by Golang and C# implementation. Should I use string? or fixed32 for IPV4 and bytes for IPV6?
message xyz {
string ip_addr = 1;
}
or
message xyz {
oneof ip_addr{
fixed32 v4 = 1;
bytes v6 = 2;
}
If it is the second one, then how to encode that in Golang implementation? Like, should I first construct a string with a valid IP address and then convert it to fixed32 format or how is it?
Upvotes: 3
Views: 1985
Reputation: 12176
If you have no performance constraints here, I would just go with a string.
Almost any language has support for reading an ip address from a string with standard ipv4 or ipv6 notation. If you use a binary representation in a fixed32 or bytes field, you'll have to figure out a way to convert that in each language you use your protocol with.
You may also want to specify whether it always has to be an ip address, or if a DNS hostname could be used instead.
Upvotes: 1