I am trying to (mis??)use the .proto definition to add a scalingFactor to all my scalar values such that both sides that are communicating can decide for themselves to internally use money values in cents, floats or decimals. In that I want to end up with just an integer going over the wire. Not two integers (value and scalingFactor for instance, or value and nanos).
If I had the freedom to just add some metadata at field level by putting it between [], it would look like this:
syntax = "not valid proto3....";
message Product1 {
int32 id = 1;
string productName = 2;
int32 priceInCents = 3
int32 weightInTons = 4
}
message Product2 {
int32 id = 1;
string productName = 2;
int32 price = 3 [scalingFactor = .01]
int32 weight = 4 [scalingFactor = 1000]
}
I would like to end up with a definition like it is described (but not valid of course) for Product2. Such that both sides can get the scalingFactor from the .proto definition. And then use that scalingFactor in their own code. Some might convert the price to a decimal in JavaScript or leave it in cents in C (e.g. a struct called price with int32 for value (in cents) and int32 for scale (e.g. -2 representing .01) )
Is there an obvious way to do this? Ideally it is simple.
UPDATE based on answer:
I now realise by paxdiablo's reaction that I was both unclear and maybe even in doubt myself.Unclear part: If price is $2.50 and weight is 7000kg, I want to transmit 250 and 7 over the wire in all cases. What I was not looking for is for protobuf to handle the scaling for me. It is more that I wanted to have the context of how transmitted values are scaled to become available to the code.
Doubt: I am not sure I want to use the generated classes in my own code at both sides. I saw protobuf more as the transport mechanism only. And create my own variables again based on the transmitted values or values to be transmitted.