Pass proto field which has value only

242 Views Asked by At

I have a service to pass input array of object to server using protobuf and successfully receive it on the server. The problem is I got the response according to the response definition. Here is the response

[ { uuid: '',
    quantity: 3,
    procurement_detail_uuid: '',
    distribution_officer_uuid: '',
    substore_uuid: '2343423443423' },
  { uuid: '',
    quantity: 3,
    procurement_detail_uuid: '',
    distribution_officer_uuid: '',
    substore_uuid: '676767' } ]

and here is the definition of the input

message ProcurementStockDistributionUpsertReqInput {
  string uuid = 1;
  int32 quantity = 2;
  string procurement_detail_uuid = 3;
  string distribution_officer_uuid = 4;
  string substore_uuid = 5;
}

as you can see some of the fields are empty. I thought about using oneof

If you have a message with many optional fields and where at most one field will be set at the same time, you can enforce this behavior and save memory by using the oneof feature.

I thought oneof is the right way to optionally pass fields which has value but hit the error. Then I rollback my code like this

service Procurement {
  rpc distributeUpsert (ProcurementStockDistributionUpsertReq) returns (ProcurementStockDistributionRes) {}
}

message ProcurementStockDistributionUpsertReq {
  message ProcurementStockDistributionUpsertReqInput {
    string uuid = 1;
    int32 quantity = 2;
    string procurement_detail_uuid = 3;
    string distribution_officer_uuid = 4;
    string substore_uuid = 5;
  }
  repeated ProcurementStockDistributionUpsertReqInput stocks = 1;
}

How to pre-omit those empty fields in proto

0

There are 0 best solutions below