1

I am using protobuf with version3 in Php. Below is my proto file

syntax ="proto3";

package message.events.user.v1;

message UserWasActivatedMessage {
  int32 userId = 1 ;
  string eventType = 2;
}

This is my proto file and whenever I do not set any user or event proto is automatically setting them up with a default value which I do not want, now I want user to explicitly define every value whenever they send a message as this will reduce the chance of not defining any value

ujwal dhakal
  • 2,289
  • 2
  • 30
  • 50

1 Answers1

1

This is a "feature" of protocol buffers and cannot be overriden using the standard SDKs. See:

https://developers.google.com/protocol-buffers/docs/proto3#default

I assume the requirement of having default values is a result of the schemaless nature of on-the-wire messages, where all fields must be included and there being no way to specify a value as nil.

Yes, there's a difference in meaning between nil and default but you can't reflect that in protocol buffers.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • Does this mean i have to check if all field are present or not from my code instead of relying on code generated from protobuf – ujwal dhakal Oct 25 '21 at 06:23
  • When you create a Protobuf message, it will include default values for any fields that you don't specify. When you receive a Protobuf message, it will include default values for any fields that weren't specified. So, I think your main concern is that you can't differentiate between nil and default fields. These are coalesced into default value fields. – DazWilkin Oct 25 '21 at 16:02
  • Yeah true, so now I am planning to validate them before sending it – ujwal dhakal Oct 28 '21 at 18:13