8

See https://github.com/grpc/grpc-node/issues/1202.

Usually in CRUD operations, the value not provided means do not change that field, and the empty array [] means to clear all items inside that field.

But if you tries to implement CRUD operations and provide them as services via grpc, then the above scenario is hard to implement.

service CRUD {
  rpc updateTable(updateRequest) returns updateResponse {}
}

message updateRequest {
  repeated string a = 1;
  string b = 2;
}

message updateResponse {
  boolean success = 1;
}

If you load the package with default options, then the client can't delete items of a by

client.CRUD.updateTable({a: []})

because the argument {a: []} becomes {} when it arrives the server side.

If you load the package with options {arrays: true}, then the field a will be cleared unintentionally while client side only tries to update other fields:

client.CRUD.updateTable({b: 'updated value'}) 

because the argument {b: 'updated value'} becomes {a: [], b: 'updated value'} when it arrives the server side.

Can anyone share some better ideas regards how to handle these 2 scenarios with grpc-node and proto3?

1
  • 2
    Protobuf has no concept of "empty/null", or arrays. It just has "repeated", and something that is repeated zero times is simply: omitted completely from the output (caveat: technically it is possible to encode "packed" primitives of zero count; not sure if the main encoders do this, and: a lot of repeated data isn't primitives). My point: no simple answers except maybe a second field to signal your intent. Nov 27, 2019 at 7:00

1 Answer 1

7

The protobuf encoding doesn't distinguish between these two cases. Since protobuf is language-agnostic, it doesn't understand the conceptual nuance of "undefined" versus "[]" of Javascript.

You would need to pass additional information inside the proto message in order to distinguish between the two cases.

I would highly suggest reading the design documentations here: https://developers.google.com/protocol-buffers

2
  • 3
    How does other language to distinguish the not changed and clear operations on CRUD operations? And is it a bad design that distinguish the not changed and clear operations by using undefined and [] in JavaScript?
    – Jeff Tian
    Nov 28, 2019 at 12:17
  • 1
    Other languages have a version of null that could be used and this seems like a huge oversight.
    – swade
    Jul 22, 2020 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.