gRPC and numeric casting

Unlike int, a type unsigned int can only store positive integers.

In language Dart there is no such thing as unsigned int, while in the go language it is possible to use these numeric types (uint8, uint16, uint32, uint64)

In language proto3 there is also the possibility of using uint32 and uint64

I decided to conduct an experiment and make a client on Dart, server on go, and try to send negative values ​​in the fields intended for uint32 and uint64

Now I will present four options for a potential outcome (what happens if you send -1 or -9223372036854775808 in the field intended for uint)?

What do you think will happen in this case?

  1. gRPC plugin on Dart will not collect files for the client

  2. An error occurs while sending your message

  3. A message with the number x will be sent, the server interprets it as 0

Those who are curious can see the answer at the end of the article. And I’ll tell you about how the experiment itself was carried out.

Step 1 – write the proto file required for the test.

We determine the type of syntax, packages, service and the cherished message containing positive int.

Step 2 – Making an Empty Application dart, go, generate code for grpc and add dependencies

All these actions require the following series of commands:

Step 3 – Writing the server in go

Step 4 – Writing a client to dart

Step 5 – check

As you can see, the client assembled according to the provided scheme without errors, so the first option with an error at the assembly stage is no longer valid.

We start the server, client and get:

The error contains the following text:

Illegal to set field first (3) of api.Inp to value (-1): out of range for unsigned 32-bit int

Which means the type error was found at runtime.

You should be more careful when using these call types from dart, since there were no errors at the compilation stage, an error occurred when calling the method at runtime, which is a bigger problem in view of the need to write additional code to check numbers on the client side.

As can be read in the official [документации](, gRPC supports typecasting between multiple languages, but some edge cases are better checked manually.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *