I want to use the "required" word as variable name in .proto file

360 Views Asked by At

My proto file looks something like:

message Ack {
    bool required = 1;
    optional string address = 2;
}

It looks like I am not able to name my variable as 'required' because its already been reserved by GRPC server system. Can anyone please guide me how to create a variable that has a name "required" instead of requiring a variable?

1

There are 1 best solutions below

0
DazWilkin On

Please include more details in your question:

  • the command that you used
  • the error that resulted

I suspect the issue arises at the protocol buffer rather than gRPC layer and probably when you attempt to compile the protos using protoc.

required is a reserved word for protocol buffers version proto2 but not (currently?) in proto3.

Compilers and other tools fail to parse sources that include reserved words in unexpected locations. Even if you can get such sources to compile, it's not good practice to try to abuse this practice because it could result in unexpected behavior elsewhere.

You should consider using an alternative name:

  • required_
  • _required
  • is_required
  • ...

Using the latest version of protoc release v21.2 and generated Golang sources, I'm able to compile proto2 and proto3 sources that include a field called required:

syntax = "proto2";

package foo;

option go_package = "github.com/stackoverflow/73038286/foo;foo";

message Ack {
    required bool required = 1;
    optional string address = 2;
}

And:

syntax = "proto3";

package foo;

option go_package = "github.com/DazWilkin/stackoverflow/73038286/foo;foo";

message Ack {
    bool required = 1;
    optional string address = 2;
}