How to distinguish not provided and empty array in grpc service?
Asked Answered
M

1

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?

Milton answered 27/11, 2019 at 6:13 Comment(1)
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.Thedathedric
N
8

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

Nodular answered 27/11, 2019 at 18:28 Comment(2)
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?Milton
Other languages have a version of null that could be used and this seems like a huge oversight.Kele

© 2022 - 2024 — McMap. All rights reserved.